Skip to content

Commit 5c2913c

Browse files
committed
fix(isBase64): reject invalid Base64 length
- Reject strings where length % 4 === 1 (impossible in valid Base64) - Simplify return statement by removing redundant condition - Add tests for invalid length cases
1 parent 784e52a commit 5c2913c

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/lib/isBase64.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function isBase64(str, options) {
1212

1313
if (str === '') return true;
1414

15+
if (str.length % 4 === 1) return false;
1516
if (options.padding && str.length % 4 !== 0) return false;
1617

1718
let regex;
@@ -21,5 +22,5 @@ export default function isBase64(str, options) {
2122
regex = options.padding ? base64WithPadding : base64WithoutPadding;
2223
}
2324

24-
return (!options.padding || str.length % 4 === 0) && regex.test(str);
25+
return regex.test(str);
2526
}

test/validators/isBase64.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ describe('isBase64', () => {
2626
'HQIDAQAB',
2727
],
2828
invalid: [
29+
'A',
30+
'AAAAA',
31+
'AAAAAAAAA',
2932
'12345',
3033
'Vml2YW11cyBmZXJtZtesting123',
3134
'Zg=',

0 commit comments

Comments
 (0)