fix(jws): use sorted-key stringifier so signatures survive middleware reordering - #314
Merged
Merged
Conversation
kirgene
requested review from
bushjames,
elnyry-sam-k,
geka-evk,
gibaros,
kalinkrustev,
kleyow,
oderayi,
shashi165 and
vijayg10
as code owners
April 17, 2026 14:04
kirgene
force-pushed
the
fix/jws-sign-serializer-mismatch
branch
2 times, most recently
from
April 17, 2026 18:51
4c1c4ae to
eb42356
Compare
Signer used JSON.stringify (via jws lib), wire body and validator use fast-safe-stringify. When they diverge, signature verification fails. Stringify once up front with safeStringify. Also replaces fast-safe-stringify with safe-stable-stringify (fast-safe-stringify has been unmaintained for ~5y).
kirgene
force-pushed
the
fix/jws-sign-serializer-mismatch
branch
from
April 17, 2026 19:08
eb42356 to
6ea2086
Compare
|
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.



The signer and validator both used insertion-order stringification (
JSON.stringifyvia the jws lib on the signer side,fast-safe-stringifyon the validator side; both insertion-order for plain objects). If anything between signer and validator parses and re-serializes the JSON with different key order (proxies, gateways, framework body-parsers), the validator hashes different bytes than the signer covered, and verification fails withinvalid signature.Switch both signer and validator to
safe-stable-stringify, which sorts keys. The validator now re-sorts whatever it receives before hashing, so the signature verifies regardless of how middleware reorders keys in transit.Also stringifies the body once up front in the signer so the bytes fed to
jws.sign()are the same sorted bytes that go on the wire.