[njs] Fixed atob() with non-padded base64 strings.

Dmitry Volyntsev xeioex at nginx.com
Fri Feb 23 01:39:33 UTC 2024


details:   https://hg.nginx.org/njs/rev/272af619b821
branches:  
changeset: 2289:272af619b821
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Feb 22 17:38:58 2024 -0800
description:
Fixed atob() with non-padded base64 strings.

This fixes #695 issue on Github.

diffstat:

 src/njs_string.c         |   9 ++++++++-
 src/test/njs_unit_test.c |  12 ++++++++++++
 2 files changed, 20 insertions(+), 1 deletions(-)

diffs (41 lines):

diff -r 0479e5821ab2 -r 272af619b821 src/njs_string.c
--- a/src/njs_string.c	Wed Feb 14 21:34:02 2024 -0800
+++ b/src/njs_string.c	Thu Feb 22 17:38:58 2024 -0800
@@ -4298,7 +4298,14 @@ njs_string_atob(njs_vm_t *vm, njs_value_
         }
     }
 
-    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);
 
diff -r 0479e5821ab2 -r 272af619b821 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Feb 14 21:34:02 2024 -0800
+++ b/src/test/njs_unit_test.c	Thu Feb 22 17:38:58 2024 -0800
@@ -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"),


More information about the nginx-devel mailing list