Added alphabet check before decoding

This commit is contained in:
Michael Theos 2021-03-04 16:08:29 +11:00
parent 755cfea789
commit 47351c126d
No known key found for this signature in database
GPG Key ID: 8C7A7E25FD369063
2 changed files with 14 additions and 0 deletions

View File

@ -1,6 +1,11 @@
"use strict"; "use strict";
const b64Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
function atob(str) { function atob(str) {
for (const ch of str)
if (!b64Alphabet.includes(ch))
throw new Error(`Invalid character '${ch}' in base64 String`);
return Buffer.from(str, 'base64').toString('binary'); return Buffer.from(str, 'base64').toString('binary');
} }

View File

@ -4,6 +4,7 @@
var atob = require('.'); var atob = require('.');
var encoded = "SGVsbG8sIFdvcmxkIQ==" var encoded = "SGVsbG8sIFdvcmxkIQ=="
var unencoded = "Hello, World!"; var unencoded = "Hello, World!";
var malformed = "SGVsbG8s{"
/* /*
, encoded = "SGVsbG8sIBZM" , encoded = "SGVsbG8sIBZM"
, unencoded = "Hello, 世界" , unencoded = "Hello, 世界"
@ -14,5 +15,13 @@
return; return;
} }
try {
const decoded = atob(malformed)
console.log('[FAIL]', malformed, decoded);
return;
} catch (_) {
/* pass */
}
console.log('[PASS] all tests pass'); console.log('[PASS] all tests pass');
}()); }());