Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: atob throw error when the input value is invalid #42662

Merged
merged 6 commits into from
Apr 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
buffer: simplify atob approach of disregarding whitespace
* Use approach of counting non-ASCII whitespace characters instead of
  removing the characters via string replacement
* Additional atob validation tests have been added
austinkelleher committed Apr 9, 2022
commit 08e4c7567eea122f0bff36468869ec6d093eb551
33 changes: 16 additions & 17 deletions lib/buffer.js
Original file line number Diff line number Diff line change
@@ -1232,12 +1232,14 @@ function btoa(input) {
return buf.toString('base64');
}

// Refs: https://infra.spec.whatwg.org/#forgiving-base64-decode
const kForgivingBase64AllowedChars = [
const asciiWhitespaceCharacters = [
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved
// ASCII whitespace
// Refs: https://infra.spec.whatwg.org/#ascii-whitespace
0x09, 0x0A, 0x0C, 0x0D, 0x20,
];

// Refs: https://infra.spec.whatwg.org/#forgiving-base64-decode
const kForgivingBase64AllowedChars = [
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved
// Uppercase letters
...ArrayFrom({ length: 26 }, (_, i) => StringPrototypeCharCodeAt('A') + i),

@@ -1260,32 +1262,29 @@ function atob(input) {
throw new ERR_MISSING_ARGS('input');
}

if (input === undefined || input === false || typeof input === 'number') {
throw lazyDOMException(
'The string to be decoded is not correctly encoded.',
'ValidationError');
}
input = `${input}`;
let nonAsciiWhitespaceCharCount = 0;

// Remove all ASCII whitespace from data.
//
// See #1 - https://infra.spec.whatwg.org/#forgiving-base64
input = `${input}`.replace(/\s/g, '');
for (let n = 0; n < input.length; n++) {
const char = StringPrototypeCharCodeAt(input, n);

if (ArrayPrototypeIncludes(kForgivingBase64AllowedChars, char)) {
nonAsciiWhitespaceCharCount++;
} else if (!ArrayPrototypeIncludes(asciiWhitespaceCharacters, char)) {
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
}
}

// If data's code point length divides by 4 leaving a remainder of 1, then
// return failure.
//
// See #3 - https://infra.spec.whatwg.org/#forgiving-base64
if (input.length % 4 === 1) {
if (nonAsciiWhitespaceCharCount % 4 === 1) {
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved
throw lazyDOMException(
'The string to be decoded is not correctly encoded.',
'ValidationError');
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved
}

for (let n = 0; n < input.length; n++) {
if (!ArrayPrototypeIncludes(kForgivingBase64AllowedChars,
StringPrototypeCharCodeAt(input, n)))
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
}
return Buffer.from(input, 'base64').toString('latin1');
}

21 changes: 13 additions & 8 deletions test/parallel/test-btoa-atob.js
Original file line number Diff line number Diff line change
@@ -16,11 +16,16 @@ throws(() => buffer.btoa(), /TypeError/);
strictEqual(atob(' '), '');
strictEqual(atob(' YW\tJ\njZA=\r= '), 'abcd');
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved

throws(() => buffer.atob(undefined), /ValidationError/);
throws(() => buffer.atob(false), /ValidationError/);
throws(() => buffer.atob(1), /ValidationError/);
throws(() => buffer.atob(0), /ValidationError/);
throws(() => buffer.atob('a'), /ValidationError/);
throws(() => buffer.atob('a '), /ValidationError/);
throws(() => buffer.atob(' a'), /ValidationError/);
throws(() => buffer.atob('aaaaa'), /ValidationError/);
strictEqual(atob(null), '\x9Eée');
strictEqual(atob(NaN), '5£');
strictEqual(atob(Infinity), '"wâ\x9E+r');
strictEqual(atob(true), '¶»\x9E');
strictEqual(atob(1234), '×mø');
strictEqual(atob([]), '');
strictEqual(atob({ toString: () => '' }), '');
strictEqual(atob({ [Symbol.toPrimitive]: () => '' }), '');

throws(() => atob(), /ERR_MISSING_ARGS/);
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
throws(() => atob(Symbol()), /TypeError/);
[undefined, false, () => {}, 0, 1, 0n, 1n, -Infinity, [1], {}].forEach((value) =>
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved
throws(() => atob(value), { constructor: DOMException }));
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved