fix(isJWT): validate header and payload decode to valid JSON objects#2689
fix(isJWT): validate header and payload decode to valid JSON objects#2689abhu85 wants to merge 2 commits intovalidatorjs:masterfrom
Conversation
…ects (validatorjs#2511) The isJWT validator now properly validates that the header (first part) and payload (second part) of a JWT decode to valid JSON objects, not just valid Base64 strings. Per RFC 7519, a JWT consists of three Base64URL-encoded parts: - Header: MUST be a JSON object containing at least "alg" - Payload: MUST be a JSON object (the claims) - Signature: Binary data (not required to be JSON) Previously, strings like "foo.bar.baz" or ".babelrc.cjs" would incorrectly return true because they matched the Base64 pattern, even though decoding them does not produce valid JSON. Fixes validatorjs#2511 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2689 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2590 2612 +22
Branches 659 664 +5
=========================================
+ Hits 2590 2612 +22 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Adds tests for 100% coverage of the new isValidJSONObject function: - Edge cases for JSON types that aren't objects (arrays, null, primitives) - Buffer.from() fallback path (when atob is unavailable) - Fallback return false path (when neither atob nor Buffer available) Fixes validatorjs#2511 coverage requirements. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR tightens isJWT validation to ensure JWT header and payload are not only Base64URL-encoded, but also decode into valid JSON objects, addressing false positives reported in #2511.
Changes:
- Add Base64URL decode + JSON-object parsing validation for JWT header and payload in
isJWT. - Extend
isJWTinvalid-case test coverage to include Base64-valid but non-JSON / non-object JSON decoded segments. - Add tests for decoder fallback behavior when
atoband/orBufferare unavailable.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/lib/isJWT.js |
Adds decoded JSON-object validation for JWT header/payload after Base64URL checks. |
test/validators.test.js |
Adds regression tests for false positives and decoder-fallback paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (typeof atob === 'function') { | ||
| decoded = atob(base64); | ||
| } else if (typeof Buffer !== 'undefined') { | ||
| decoded = Buffer.from(base64, 'base64').toString('utf8'); |
There was a problem hiding this comment.
isValidJSONObject uses Buffer.from(...), but this library declares support for Node >=0.10 (where Buffer.from does not exist). In those runtimes typeof Buffer !== 'undefined' will be true, then Buffer.from throws, and valid JWTs will be rejected. Consider feature-detecting Buffer.from and falling back to new Buffer(base64, 'base64')/Buffer(base64, 'base64') when Buffer.from is unavailable (while keeping the browser atob path).
| decoded = Buffer.from(base64, 'base64').toString('utf8'); | |
| if (typeof Buffer.from === 'function') { | |
| decoded = Buffer.from(base64, 'base64').toString('utf8'); | |
| } else { | |
| // Fallback for older Node.js versions (e.g., Node 0.10) where Buffer.from is unavailable | |
| decoded = new Buffer(base64, 'base64').toString('utf8'); | |
| } |
Summary
isJWTto properly validate that JWT header and payload decode to valid JSON objectsfoo.bar.baz,.babelrc.cjs)Problem
The current
isJWTvalidator only checks if the three dot-separated parts are valid Base64, but doesn't verify that the decoded header and payload are valid JSON objects as required by RFC 7519.This causes false positives:
Solution
Per RFC 7519, a JWT consists of:
alg,typ, etc.)The fix adds JSON parsing validation for the header and payload parts after Base64 decoding, ensuring they are valid JSON objects (not arrays, strings, or other types).
Test Plan
foo.bar.baz,.babelrc.cjs,..,.t.)Compatibility
Fixes #2511