Skip to content

Skip failed public key parse for HMAC string secrets - #1047

Open
Hashim1999164 wants to merge 2 commits into
auth0:masterfrom
Hashim1999164:fix/hs256-string-secret-key-parse
Open

Hashim1999164 wants to merge 2 commits into
auth0:masterfrom
Hashim1999164:fix/hs256-string-secret-key-parse

Conversation

@Hashim1999164

@Hashim1999164 Hashim1999164 commented Sep 18, 2026 •

Copy link
Copy Markdown

By submitting a PR to this repository, you agree to the terms within the Auth0 Code of Conduct.

Description

verify() currently tries createPublicKey first for every non KeyObject secret. For a normal HMAC string secret that call always throws, and that throw dominates verify time.

String secrets that do not look like PEM, SSH, or JWK now go through createSecretKey first. PEM public keys still go through createPublicKey first, so an HS256 token cannot treat an RSA public key as an HMAC secret.

References

Fixes #1046

Testing

  • mocha (513 passing, 1 pending)
  • eslint on verify.js and the new tests
  • HS256 string secret verify still works
  • PEM public key plus HS256 token still fails with must be a symmetric key

verify() always called createPublicKey first. For a normal HMAC string
secret that throws, which dominated verify time. Try createSecretKey
first for strings that are not PEM, SSH, or JWK, and keep PEM public
keys on the public-key path so HS256 cannot treat them as HMAC secrets.
@Hashim1999164
Hashim1999164 requested a review from a team as a code owner September 18, 2026 15:49
@Katha-Sikdar

Copy link
Copy Markdown

Thanks for the fix. One gap: looksLikeAsymmetricKey() only matches -----BEGIN at the start of the string, but OpenSSL skips any text before a PEM header. So a PEM public key preceded by, for example, a comment line now takes the secret-first path, and an HS256 token HMACed with the key text is accepted when algorithms is not set. 9.0.3 rejects the same token with "invalid algorithm".

Minimal Repro:
const crypto = require('crypto'); const jwt = require('jsonwebtoken'); const { publicKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 }); const key = 'public key for service X\n' + publicKey.export({ type: 'spki', format: 'pem' }); const b64u = (o) => Buffer.from(JSON.stringify(o)).toString('base64url'); const data = ${b64u({ alg: 'HS256', typ: 'JWT' })}.${b64u({ sub: 'attacker' })}; const token = ${data}.${crypto.createHmac('sha256', key).update(data).digest('base64url')}; jwt.verify(token, key); // this PR: accepted; 9.0.3: throws "invalid algorithm"

Searching for the header anywhere in the string closes it:
`
function looksLikeAsymmetricKey(key) {
if (typeof key !== 'string') {
return true;
}

  • return /^\s*(-----BEGIN |ssh-|{)/.test(key);
  • return key.indexOf('-----BEGIN') !== -1 || /^\s*(ssh-|{)/.test(key);
    }
    `
    I have a patch on top of 0d88644 with that change, a regression test for the case above, and four key-path tests (an HS* token is refused when the configured key is asymmetric material, with and without algorithms; a string secret and its KeyObject verify identically; a wrong string secret is still rejected). With it the suite passes 518/0; without the verify.js change the new regression test fails. Happy to share it or push it wherever is convenient.

@Hashim1999164

Copy link
Copy Markdown
Author

pem with text before the header was still taking the hmac path. begin is matched anywhere in the key now, and that case is in the tests.

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.

verify() resolves a string secret by attempting createPublicKey() first, costing 4x-52x on the HS* path

2 participants