From 5c2913cd0fcffc117d21e86a5e48a66b98d53e53 Mon Sep 17 00:00:00 2001 From: coti-z Date: Sun, 28 Dec 2025 16:53:45 +0900 Subject: [PATCH] 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 --- src/lib/isBase64.js | 3 ++- test/validators/isBase64.test.js | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/isBase64.js b/src/lib/isBase64.js index fd876e4c0..67e734e57 100644 --- a/src/lib/isBase64.js +++ b/src/lib/isBase64.js @@ -12,6 +12,7 @@ export default function isBase64(str, options) { if (str === '') return true; + if (str.length % 4 === 1) return false; if (options.padding && str.length % 4 !== 0) return false; let regex; @@ -21,5 +22,5 @@ export default function isBase64(str, options) { regex = options.padding ? base64WithPadding : base64WithoutPadding; } - return (!options.padding || str.length % 4 === 0) && regex.test(str); + return regex.test(str); } diff --git a/test/validators/isBase64.test.js b/test/validators/isBase64.test.js index c0074343a..9bf30c5ae 100644 --- a/test/validators/isBase64.test.js +++ b/test/validators/isBase64.test.js @@ -26,6 +26,9 @@ describe('isBase64', () => { 'HQIDAQAB', ], invalid: [ + 'A', + 'AAAAA', + 'AAAAAAAAA', '12345', 'Vml2YW11cyBmZXJtZtesting123', 'Zg=',