]> git.kaiwu.me - njs.git/commitdiff
Fixed decodeURI() and decodeURIComponent() with invalid byte strings.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 29 Oct 2021 13:57:26 +0000 (13:57 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 29 Oct 2021 13:57:26 +0000 (13:57 +0000)
The issue was introduced in 855edd76bdb6 (0.4.3).

This closes #435 issue on Github.

src/njs_string.c
src/test/njs_unit_test.c

index fc7b3deceb19db71f749beba3c0274c7ba5e9160..871561bf259ec4f97c6cba17632815fb40d32b5a 100644 (file)
@@ -4496,26 +4496,27 @@ njs_string_decode_uri_cp(const int8_t *hex, const u_char **start,
 
     cp = njs_utf8_decode(&ctx, start, end);
     if (njs_fast_path(cp != '%')) {
-        return expect_percent ? 0xFFFFFFFF: cp;
+        return expect_percent ? NJS_UNICODE_ERROR : cp;
     }
 
     p = *start;
 
     if (njs_slow_path((p + 1) >= end)) {
-        return 0xFFFFFFFF;
+        return NJS_UNICODE_ERROR;
     }
 
     d0 = hex[*p++];
     if (njs_slow_path(d0 < 0)) {
-        return 0xFFFFFFFF;
+        return NJS_UNICODE_ERROR;
     }
 
     d1 = hex[*p++];
     if (njs_slow_path(d1 < 0)) {
-        return 0xFFFFFFFF;
+        return NJS_UNICODE_ERROR;
     }
 
     *start += 2;
+
     return (d0 << 4) + d1;
 }
 
@@ -4631,7 +4632,7 @@ njs_string_decode_uri(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     while (src < end) {
         percent = (src[0] == '%');
         cp = njs_string_decode_uri_cp(hex, &src, end, 0);
-        if (njs_slow_path(cp == 0xFFFFFFFF)) {
+        if (njs_slow_path(cp > NJS_UNICODE_MAX_CODEPOINT)) {
             goto uri_error;
         }
 
@@ -4677,7 +4678,7 @@ njs_string_decode_uri(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 
         for (i = 1; i < n; i++) {
             cp = njs_string_decode_uri_cp(hex, &src, end, 1);
-            if (njs_slow_path(cp == 0xFFFFFFFF)) {
+            if (njs_slow_path(cp > NJS_UNICODE_MAX_CODEPOINT)) {
                 goto uri_error;
             }
 
index 4309c508f3f7a7052c859d47a9161c86318befb8..1cfa7699d8674c58731f883eb8d37d780e96fca4 100644 (file)
@@ -9209,6 +9209,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("decodeURI('%D0%B0%D0%B1%D0%B2').length"),
       njs_str("3")},
 
+    { njs_str("decodeURI(String.bytesFrom([0x80,0x80]))"),
+      njs_str("URIError: malformed URI")},
+
     { njs_str("["
               " '%',"
               " '%0',"