diff --git a/lib/buffer.js b/lib/buffer.js index 773d56572aa2a4..2b7536643ba95d 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1259,7 +1259,28 @@ function atob(input) { if (arguments.length === 0) { throw new ERR_MISSING_ARGS('input'); } - input = `${input}`; + + if (input === undefined || input === false || typeof input === 'number') { + throw lazyDOMException( + 'The string to be decoded is not correctly encoded.', + 'ValidationError'); + } + + // Remove all ASCII whitespace from data. + // + // See #1 - https://infra.spec.whatwg.org/#forgiving-base64 + input = `${input}`.replace(/\s/g, ''); + + // 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) { + throw lazyDOMException( + 'The string to be decoded is not correctly encoded.', + 'ValidationError'); + } + for (let n = 0; n < input.length; n++) { if (!ArrayPrototypeIncludes(kForgivingBase64AllowedChars, StringPrototypeCharCodeAt(input, n))) diff --git a/test/parallel/test-btoa-atob.js b/test/parallel/test-btoa-atob.js index 64f53671030ba0..3a9786f7a62111 100644 --- a/test/parallel/test-btoa-atob.js +++ b/test/parallel/test-btoa-atob.js @@ -15,3 +15,12 @@ throws(() => buffer.btoa(), /TypeError/); strictEqual(atob(' '), ''); strictEqual(atob(' YW\tJ\njZA=\r= '), 'abcd'); + +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/);