Skip to content

fix(isJWT): reject tokens with non-JSON header or payload#2723

Closed
xxiaoxiong wants to merge 6 commits into
validatorjs:masterfrom
xxiaoxiong:fix/isJWT-non-json-rejection-2511
Closed

fix(isJWT): reject tokens with non-JSON header or payload#2723
xxiaoxiong wants to merge 6 commits into
validatorjs:masterfrom
xxiaoxiong:fix/isJWT-non-json-rejection-2511

Conversation

@xxiaoxiong
Copy link
Copy Markdown

Description

The isJWT function currently only validates that a string has three dot-separated segments and that each segment is valid URL-safe base64. However, a JWT's header and payload must be valid JSON when decoded.

For example:

isJWT("foo.bar.dGVzdA"); // Currently true, but should be false

The string "foo" is valid base64 but is not valid JSON, meaning this is not a real JWT.

Changes

  • Decode the header and payload base64 segments and parse as JSON
  • Return false if either is not valid JSON

Related Issue

Fixes #2511

The isJWT function currently only validates that the JWT has three
dot-separated segments and that each segment is valid URL-safe base64.
However, it does not verify that the decoded header and payload are valid
JSON. This allows strings like `foo.bar.dGVzdA` to pass validation
even though they are not valid JWTs.

This fix decodes the base64-encoded header and payload and checks that
they parse as valid JSON before returning true.

Fixes #2511
@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

❌ Patch coverage is 73.68421% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.80%. Comparing base (7fdc788) to head (6ce349b).

Files with missing lines Patch % Lines
src/lib/isJWT.js 73.68% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##            master    #2723      +/-   ##
===========================================
- Coverage   100.00%   99.80%   -0.20%     
===========================================
  Files          114      114              
  Lines         2587     2605      +18     
  Branches       656      660       +4     
===========================================
+ Hits          2587     2600      +13     
- Misses           0        4       +4     
- Partials         0        1       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xxiaoxiong
Copy link
Copy Markdown
Author

Closing this PR as @abhu85's PR #2689 was submitted earlier and addresses the same issue (#2511). Their implementation is the original fix.

@xxiaoxiong xxiaoxiong closed this May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isJWT does not check whether the decoded sections are valid JSON

1 participant