From ef9d1c09808cdc6cd42b5e96c676930231de919b Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 13:32:55 +0530 Subject: [PATCH 1/5] Merge conflicts resolved --- es/lib/isJWT.js | 15 +++++++++++++-- lib/isJWT.js | 17 ++++++++++++++--- src/lib/isJWT.js | 13 ++++++++++--- test/validators.js | 7 +++++++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/es/lib/isJWT.js b/es/lib/isJWT.js index 41a6ece0d..c1afb7b6a 100644 --- a/es/lib/isJWT.js +++ b/es/lib/isJWT.js @@ -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); } \ No newline at end of file diff --git a/lib/isJWT.js b/lib/isJWT.js index 9dfb7cfb7..65d89fc48 100644 --- a/lib/isJWT.js +++ b/lib/isJWT.js @@ -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; diff --git a/src/lib/isJWT.js b/src/lib/isJWT.js index 42c5ee090..1a8896f98 100644 --- a/src/lib/isJWT.js +++ b/src/lib/isJWT.js @@ -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); } diff --git a/test/validators.js b/test/validators.js index 935049729..398abb971 100755 --- a/test/validators.js +++ b/test/validators.js @@ -3436,6 +3436,12 @@ describe('Validators', () => { '$Zs.ewu.su84', 'ks64$S/9.dy$§kz.3sd73b', ], + error: [ + [], + {}, + null, + undefined, + ], }); }); @@ -8229,6 +8235,7 @@ describe('Validators', () => { '\rAA', '\nAA', '123=', + '', 'This+isa/bad+base64Url==', '0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw', ], From 3f8acfda46f7a7c53eab04429e4d606d3fbbe362 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 13:58:23 +0530 Subject: [PATCH 2/5] Merge branch 'patch-jwt' of https://github.com/parasg1999/validator.js into patch-jwt --- es/lib/isBase64.js | 14 +++++++++----- lib/isBase64.js | 15 ++++++++++----- src/lib/isBase64.js | 14 +++++++++++--- test/validators.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/es/lib/isBase64.js b/es/lib/isBase64.js index b54d0b9ec..ef05c5bc0 100644 --- a/es/lib/isBase64.js +++ b/es/lib/isBase64.js @@ -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 = { + 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; } diff --git a/lib/isBase64.js b/lib/isBase64.js index e42da06c4..fc74c7ea3 100644 --- a/lib/isBase64.js +++ b/lib/isBase64.js @@ -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; } diff --git a/src/lib/isBase64.js b/src/lib/isBase64.js index 523ee1111..c0898fcf9 100644 --- a/src/lib/isBase64.js +++ b/src/lib/isBase64.js @@ -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 || diff --git a/test/validators.js b/test/validators.js index 398abb971..56dcf0c70 100755 --- a/test/validators.js +++ b/test/validators.js @@ -4545,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'); From 48b3e4b720268055e4f3f838456cc5b3e0b2f0c7 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 16:59:18 +0530 Subject: [PATCH 3/5] Pascal to camelcase --- src/lib/isBase64.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/isBase64.js b/src/lib/isBase64.js index c0898fcf9..a8fd3b2e9 100644 --- a/src/lib/isBase64.js +++ b/src/lib/isBase64.js @@ -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 default_base64_options = { urlSafe: false, @@ -14,7 +14,7 @@ export default function isBase64(str, options) { const len = str.length; if (options.urlSafe) { - return UrlSafeBase64.test(str); + return urlSafeBase64.test(str); } if (!len || len % 4 !== 0 || notBase64.test(str)) { From f24bdcbc57d6c7c3700f8c0613d89265594604ff Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 17:01:05 +0530 Subject: [PATCH 4/5] update base64 files --- es/lib/isBase64.js | 4 ++-- lib/isBase64.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/es/lib/isBase64.js b/es/lib/isBase64.js index ef05c5bc0..bf93f3f34 100644 --- a/es/lib/isBase64.js +++ b/es/lib/isBase64.js @@ -1,7 +1,7 @@ import assertString from './util/assertString'; import merge from './util/merge'; var notBase64 = /[^A-Z0-9+\/=]/i; -var UrlSafeBase64 = /^[A-Z0-9_\-]+$/i; +var urlSafeBase64 = /^[A-Z0-9_\-]+$/i; var default_base64_options = { urlSafe: false }; @@ -11,7 +11,7 @@ export default function isBase64(str, options) { var len = str.length; if (options.urlSafe) { - return UrlSafeBase64.test(str); + return urlSafeBase64.test(str); } if (!len || len % 4 !== 0 || notBase64.test(str)) { diff --git a/lib/isBase64.js b/lib/isBase64.js index fc74c7ea3..19e7c07bd 100644 --- a/lib/isBase64.js +++ b/lib/isBase64.js @@ -12,7 +12,7 @@ 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 urlSafeBase64 = /^[A-Z0-9_\-]+$/i; var default_base64_options = { urlSafe: false }; @@ -23,7 +23,7 @@ function isBase64(str, options) { var len = str.length; if (options.urlSafe) { - return UrlSafeBase64.test(str); + return urlSafeBase64.test(str); } if (!len || len % 4 !== 0 || notBase64.test(str)) { From cad964247d55e004c40fea884151aa69f0e966d4 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 20:48:39 +0530 Subject: [PATCH 5/5] Fixed camelcase for consistency --- es/lib/isBase64.js | 4 ++-- lib/isBase64.js | 4 ++-- src/lib/isBase64.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/es/lib/isBase64.js b/es/lib/isBase64.js index bf93f3f34..6d4e560ec 100644 --- a/es/lib/isBase64.js +++ b/es/lib/isBase64.js @@ -2,12 +2,12 @@ import assertString from './util/assertString'; import merge from './util/merge'; var notBase64 = /[^A-Z0-9+\/=]/i; var urlSafeBase64 = /^[A-Z0-9_\-]+$/i; -var default_base64_options = { +var defaultBase64Options = { urlSafe: false }; export default function isBase64(str, options) { assertString(str); - options = merge(options, default_base64_options); + options = merge(options, defaultBase64Options); var len = str.length; if (options.urlSafe) { diff --git a/lib/isBase64.js b/lib/isBase64.js index 19e7c07bd..444b0ec3f 100644 --- a/lib/isBase64.js +++ b/lib/isBase64.js @@ -13,13 +13,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de var notBase64 = /[^A-Z0-9+\/=]/i; var urlSafeBase64 = /^[A-Z0-9_\-]+$/i; -var default_base64_options = { +var defaultBase64Options = { urlSafe: false }; function isBase64(str, options) { (0, _assertString.default)(str); - options = (0, _merge.default)(options, default_base64_options); + options = (0, _merge.default)(options, defaultBase64Options); var len = str.length; if (options.urlSafe) { diff --git a/src/lib/isBase64.js b/src/lib/isBase64.js index a8fd3b2e9..6bfe0310b 100644 --- a/src/lib/isBase64.js +++ b/src/lib/isBase64.js @@ -4,13 +4,13 @@ import merge from './util/merge'; const notBase64 = /[^A-Z0-9+\/=]/i; const urlSafeBase64 = /^[A-Z0-9_\-]+$/i; -const default_base64_options = { +const defaultBase64Options = { urlSafe: false, }; export default function isBase64(str, options) { assertString(str); - options = merge(options, default_base64_options); + options = merge(options, defaultBase64Options); const len = str.length; if (options.urlSafe) {