fix: wrap crypto TypeError in JsonWebTokenError for malformed EC/RSA signatures#1018
Open
Vansh1811 wants to merge 1 commit intoauth0:masterfrom
Open
fix: wrap crypto TypeError in JsonWebTokenError for malformed EC/RSA signatures#1018Vansh1811 wants to merge 1 commit intoauth0:masterfrom
Vansh1811 wants to merge 1 commit intoauth0:masterfrom
Conversation
When verifying a JWT with EC or RSA algorithms, malformed signatures
could cause jws.verify() to throw a raw TypeError from the underlying
Node.js crypto module (e.g. 'ES512 signatures must be 132 bytes, saw 131')
instead of a JsonWebTokenError.
This TypeError was passed directly to done(e) and would surface to
callers as an undocumented error type, breaking any code that catches
JsonWebTokenError to handle invalid-signature scenarios.
Fix: in the jws.verify() catch block, check if the thrown error is
already a JsonWebTokenError; if not, wrap it in
new JsonWebTokenError('invalid signature') so callers always receive
the documented, consistent error type.
Fixes auth0#767
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.
Problem
When verifying a JWT signed with an EC (
ES256,ES384,ES512) or RSA algorithm and the signature is malformed (e.g., truncated by one byte),jws.verify()can throw a rawTypeErrorfrom the underlying Node.js crypto module:This
TypeErroris not aJsonWebTokenErrorand is passed directly todone(e). As a result:TypeErrorinstead of the expectedJsonWebTokenErrorJsonWebTokenErrorto classify errors (e.g., returning401 Unauthorized) will not catch this errorJsonWebTokenError('invalid signature')Reproduction
Fix
In the
jws.verify()catch block inverify.js, check if the thrown error is already aJsonWebTokenError. If it is not, wrap it innew JsonWebTokenError('invalid signature')before passing todone().This ensures:
JsonWebTokenError'invalid signature'is consistent with valid-but-wrong-signature failuresJsonWebTokenErrors thrown byjwsis preservedChanges
verify.js: 3 lines changed in thejws.verify()catch blockFixes #767