Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
60 changes: 55 additions & 5 deletions src/lib/isJWT.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
import assertString from './util/assertString';
import isBase64 from './isBase64';

/* istanbul ignore next */
function decodeBase64Url(b64) {
if (typeof Buffer !== 'undefined') {
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

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

/* istanbul ignore next */ on decodeBase64Url() will exclude the entire helper from coverage, even though it’s now part of isJWT()’s core behavior. Prefer removing this ignore, or scoping ignores to the genuinely untestable branches (e.g., the atob path) so Node-based tests still cover the main decoding logic.

Copilot uses AI. Check for mistakes.
if (typeof Buffer.from === 'function') {
return Buffer.from(b64, 'base64').toString('utf8');
}
// eslint-disable-next-line no-buffer-constructor
return new Buffer(b64, 'base64').toString('utf8');
}
if (typeof atob === 'function') {
const binary = atob(b64);
if (typeof TextDecoder !== 'undefined') {
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i += 1) {
bytes[i] = binary.charCodeAt(i);
}
return new TextDecoder('utf-8').decode(bytes);
}
let encoded = '';
for (let i = 0; i < binary.length; i += 1) {
const code = binary.charCodeAt(i).toString(16).padStart(2, '0');
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

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

decodeBase64Url() uses String.prototype.padStart(). This built-in isn’t available in some older JS runtimes, and this repo targets very old Node versions and produces a browser bundle without guaranteed polyfills. Consider replacing the padStart(2, '0') usage with a small manual 2-digit hex padding implementation to avoid relying on padStart.

Suggested change
const code = binary.charCodeAt(i).toString(16).padStart(2, '0');
const hex = binary.charCodeAt(i).toString(16);
const code = hex.length === 1 ? `0${hex}` : hex;

Copilot uses AI. Check for mistakes.
encoded += `%${code}`;
}
return decodeURIComponent(encoded);
}
return b64;
}

function tryDecodeJSON(segment) {
if (!isBase64(segment, { urlSafe: true })) return false;
try {
// Normalize base64url alphabet to base64, then restore stripped padding
let b64 = segment.replace(/-/g, '+').replace(/_/g, '/');
while (b64.length % 4) b64 += '=';
const decoded = decodeBase64Url(b64);
const parsed = JSON.parse(decoded);
if (typeof parsed !== 'object') return false;
if (parsed === null) return false;
if (Array.isArray(parsed)) return false;
return true;
} catch (e) {
return false;
}
}

export default function isJWT(str) {
assertString(str);

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

if (len !== 3) {
return false;
}
if (dotSplit.length !== 3) return false;

const header = dotSplit[0];
const payload = dotSplit[1];
const signature = dotSplit[2];

if (!tryDecodeJSON(header)) return false;
if (!tryDecodeJSON(payload)) return false;
if (!isBase64(signature, { urlSafe: true })) return false;

return dotSplit.reduce((acc, currElem) => acc && isBase64(currElem, { urlSafe: true }), true);
return true;
}
14 changes: 14 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5542,13 +5542,27 @@ describe('Validators', () => {
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3JlbSI6Imlwc3VtIn0.ymiJSsMJXR6tMSr8G9usjQ15_8hKPDv_CArLhxw28MI',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2xvciI6InNpdCIsImFtZXQiOlsibG9yZW0iLCJpcHN1bSJdfQ.rRpe04zbWbbJjwM43VnHzAboDzszJtGrNsUxaqQ-GQ8',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSwiamFrZSI6eyJhZ2UiOjMwLCJoZWlnaHQiOjI3MH19.YRLPARDmhGMC3BBk_OhtwwK21PIkVCqQe8ncIRPKo-E',
'eyJhbGciOiJub25lIn0.eyJpc3MiOiJqb2UiLCJleHAiOjEzMDA4MTkzODAsImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.',
],
invalid: [
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTUxNjIzOTAyMn0',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTYxNjY1Mzg3Mn0.eyJpc3MiOiJodHRwczovL2V4YW1wbGUuY29tIiwiaWF0IjoxNjE2NjUzODcyLCJleHAiOjE2MTY2NTM4ODJ9.a1jLRQkO5TV5y5ERcaPAiM9Xm2gBdRjKrrCpHkGr_8M',
'$Zs.ewu.su84',
'ks64$S/9.dy$§kz.3sd73b',
'foo.bar.',
'..',
'.t.',
'foo.bar.baz',
'Zm9v.YmFy.',
Comment on lines +5553 to +5557
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

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

The PR description mentions adding a test for an unsecured JWT with an empty signature (alg: none), but this test block only adds additional invalid cases. Consider adding an explicit valid token where the header sets alg to none and the signature segment is empty (trailing dot) to ensure the intended behavior is actually covered.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

'eyJmb28iOiJiYXIifQ.YmFy.',
'Zm9v.eyJiYXIiOiJiYXoifQ.',
'W10=.eyJiYXIiOiJiYXoifQ.',
'eyJmb28iOiJiYXIifQ.W10=.',
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

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

Some of the newly added invalid JWT fixtures include = padding in header/payload segments (e.g., W10=). Since isBase64(..., { urlSafe: true }) rejects =, these cases fail before exercising the new base64url normalization + JSON parsing logic. If the intent is to test “non-object decoded values”, use the unpadded base64url forms (e.g., W10 for []) so the decode/parse path is actually covered.

Suggested change
'W10=.eyJiYXIiOiJiYXoifQ.',
'eyJmb28iOiJiYXIifQ.W10=.',
'W10.eyJiYXIiOiJiYXoifQ.',
'eyJmb28iOiJiYXIifQ.W10.',

Copilot uses AI. Check for mistakes.
'bnVsbA.eyJiYXIiOiJiYXoifQ.',
'WzFd.eyJiYXIiOiJiYXoifQ.',
'ImhlbGxvIg.eyJiYXIiOiJiYXoifQ.',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.invalid$sig',
],
error: [
[],
Expand Down
Loading