Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified isJWT to use urlsafe base64 (also added) #1316

Merged
merged 5 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions es/lib/isBase64.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import assertString from './util/assertString';
import merge from './util/merge';
var notBase64 = /[^A-Z0-9+\/=]/i;
export default function isBase64(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var urlSafeBase64 = /^[A-Z0-9_\-]+$/i;
var default_base64_options = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: for the camelCase consistency...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has been fixed now! @profnandaa

urlSafe: false
};
export default function isBase64(str, options) {
assertString(str);
options = merge(options, default_base64_options);
var len = str.length;

if (options.urlSafe) {
return /^[A-Za-z0-9_-]+$/.test(str);
return urlSafeBase64.test(str);
}

var len = str.length;

if (!len || len % 4 !== 0 || notBase64.test(str)) {
return false;
}
Expand Down
15 changes: 13 additions & 2 deletions es/lib/isJWT.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import assertString from './util/assertString';
var jwt = /^([A-Za-z0-9\-_~+\/]+[=]{0,2})\.([A-Za-z0-9\-_~+\/]+[=]{0,2})(?:\.([A-Za-z0-9\-_~+\/]+[=]{0,2}))?$/;
import isBase64 from './isBase64';
export default function isJWT(str) {
assertString(str);
return jwt.test(str);
var dotSplit = str.split('.');
var len = dotSplit.length;

if (len > 3 || len < 2) {
return false;
}

return dotSplit.reduce(function (acc, currElem) {
return acc && isBase64(currElem, {
urlSafe: true
});
}, true);
}
15 changes: 10 additions & 5 deletions lib/isBase64.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ exports.default = isBase64;

var _assertString = _interopRequireDefault(require("./util/assertString"));

var _merge = _interopRequireDefault(require("./util/merge"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var notBase64 = /[^A-Z0-9+\/=]/i;
var urlSafeBase64 = /^[A-Z0-9_\-]+$/i;
var default_base64_options = {
urlSafe: false
};

function isBase64(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
function isBase64(str, options) {
(0, _assertString.default)(str);
options = (0, _merge.default)(options, default_base64_options);
var len = str.length;

if (options.urlSafe) {
return /^[A-Za-z0-9_-]+$/.test(str);
return urlSafeBase64.test(str);
}

var len = str.length;

if (!len || len % 4 !== 0 || notBase64.test(str)) {
return false;
}
Expand Down
17 changes: 14 additions & 3 deletions lib/isJWT.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ exports.default = isJWT;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _isBase = _interopRequireDefault(require("./isBase64"));

var jwt = /^([A-Za-z0-9\-_~+\/]+[=]{0,2})\.([A-Za-z0-9\-_~+\/]+[=]{0,2})(?:\.([A-Za-z0-9\-_~+\/]+[=]{0,2}))?$/;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function isJWT(str) {
(0, _assertString.default)(str);
return jwt.test(str);
var dotSplit = str.split('.');
var len = dotSplit.length;

if (len > 3 || len < 2) {
return false;
}

return dotSplit.reduce(function (acc, currElem) {
return acc && (0, _isBase.default)(currElem, {
urlSafe: true
});
}, true);
}

module.exports = exports.default;
Expand Down
14 changes: 11 additions & 3 deletions src/lib/isBase64.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import assertString from './util/assertString';
import merge from './util/merge';

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

export default function isBase64(str, options = {}) {
const default_base64_options = {
urlSafe: false,
};

export default function isBase64(str, options) {
assertString(str);
options = merge(options, default_base64_options);
const len = str.length;

if (options.urlSafe) {
return /^[A-Za-z0-9_-]+$/.test(str);
return urlSafeBase64.test(str);
}

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

const firstPaddingChar = str.indexOf('=');
return firstPaddingChar === -1 ||
firstPaddingChar === len - 1 ||
Expand Down
13 changes: 10 additions & 3 deletions src/lib/isJWT.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import assertString from './util/assertString';

const jwt = /^([A-Za-z0-9\-_~+\/]+[=]{0,2})\.([A-Za-z0-9\-_~+\/]+[=]{0,2})(?:\.([A-Za-z0-9\-_~+\/]+[=]{0,2}))?$/;
import isBase64 from './isBase64';

export default function isJWT(str) {
assertString(str);
return jwt.test(str);

const dotSplit = str.split('.');
const len = dotSplit.length;

if (len > 3 || len < 2) {
return false;
}

return dotSplit.reduce((acc, currElem) => acc && isBase64(currElem, { urlSafe: true }), true);
}
36 changes: 36 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3436,6 +3436,12 @@ describe('Validators', () => {
'$Zs.ewu.su84',
'ks64$S/9.dy$§kz.3sd73b',
],
error: [
[],
{},
null,
undefined,
],
});
});

Expand Down Expand Up @@ -4539,6 +4545,35 @@ describe('Validators', () => {
'Zm9vYmFy====',
],
});

test({
validator: 'isBase64',
args: [{ urlSafe: true }],
valid: [
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
'1234',
'bXVtLW5ldmVyLXByb3Vk',
'PDw_Pz8-Pg',
'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw',
],
invalid: [
' AA',
'\tAA',
'\rAA',
'',
'\nAA',
'This+isa/bad+base64Url==',
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
],
error: [
null,
undefined,
{},
[],
42,
],
});

for (let i = 0, str = '', encoded; i < 1000; i++) {
str += String.fromCharCode(Math.random() * 26 | 97); // eslint-disable-line no-bitwise
encoded = Buffer.from(str).toString('base64');
Expand Down Expand Up @@ -8229,6 +8264,7 @@ describe('Validators', () => {
'\rAA',
'\nAA',
'123=',
'',
'This+isa/bad+base64Url==',
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
],
Expand Down