Repository navigation
Conversation
A malformed PEM key passed to verify() fell back to being treated as an HMAC secret, which then failed with a misleading "invalid algorithm" error. Now, when the key material looks like PEM and createPublicKey() fails, verify() returns a JsonWebTokenError carrying the crypto error. Fixes auth0#882
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.
By submitting a PR to this repository, you agree to the terms within the Auth0 Code of Conduct. Please see the contributing guidelines for how to create and submit a high-quality PR for this repo.
Description
When
verify()receives a malformed PEM public key,createPublicKey()throws and the error is swallowed. The key material then falls back tocreateSecretKey(), so the allowed algorithms default toHS*and an RS256 token fails withinvalid algorithm. That message hides the real problem: the key could not be parsed.With this change, if the key material is a string or Buffer containing
-----BEGINandcreatePublicKey()fails,verify()returns:The original crypto error is available on
err.inner. HMAC secrets that do not look like PEM are unaffected and still go throughcreateSecretKey().No public API changes. The only behavioral change is the error returned for malformed PEM key material, which previously was
invalid algorithm(or, whenalgorithmsis passed explicitly, e.g.['RS256'],secretOrPublicKey must be an asymmetric key when using RS256).References
verify.jsand may conflict depending on merge order.Testing
Added tests in
test/verify.tests.jsfor a malformed PEM public key passed:They fail on
masterwithinvalid algorithmand pass with this change. The full suite (npm test) passes: 514 passing, 1 pending. eslint is clean on the changed files.Environment: Node.js v24.12.0 on Windows 11.
Checklist