fix: properly validate JWT signatures in verifyJwt#762
Open
adityabhatkar23 wants to merge 1 commit into
Open
Conversation
Ensure tampered signatures are rejected and return null as expected. Add explicit length checks and base64url error handling. Fixes: LabsCrypt#761
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.
Description
This PR fixes a critical security vulnerability in the JWT verification function where tampered signatures were being accepted as valid tokens. The
verifyJwtfunction now properly validates JWT signatures and returnsnullfor any verification failure.Type of Change
Related Issues
Fixes #761
Changes Made
timingSafeEqualto prevent timing attacks and buffer length mismatchespayload.expto handle missing or invalid expiration claimsnullThe
verifyJwtfunction now properly validates:Testing
Test Coverage
Test Steps
pnpm test -- tests/auth-jwt.test.tsBreaking Changes
None. This is a bug fix that strengthens security without changing the public API or expected behavior for valid tokens.
Screenshots/Demo
N/A - Backend security fix
Checklist
Additional Notes
This fix addresses a critical security vulnerability where the JWT signature verification was not properly enforcing signature validation. The implementation now uses
crypto.timingSafeEqual()with proper length checking to prevent timing attacks and malformed tokens from being accepted. The fix ensures that any tampering with the JWT (header, payload, or signature) will result in verification failure and returnnullas expected.