| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent db151e1 commit f700074
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,7 @@ const { | |||
| 26 | 26 | ArrayFrom, | |
| 27 | 27 | ArrayIsArray, | |
| 28 | 28 | ArrayPrototypeForEach, | |
| 29 | - ArrayPrototypeIncludes, | ||
| 29 | + ArrayPrototypeIndexOf, | ||
| 30 | 30 | MathFloor, | |
| 31 | 31 | MathMin, | |
| 32 | 32 | MathTrunc, | |
@@ -1265,12 +1265,31 @@ function atob(input) { | |||
| 1265 | 1265 | if (arguments.length === 0) { | |
| 1266 | 1266 | throw new ERR_MISSING_ARGS('input'); | |
| 1267 | 1267 | } | |
| 1268 | + | ||
| 1268 | 1269 | input = `${input}`; | |
| 1270 | + let nonAsciiWhitespaceCharCount = 0; | ||
| 1271 | + | ||
| 1269 | 1272 | for (let n = 0; n < input.length; n++) { | |
| 1270 | - if (!ArrayPrototypeIncludes(kForgivingBase64AllowedChars, | ||
| 1271 | - StringPrototypeCharCodeAt(input, n))) | ||
| 1273 | + const index = ArrayPrototypeIndexOf( | ||
| 1274 | + kForgivingBase64AllowedChars, | ||
| 1275 | + StringPrototypeCharCodeAt(input, n)); | ||
| 1276 | + | ||
| 1277 | + if (index > 4) { | ||
| 1278 | + // The first 5 elements of `kForgivingBase64AllowedChars` are | ||
| 1279 | + // ASCII whitespace char codes. | ||
| 1280 | + nonAsciiWhitespaceCharCount++; | ||
| 1281 | + } else if (index === -1) { | ||
| 1272 | 1282 | throw lazyDOMException('Invalid character', 'InvalidCharacterError'); | |
| 1283 | + } | ||
| 1273 | 1284 | } | |
| 1285 | + | ||
| 1286 | + // See #3 - https://infra.spec.whatwg.org/#forgiving-base64 | ||
| 1287 | + if (nonAsciiWhitespaceCharCount % 4 === 1) { | ||
| 1288 | + throw lazyDOMException( | ||
| 1289 | + 'The string to be decoded is not correctly encoded.', | ||
| 1290 | + 'InvalidCharacterError'); | ||
| 1291 | + } | ||
| 1292 | + | ||
| 1274 | 1293 | return Buffer.from(input, 'base64').toString('latin1'); | |
| 1275 | 1294 | } | |
| 1276 | 1295 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,14 @@ | |||
| 1 | + // Flags: --expose-internals | ||
| 1 | 2 | 'use strict'; | |
| 2 | 3 | ||
| 3 | 4 | require('../common'); | |
| 4 | 5 | ||
| 5 | 6 | const { strictEqual, throws } = require('assert'); | |
| 6 | 7 | const buffer = require('buffer'); | |
| 7 | 8 | ||
| 9 | + const { internalBinding } = require('internal/test/binding'); | ||
| 10 | + const { DOMException } = internalBinding('messaging'); | ||
| 11 | + | ||
| 8 | 12 | // Exported on the global object | |
| 9 | 13 | strictEqual(globalThis.atob, buffer.atob); | |
| 10 | 14 | strictEqual(globalThis.btoa, buffer.btoa); | |
@@ -14,4 +18,26 @@ throws(() => buffer.atob(), /TypeError/); | |||
| 14 | 18 | throws(() => buffer.btoa(), /TypeError/); | |
| 15 | 19 | ||
| 16 | 20 | strictEqual(atob(' '), ''); | |
| 17 | - strictEqual(atob(' YW\tJ\njZA=\r= '), 'abcd'); | ||
| 21 | + strictEqual(atob(' Y\fW\tJ\njZ A=\r= '), 'abcd'); | ||
| 22 | + | ||
| 23 | + strictEqual(atob(null), '\x9Eée'); | ||
| 24 | + strictEqual(atob(NaN), '5£'); | ||
| 25 | + strictEqual(atob(Infinity), '"wâ\x9E+r'); | ||
| 26 | + strictEqual(atob(true), '¶»\x9E'); | ||
| 27 | + strictEqual(atob(1234), '×mø'); | ||
| 28 | + strictEqual(atob([]), ''); | ||
| 29 | + strictEqual(atob({ toString: () => '' }), ''); | ||
| 30 | + strictEqual(atob({ [Symbol.toPrimitive]: () => '' }), ''); | ||
| 31 | + | ||
| 32 | + throws(() => atob(Symbol()), /TypeError/); | ||
| 33 | + [ | ||
| 34 | + undefined, false, () => {}, {}, [1], | ||
| 35 | + 0, 1, 0n, 1n, -Infinity, | ||
| 36 | + 'a', 'a\n\n\n', '\ra\r\r', ' a ', '\t\t\ta', 'a\f\f\f', '\ta\r \n\f', | ||
| 37 | + ].forEach((value) => | ||
| 38 | + // See #2 - https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob | ||
| 39 | + throws(() => atob(value), { | ||
| 40 | + constructor: DOMException, | ||
| 41 | + name: 'InvalidCharacterError', | ||
| 42 | + code: 5, | ||
| 43 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments