@@ -1232,12 +1232,14 @@ function btoa(input) {
1232
1232
return buf . toString ( 'base64' ) ;
1233
1233
}
1234
1234
1235
- // Refs: https://infra.spec.whatwg.org/#forgiving-base64-decode
1236
- const kForgivingBase64AllowedChars = [
1235
+ const asciiWhitespaceCharacters = [
1237
1236
// ASCII whitespace
1238
1237
// Refs: https://infra.spec.whatwg.org/#ascii-whitespace
1239
1238
0x09 , 0x0A , 0x0C , 0x0D , 0x20 ,
1239
+ ] ;
1240
1240
1241
+ // Refs: https://infra.spec.whatwg.org/#forgiving-base64-decode
1242
+ const kForgivingBase64AllowedChars = [
1241
1243
// Uppercase letters
1242
1244
...ArrayFrom ( { length : 26 } , ( _ , i ) => StringPrototypeCharCodeAt ( 'A' ) + i ) ,
1243
1245
@@ -1260,32 +1262,29 @@ function atob(input) {
1260
1262
throw new ERR_MISSING_ARGS ( 'input' ) ;
1261
1263
}
1262
1264
1263
- if ( input === undefined || input === false || typeof input === 'number' ) {
1264
- throw lazyDOMException (
1265
- 'The string to be decoded is not correctly encoded.' ,
1266
- 'ValidationError' ) ;
1267
- }
1265
+ input = `${ input } ` ;
1266
+ let nonAsciiWhitespaceCharCount = 0 ;
1268
1267
1269
- // Remove all ASCII whitespace from data.
1270
- //
1271
- // See #1 - https://infra.spec.whatwg.org/#forgiving-base64
1272
- input = `${ input } ` . replace ( / \s / g, '' ) ;
1268
+ for ( let n = 0 ; n < input . length ; n ++ ) {
1269
+ const char = StringPrototypeCharCodeAt ( input , n ) ;
1270
+
1271
+ if ( ArrayPrototypeIncludes ( kForgivingBase64AllowedChars , char ) ) {
1272
+ nonAsciiWhitespaceCharCount ++ ;
1273
+ } else if ( ! ArrayPrototypeIncludes ( asciiWhitespaceCharacters , char ) ) {
1274
+ throw lazyDOMException ( 'Invalid character' , 'InvalidCharacterError' ) ;
1275
+ }
1276
+ }
1273
1277
1274
1278
// If data's code point length divides by 4 leaving a remainder of 1, then
1275
1279
// return failure.
1276
1280
//
1277
1281
// See #3 - https://infra.spec.whatwg.org/#forgiving-base64
1278
- if ( input . length % 4 === 1 ) {
1282
+ if ( nonAsciiWhitespaceCharCount % 4 === 1 ) {
1279
1283
throw lazyDOMException (
1280
1284
'The string to be decoded is not correctly encoded.' ,
1281
1285
'ValidationError' ) ;
1282
1286
}
1283
1287
1284
- for ( let n = 0 ; n < input . length ; n ++ ) {
1285
- if ( ! ArrayPrototypeIncludes ( kForgivingBase64AllowedChars ,
1286
- StringPrototypeCharCodeAt ( input , n ) ) )
1287
- throw lazyDOMException ( 'Invalid character' , 'InvalidCharacterError' ) ;
1288
- }
1289
1288
return Buffer . from ( input , 'base64' ) . toString ( 'latin1' ) ;
1290
1289
}
1291
1290
0 commit comments