Add Jwe module (RFC 7516) ported from the upstream JOSE PR - #4
Merged
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.
Ports the JWE module from Effect-TS/effect#6566 at its latest commit (
35830ea0), so all of that PR's JWE security hardening is included. Complements #3, which ported the PR's fixes for the modules that already existed here.Changes
src/Jwe.ts(new) — JWE Compact Serialization (RFC 7516):encrypt/decryptbuilt on WebCrypto,ProtectedHeaderandCompactschemas, and a typedJweError. Content encryption: A128/192/256GCM and A128CBC-HS256/A192CBC-HS384/A256CBC-HS512. Key management:dir, RSA-OAEP(-256), A*KW, A*GCMKW, ECDH-ES (direct and key-wrap), and PBES2.RSA1_5is intentionally unsupported — WebCrypto does not implement it and RFC 8725 discourages it.src/Jwa.ts— addJweAlgorithm,JweEncryption, andencryptionParameters(RFC 7518 Sections 4–5).src/index.ts— regenerated (pnpm codegen) to exportJwe.test/Jwe.test.ts(new) — round-trips all 16×6 alg × enc combinations, extra protected-header parameters, tampered-ciphertext and tampered-CBC-HMAC-tag rejection, wrong-key failure, and the RFC 7516 Appendix A.3 vector as external ground truth.test/Security.test.ts— port the upstream JWE hardening regressions: PBES2 iteration-count bound (DoS guard),alg/encallowlists, unrecognizedcritrejection, typedMalformedon bad base64url, ECDH-ESapu/apvbound into the KDF,dirkey-length mismatch, wrong-length CEK failing closed, and a non-exportabledirkey failing closed.Security posture carried over from upstream
JweError— never an unhandled defect.critextensions are rejected (RFC 7516 §4.1.13).p2cis bounded (1000–10000 by default) before the expensive derivation runs (RFC 8725).importKey.epkis imported on the recipient's own curve; WebCrypto validates the point lies on it, defeating invalid-curve attacks.Adaptation notes
Imports go through the
effectbarrel instead of the monorepo's relative paths, docs use@since 1.0.0with@seeRFC links, tests use this repo'sit.live+expectconventions, and the upstreamheader.p2c!assertion became a narrowed local. Test helpers takeUint8Array<ArrayBuffer>(TS 6 flags theArrayBufferLikedefault againstBufferSource).Verification
pnpm check,pnpm lint,pnpm circular,pnpm build, andpnpm testall pass (44/44, including the 29 new JWE tests).