Skip to content

fix(crypto): add KeyObject shim for browser JWT operations - #2931

Closed
Henglie wants to merge 1 commit into
gchq:masterfrom
Henglie:fix/jwt-keyobject-polyfill
Closed

Henglie wants to merge 1 commit into
gchq:masterfrom
Henglie:fix/jwt-keyobject-polyfill

Conversation

@Henglie

@Henglie Henglie commented Nov 6, 2026

Copy link
Copy Markdown

Problem

JWT Sign and JWT Verify fail in the browser build for every algorithm with:

TypeError: Right-hand side of 'instanceof' is not an object

jsonwebtoken@9.0.3 calls KeyObject, createSecretKey, and the other create*Key helpers from crypto. The webpack build resolves crypto to crypto-browserify, which does not implement any of them, so secretOrPrivateKey instanceof KeyObject throws before signing starts.

Fix

Add a small crypto shim used only by the webpack browser build:

  • implement KeyObject, createSecretKey, createPrivateKey, and createPublicKey
  • re-export the rest of crypto-browserify
  • unwrap KeyObject instances before passing them to createHmac

Symmetric (HS256/384/512) JWT signing and verification now work in the browser bundle. Asymmetric keys are still rejected in the browser build, matching the previous behaviour.

Validation

  • npx eslint src/core/lib/CryptoShim.js tests/node/tests/lib/CryptoShim.mjs tests/node/index.mjs passes.
  • Node API suite: 283 passed (includes new unit tests for the shim and HMAC output parity with native crypto).
  • npm run build still fails locally on unrelated existing PNG loader errors and a $ is not a function CSS error; no crypto-related build errors were reported.

crypto-browserify does not implement KeyObject or the create*Key
helpers, so jsonwebtoken throws "Right-hand side of 'instanceof' is
not an object" for every JWT Sign/Verify call in the browser bundle.

Add a minimal shim for symmetric keys and unwrap KeyObject instances
before passing them to crypto-browserify's createHmac.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Thank you for your contribution - unfortunately this repository only accepts pull requests from organisation members or approved outside collaborators. Due to that, this pull request has been closed automatically.

@github-actions github-actions Bot closed this Nov 6, 2026
@Henglie

Henglie commented Nov 6, 2026

Copy link
Copy Markdown
Author

Hi @GCHQDeveloper581, following up on #2860, where a PR for this was welcomed.

This PR was closed automatically because the repository now only accepts PRs from organization members or approved outside collaborators. Could you let me know the process for becoming an approved outside collaborator, or review the branch directly?

Happy to adjust the change or provide more context.

@GCHQDeveloper581

GCHQDeveloper581 commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

@Henglie - we are currently working out the process on this. When we've completed this exercise, relevant affected PRs will be reopened/looked at for processing.

In the meantime I suggest not making any changes to older PRs as this is likely to result in the bot closing them, and older ones are more likely to be missed when reopening.

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.

2 participants