Skip to content

Commit 822e450

Browse files
committed
Fixed atob() with non-padded base64 strings.
This fixes #695 issue on Github.
1 parent a26f8cc commit 822e450

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/njs_string.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,7 +4298,14 @@ njs_string_atob(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
42984298
}
42994299
}
43004300

4301-
len = njs_base64_decoded_length(str.length, pad);
4301+
len = str.length;
4302+
4303+
if (len % 4 != 0) {
4304+
pad = 4 - (len % 4);
4305+
len += pad;
4306+
}
4307+
4308+
len = njs_base64_decoded_length(len, pad);
43024309

43034310
njs_chb_init(&chain, vm->mem_pool);
43044311

src/test/njs_unit_test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10093,6 +10093,18 @@ static njs_unit_test_t njs_test[] =
1009310093
"].every(v => c(atob(v)).toString() == '8,52,86')"),
1009410094
njs_str("true")},
1009510095

10096+
{ njs_str("atob('aGVsbG8=')"),
10097+
njs_str("hello") },
10098+
10099+
{ njs_str("atob('aGVsbG8')"),
10100+
njs_str("hello") },
10101+
10102+
{ njs_str("atob('TQ==')"),
10103+
njs_str("M") },
10104+
10105+
{ njs_str("atob('TQ')"),
10106+
njs_str("M") },
10107+
1009610108
/* Functions. */
1009710109

1009810110
{ njs_str("return"),

0 commit comments

Comments
 (0)