Repository navigation
Conversation
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.
Description
jsonwebtoken.sign(payload, key, opts, callback)currently only defers the callback. It goes throughjws.createSign, which ends in jwa'scrypto.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 andcrypto.signoptions per algorithm (RS/PS/ES only). PS* usesRSA_PKCS1_PSS_PADDINGwithRSA_PSS_SALTLEN_DIGEST. ES* usesdsaEncoding: 'ieee-p1363'.sign.js, callback form only: buildsbase64url(header).base64url(payload)exactly as jws does, signs with asynccrypto.sign(digest, data, key, cb), and returnsdata.base64url(signature).crypto.signgo to the callback exactly once.allowInvalidAsymmetricKeyTypeserrors, as the sync form does.alg(including one set viaoptions.header.alg) is rejected through the callback with the same jwaTypeErroras before.crypto.createHmacandnoneuses an empty signature, both without jws. This fixes the previously skippednone+ falsy-secret callback test, which is now enabled.crypto.sign. There the callback form signs synchronously through jws and calls back on a later tick.enginesis unchanged.Behavior notes
options_to_payloadloop usedforEach, 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.0,false,'', numbers and booleans now produce tokens that match the sync form. Before, the callback was never called for the falsy ones, andsign()threw for numbers and booleans. Object payloads are also serialized at call time, not on a later tick.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.
verifyanddecodeare 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:Credit
The digest/options table and the async
crypto.signbranch 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:
{ key, passphrase }andKeyObjectkeysjwt.verify, with ES* in 64/96/132-byte formheader.algvalidation, and HS*/noneNot run:
npm run coveragethroughnpm(nyc doesn't finish in my sandbox, even onmaster); 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