Skip to content

Commit

Permalink
fix isBase64 and isBase32 seeing empty string as invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Grosse-Holz committed Aug 20, 2020
1 parent db0a402 commit a48221a
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 @@ -4733,6 +4733,7 @@ describe('Validators', () => {
test({
validator: 'isBase64',
valid: [
'',
'Zg==',
'Zm8=',
'Zm9v',
Expand All @@ -4752,7 +4753,6 @@ describe('Validators', () => {
],
invalid: [
'12345',
'',
'Vml2YW11cyBmZXJtZtesting123',
'Zg=',
'Z===',
Expand All @@ -4766,6 +4766,7 @@ describe('Validators', () => {
validator: 'isBase64',
args: [{ urlSafe: true }],
valid: [
'',
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
'1234',
'bXVtLW5ldmVyLXByb3Vk',
Expand All @@ -4776,7 +4777,6 @@ describe('Validators', () => {
' AA',
'\tAA',
'\rAA',
'',
'\nAA',
'This+isa/bad+base64Url==',
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
Expand Down Expand Up @@ -8717,6 +8717,7 @@ describe('Validators', () => {
validator: 'isBase64',
args: [{ urlSafe: true }],
valid: [
'',
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
'1234',
'bXVtLW5ldmVyLXByb3Vk',
Expand All @@ -8729,7 +8730,6 @@ describe('Validators', () => {
'\rAA',
'\nAA',
'123=',
'',
'This+isa/bad+base64Url==',
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
],
Expand Down

0 comments on commit a48221a

Please sign in to comment.