]> git.kaiwu.me - njs.git/commitdiff
Fixed atob() with non-padded base64 strings.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 23 Feb 2024 01:38:58 +0000 (17:38 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 23 Feb 2024 01:38:58 +0000 (17:38 -0800)
This fixes #695 issue on Github.

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

index 3a944ecd910c411f78e2218bf146704aa4922320..f22ab43a2519dddf8bb6e276b2557aa2d5b592eb 100644 (file)
@@ -4298,7 +4298,14 @@ njs_string_atob(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         }
     }
 
-    len = njs_base64_decoded_length(str.length, pad);
+    len = str.length;
+
+    if (len % 4 != 0) {
+        pad = 4 - (len % 4);
+        len += pad;
+    }
+
+    len = njs_base64_decoded_length(len, pad);
 
     njs_chb_init(&chain, vm->mem_pool);
 
index 4e54c7121b894af28fcfe1949483c3c46b9b46db..636157700c37eea55b1609a7937ba8fb007a59b6 100644 (file)
@@ -10093,6 +10093,18 @@ static njs_unit_test_t  njs_test[] =
               "].every(v => c(atob(v)).toString() == '8,52,86')"),
       njs_str("true")},
 
+    { njs_str("atob('aGVsbG8=')"),
+      njs_str("hello") },
+
+    { njs_str("atob('aGVsbG8')"),
+      njs_str("hello") },
+
+    { njs_str("atob('TQ==')"),
+      njs_str("M") },
+
+    { njs_str("atob('TQ')"),
+      njs_str("M") },
+
     /* Functions. */
 
     { njs_str("return"),