Skip to content

Commit e947d6f

Browse files
author
Kevin Lentin
committed
validatorjs#2491 validatorjs#2573 Simplify isBase64 to prevent stack overflow
1 parent 72573b3 commit e947d6f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/lib/isBase64.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assertString from './util/assertString';
22
import merge from './util/merge';
33

4-
const base64WithPadding = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/;
4+
const base64WithPadding = /^[A-Za-z0-9+/]+={0,2}$/;
55
const base64WithoutPadding = /^[A-Za-z0-9+/]+$/;
6-
const base64UrlWithPadding = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}==|[A-Za-z0-9_-]{3}=|[A-Za-z0-9_-]{4})$/;
6+
const base64UrlWithPadding = /^[A-Za-z0-9_-]+={0,2}$/;
77
const base64UrlWithoutPadding = /^[A-Za-z0-9_-]+$/;
88

99
export default function isBase64(str, options) {
@@ -12,6 +12,8 @@ export default function isBase64(str, options) {
1212

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

15+
if (options.padding && str.length % 4 !== 0) return false;
16+
1517
let regex;
1618
if (options.urlSafe) {
1719
regex = options.padding ? base64UrlWithPadding : base64UrlWithoutPadding;

0 commit comments

Comments
 (0)