fix(isJWT): validate header and payload decode to valid JSON objects#2689
Open
abhu85 wants to merge 2 commits intovalidatorjs:masterfrom
Open
fix(isJWT): validate header and payload decode to valid JSON objects#2689abhu85 wants to merge 2 commits intovalidatorjs:masterfrom
abhu85 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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