fix: keep RSA-OAEP-256 digest correct in runtimes that ignore oaepHash - #106
Open
mmorales-post wants to merge 1 commit into
Open
fix: keep RSA-OAEP-256 digest correct in runtimes that ignore oaepHash#106mmorales-post wants to merge 1 commit into
mmorales-post wants to merge 1 commit into
Conversation
mmorales-post
marked this pull request as ready for review
August 24, 2026 21:03
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.
PR checklist
masterbranchLink to issue/feature request: #105
Description
The JWE key wrap asked node's
crypto.publicEncryptforoaepHash: "sha256". nodehonours that; the
crypto-browserifypolyfill silently ignores it and wraps with SHA-1,producing a token whose header claims
RSA-OAEP-256. It fails only at the recipient.node callers are unaffected. The native path is retained and selected by a one-time
probe. Measured 0.055 ms against 0.050 ms on
mainforencryptData, within noise.The probe. node validates the digest name and throws on an unrecognised one; a
polyfill that ignores
oaepHashaccepts anything. Attempting a wrap under a deliberatelybogus digest therefore distinguishes them, costs one call, and is memoised. It also treats
an absent or stubbed
cryptomodule as unsupported, which is whatresolve.fallback.crypto = falsein this repo'swebpack.config.jsproduces.The fallback goes through node-forge with SHA-256 set for both the OAEP label digest
and MGF1. MGF1 has to be set explicitly or forge defaults it to SHA-1, which would
reintroduce the same class of bug one layer down. The helper mirrors
createOAEPOptionsin
field-level-crypto.js. Forge keys are parsed lazily, so node callers never parse them.decryptDatagets the same treatment, so a browser build can read responses as well assend requests.
Scope. This fixes the digest, which is the failure that is silent. It does not make
the library work under
resolve.fallback.crypto = false, becauserandomBytes,createCipherivandcreateDecipherivstill require the module. Under a bundler thatpolyfills
cryptothe full JWE path now works. Happy to raise the remaining pieceseparately if that is useful.
Validation
Three tests added to
test/jwe-crypto.test.js:still wraps with SHA-256 when the runtime ignores oaepHashstubspublicEncrypttobehave like the polyfill, then asserts the emitted key unwraps with SHA-256 and not
SHA-1. Fails on
main.detects whether the runtime honours oaepHashcovers the probe against real node, apolyfill, and an empty module. Fails on
main.wraps the content encryption key with RSA-OAEP-256, not SHA-1locks the digest for thenative path. This one passes on
mainas well, by design: under node the old code wasalready correct, which is why the bug survived. It is there to stop a regression.
Cross-checked that forge and native interoperate in both directions on the same key, and
that a SHA-1 unwrap of the forge output is rejected.
Full suite: 192 passing. eslint: 0 errors, 5 warnings, unchanged from
main.Validated on Node 24.