Skip to content

fix(jws): use sorted-key stringifier so signatures survive middleware reordering - #314

Merged
kirgene merged 1 commit into
masterfrom
fix/jws-sign-serializer-mismatch
Apr 21, 2026
Merged

kirgene merged 1 commit into
masterfrom
fix/jws-sign-serializer-mismatch

Conversation

@kirgene

@kirgene kirgene commented Apr 17, 2026 •

Copy link
Copy Markdown
Contributor

The signer and validator both used insertion-order stringification (JSON.stringify via the jws lib on the signer side, fast-safe-stringify on 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 with invalid 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.

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
kirgene force-pushed the fix/jws-sign-serializer-mismatch branch from eb42356 to 6ea2086 Compare April 17, 2026 19:08
@sonarqubecloud

Copy link
Copy Markdown

@kirgene kirgene changed the title fix(jws): sign the same bytes we send and validate fix(jws): use sorted-key stringifier so signatures survive middleware reordering Apr 17, 2026

@elnyry-sam-k elnyry-sam-k left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kirgene
kirgene merged commit 30f3ace into master Apr 21, 2026
17 checks passed
@kirgene
kirgene deleted the fix/jws-sign-serializer-mismatch branch April 21, 2026 19:04
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.

JWS signature verification regression in sdk-standard-components

2 participants