Skip to content

Commit 7533d08

Browse files
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 aa52873 commit 7533d08

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-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,
@@ -1259,12 +1259,31 @@ function atob(input) {
12591259
if (arguments.length === 0) {
12601260
throw new ERR_MISSING_ARGS('input');
12611261
}
1262+
12621263
input = `${input}`;
1264+
let nonAsciiWhitespaceCharCount = 0;
1265+
12631266
for (let n = 0; n < input.length; n++) {
1264-
if (!ArrayPrototypeIncludes(kForgivingBase64AllowedChars,
1265-
StringPrototypeCharCodeAt(input, n)))
1267+
const index = ArrayPrototypeIndexOf(
1268+
kForgivingBase64AllowedChars,
1269+
StringPrototypeCharCodeAt(input, n));
1270+
1271+
if (index > 4) {
1272+
// The first 5 elements of `kForgivingBase64AllowedChars` are
1273+
// ASCII whitespace char codes.
1274+
nonAsciiWhitespaceCharCount++;
1275+
} else if (index === -1) {
12661276
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
1277+
}
12671278
}
1279+
1280+
// See #3 - https://infra.spec.whatwg.org/#forgiving-base64
1281+
if (nonAsciiWhitespaceCharCount % 4 === 1) {
1282+
throw lazyDOMException(
1283+
'The string to be decoded is not correctly encoded.',
1284+
'InvalidCharacterError');
1285+
}
1286+
12681287
return Buffer.from(input, 'base64').toString('latin1');
12691288
}
12701289

test/parallel/test-btoa-atob.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,26 @@ 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+
constructor: DOMException,
37+
name: 'InvalidCharacterError',
38+
code: 5,
39+
}));

0 commit comments

Comments
 (0)