Skip to content

Commit

Permalink
fix: isBase64 and isBase32 seeing empty string as invalid (#1419)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Grosse-Holz <[email protected]>
  • Loading branch information
AberDerBart and Jonas Grosse-Holz authored Sep 6, 2020
1 parent 0177658 commit 491d9c0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/isBase32.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const base32 = /^[A-Z2-7]+=*$/;
export default function isBase32(str) {
assertString(str);
const len = str.length;
if (len > 0 && len % 8 === 0 && base32.test(str)) {
if (len % 8 === 0 && base32.test(str)) {
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/isBase64.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assertString from './util/assertString';
import merge from './util/merge';

const notBase64 = /[^A-Z0-9+\/=]/i;
const urlSafeBase64 = /^[A-Z0-9_\-]+$/i;
const urlSafeBase64 = /^[A-Z0-9_\-]*$/i;

const defaultBase64Options = {
urlSafe: false,
Expand All @@ -17,7 +17,7 @@ export default function isBase64(str, options) {
return urlSafeBase64.test(str);
}

if (!len || len % 4 !== 0 || notBase64.test(str)) {
if (len % 4 !== 0 || notBase64.test(str)) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4735,6 +4735,7 @@ describe('Validators', () => {
test({
validator: 'isBase64',
valid: [
'',
'Zg==',
'Zm8=',
'Zm9v',
Expand All @@ -4754,7 +4755,6 @@ describe('Validators', () => {
],
invalid: [
'12345',
'',
'Vml2YW11cyBmZXJtZtesting123',
'Zg=',
'Z===',
Expand All @@ -4768,6 +4768,7 @@ describe('Validators', () => {
validator: 'isBase64',
args: [{ urlSafe: true }],
valid: [
'',
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
'1234',
'bXVtLW5ldmVyLXByb3Vk',
Expand All @@ -4778,7 +4779,6 @@ describe('Validators', () => {
' AA',
'\tAA',
'\rAA',
'',
'\nAA',
'This+isa/bad+base64Url==',
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
Expand Down Expand Up @@ -8742,6 +8742,7 @@ describe('Validators', () => {
validator: 'isBase64',
args: [{ urlSafe: true }],
valid: [
'',
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
'1234',
'bXVtLW5ldmVyLXByb3Vk',
Expand All @@ -8754,7 +8755,6 @@ describe('Validators', () => {
'\rAA',
'\nAA',
'123=',
'',
'This+isa/bad+base64Url==',
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
],
Expand Down

0 comments on commit 491d9c0

Please sign in to comment.