Skip to content

fix(verify): surface key parsing error for malformed PEM public keys - #1049

Open
peralva wants to merge 1 commit into
auth0:masterfrom
peralva:fix/verify-malformed-pem-error
Open

peralva wants to merge 1 commit into
auth0:masterfrom
peralva:fix/verify-malformed-pem-error

Conversation

@peralva

@peralva peralva commented Oct 15, 2026 •

Copy link
Copy Markdown

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 to createSecretKey(), so the allowed algorithms default to HS* and an RS256 token fails with invalid 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 -----BEGIN and createPublicKey() fails, verify() returns:

JsonWebTokenError: secretOrPublicKey is not a valid PEM key: <crypto error message>

The original crypto error is available on err.inner. HMAC secrets that do not look like PEM are unaffected and still go through createSecretKey().

No public API changes. The only behavioral change is the error returned for malformed PEM key material, which previously was invalid algorithm (or, when algorithms is passed explicitly, e.g. ['RS256'], secretOrPublicKey must be an asymmetric key when using RS256).

References

Testing

Added tests in test/verify.tests.js for a malformed PEM public key passed:

  • as a string (sync)
  • as a Buffer (sync)
  • as a string with a callback

They fail on master with invalid algorithm and 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.

  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not the default branch

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
@peralva
peralva requested a review from a team as a code owner October 15, 2026 13:51
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

Successfully merging this pull request may close these issues.

Misleading "invalid algorithm" error on malformed public key

1 participant