Skip to content

perf: support async signing - #1059

Open
elbuo8 wants to merge 1 commit into
auth0:masterfrom
elbuo8:feat/async-sign
Open

elbuo8 wants to merge 1 commit into
auth0:masterfrom
elbuo8:feat/async-sign

Conversation

@elbuo8

@elbuo8 elbuo8 commented Dec 3, 2026

Copy link
Copy Markdown

Description

jsonwebtoken.sign(payload, key, opts, callback) currently only defers the callback. It goes through jws.createSign, which ends in jwa's crypto.createSign(...).sign(), a synchronous private-key operation on the main thread. An RS256 sign with a 2048-bit key costs about 428 µs of main-thread CPU, and every other request waits behind it.

This change makes the callback form do the RSA/ECDSA signing on the libuv threadpool, with the same tokens as before.

What changed

  • lib/oneShotAlgs.js: digest and crypto.sign options per algorithm (RS/PS/ES only). PS* uses RSA_PKCS1_PSS_PADDING with RSA_PSS_SALTLEN_DIGEST. ES* uses dsaEncoding: 'ieee-p1363'.
  • sign.js, callback form only: builds base64url(header).base64url(payload) exactly as jws does, signs with async crypto.sign(digest, data, key, cb), and returns data.base64url(signature).
    • Sync throws from crypto.sign go to the callback exactly once.
    • ES* signatures are length-checked (64/96/132 bytes). An RSA key under ES* with allowInvalidAsymmetricKeyTypes errors, as the sync form does.
    • An unsupported alg (including one set via options.header.alg) is rejected through the callback with the same jwa TypeError as before.
    • HS* uses crypto.createHmac and none uses an empty signature, both without jws. This fixes the previously skipped none + falsy-secret callback test, which is now enabled.
  • Node < 15.12 has no callback form of crypto.sign. There the callback form signs synchronously through jws and calls back on a later tick. engines is unchanged.
  • The sync form (no callback) is unchanged and still uses jws.

Behavior notes

  • Errors thrown by the caller's callback after a successful sign are still swallowed, as jws did. Errors thrown by a callback that received an error still surface, as before. The swallowing is marked in code to change in the next major.
  • The options_to_payload loop used forEach, so a conflict such as { iss } with { issuer } fell through after reporting the error and called the callback again (three times in total). It's now a real early return.
  • In callback form, 0, false, '', numbers and booleans now produce tokens that match the sync form. Before, the callback was never called for the falsy ones, and sign() threw for numbers and booleans. Object payloads are also serialized at call time, not on a later tick.
  • Key-size check: the post-sign check now looks at the signature segment instead of the whole token. This only differs on Node 12–15.6, where the modulus pre-check isn't available. On 15.7+ the pre-check fires first.

Why sign only

Verify is about 14 µs against about 428 µs for sign, so moving it to the threadpool would cost about what it saves, and it's the security-critical path. verify and decode are untouched.

Benchmark

32 requests in flight, each doing 300 µs of other event-loop work then one RS256 callback sign, UV_THREADPOOL_SIZE=40, Node 22:

req/s p50 (ms) event-loop max (ms)
master 1318 24.3 24.9
this branch 3252 9.2 9.4

Credit

The digest/options table and the async crypto.sign branch come from @panva's #978. I took only those two pieces and did not fork that PR (it also rewrites verify/decode and adds EdDSA and ES256K). One gap in #978 is avoided here: crypto.sign(..., cb) can throw synchronously on a bad key, and this change wraps it.

Testing

The full test suite and lint have run. New tests cover:

  • byte equality with the sync form for RS256/384/512 across PEM string, Buffer, { key, passphrase } and KeyObject keys
  • PS* and ES* output verifying with jwt.verify, with ES* in 64/96/132-byte form
  • the callback never firing in the same tick, and lower event-loop delay than the sync form
  • errors from signing reaching the callback exactly once
  • caller-callback throws not crashing the process after a successful sign
  • key-size rejection, header.alg validation, and HS*/none

Not run: npm run coverage through npm (nyc doesn't finish in my sandbox, even on master); run by hand it meets the thresholds. The Node < 15.12 fallback is untested because no old Node is installed here, and CI only covers Node 16–24.

References

🤖 Generated with Claude Code

@elbuo8
elbuo8 requested a review from a team as a code owner December 3, 2026 18:26
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.

1 participant