Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throwError does not throw on invalid signature #80

Open
stephent opened this issue Jun 9, 2024 · 5 comments
Open

throwError does not throw on invalid signature #80

stephent opened this issue Jun 9, 2024 · 5 comments

Comments

@stephent
Copy link

stephent commented Jun 9, 2024

The readme states:

throws

If options.throwError is true and the token is invalid, an error will be thrown.

But the verify method does not throw if the following line returns false, even if throwError is true:

return await crypto.subtle.verify(algorithm, key, base64UrlToArrayBuffer(tokenParts[2]), textToArrayBuffer(`${tokenParts[0]}.${tokenParts[1]}`))

This could result in invalid JWTs being mistakenly accepted, if the caller assumes they can simply try/catch with throwError passed as true and don't also check the return value.

@stephent stephent changed the title throwsError does not throw on invalid signature throwError does not throw on invalid signature Jun 9, 2024
@stephent
Copy link
Author

stephent commented Jun 9, 2024

See also #76 - the code shown there appears to make exactly this incorrect assumption.

@chamini2
Copy link

what about

if (options.throwError)
throw err
return false

@stephent
Copy link
Author

stephent commented Sep 27, 2024

@chamini2 what about it? That doesn't change anything about the reported issue...

@chamini2
Copy link

Can you share an example of this that should have failed

@stephent
Copy link
Author

A JWT with an invalid signature will simply return false and won't throw even if throwError is set to true. crypto.subtle.verify doesn't always throw, per the docs (https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/verify#return_value).

I think it would be fine either throw an error if throwError is true and verify returns false`, or alternatively to update the documentation along the lines of

If options.throwError is true, any errors encountered while validating the token will be rethrown, however callers must always check the method return value to confirm token validity, as an invalid token does not necessarily result in a thrown error.

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

No branches or pull requests

2 participants