Skip to content

Commit c6d8503

Browse files
austinkelleherjuanarbol
authored andcommitted
buffer: fix atob input validation
This commit fixes a few inconsistencies between Node.js `atob` implementation and the WHATWG spec. Refs: https://infra.spec.whatwg.org/#forgiving-base64-decode Fixes: #42646 PR-URL: #42662 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com>
1 parent 567b96a commit c6d8503

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/buffer.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const {
2626
ArrayFrom,
2727
ArrayIsArray,
2828
ArrayPrototypeForEach,
29-
ArrayPrototypeIncludes,
29+
ArrayPrototypeIndexOf,
3030
MathFloor,
3131
MathMin,
3232
MathTrunc,
@@ -1260,12 +1260,31 @@ function atob(input) {
12601260
if (arguments.length === 0) {
12611261
throw new ERR_MISSING_ARGS('input');
12621262
}
1263+
12631264
input = `${input}`;
1265+
let nonAsciiWhitespaceCharCount = 0;
1266+
12641267
for (let n = 0; n < input.length; n++) {
1265-
if (!ArrayPrototypeIncludes(kForgivingBase64AllowedChars,
1266-
StringPrototypeCharCodeAt(input, n)))
1268+
const index = ArrayPrototypeIndexOf(
1269+
kForgivingBase64AllowedChars,
1270+
StringPrototypeCharCodeAt(input, n));
1271+
1272+
if (index > 4) {
1273+
// The first 5 elements of `kForgivingBase64AllowedChars` are
1274+
// ASCII whitespace char codes.
1275+
nonAsciiWhitespaceCharCount++;
1276+
} else if (index === -1) {
12671277
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
1278+
}
12681279
}
1280+
1281+
// See #3 - https://infra.spec.whatwg.org/#forgiving-base64
1282+
if (nonAsciiWhitespaceCharCount % 4 === 1) {
1283+
throw lazyDOMException(
1284+
'The string to be decoded is not correctly encoded.',
1285+
'InvalidCharacterError');
1286+
}
1287+
12691288
return Buffer.from(input, 'base64').toString('latin1');
12701289
}
12711290

test/parallel/test-btoa-atob.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,25 @@ throws(() => buffer.atob(), /TypeError/);
1414
throws(() => buffer.btoa(), /TypeError/);
1515

1616
strictEqual(atob(' '), '');
17-
strictEqual(atob(' YW\tJ\njZA=\r= '), 'abcd');
17+
strictEqual(atob(' Y\fW\tJ\njZ A=\r= '), 'abcd');
18+
19+
strictEqual(atob(null), '\x9Eée');
20+
strictEqual(atob(NaN), '5£');
21+
strictEqual(atob(Infinity), '"wâ\x9E+r');
22+
strictEqual(atob(true), '¶»\x9E');
23+
strictEqual(atob(1234), '×mø');
24+
strictEqual(atob([]), '');
25+
strictEqual(atob({ toString: () => '' }), '');
26+
strictEqual(atob({ [Symbol.toPrimitive]: () => '' }), '');
27+
28+
throws(() => atob(Symbol()), /TypeError/);
29+
[
30+
undefined, false, () => {}, {}, [1],
31+
0, 1, 0n, 1n, -Infinity,
32+
'a', 'a\n\n\n', '\ra\r\r', ' a ', '\t\t\ta', 'a\f\f\f', '\ta\r \n\f',
33+
].forEach((value) =>
34+
// See #2 - https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
35+
throws(() => atob(value), {
36+
name: 'InvalidCharacterError',
37+
code: 5,
38+
}));

0 commit comments

Comments
 (0)