RFC-0023: Independent JavaScript Reader

  • Status: Proposed; meets the RFC-0010 second-implementation release gate for reading.

  • Owner: Format lead.

  • Last updated: 2026-11-29

Context

RFC-0005 and RFC-0010 require a second implementation of the envelope format before 4.0.0, as evidence that the specification alone is enough to interoperate. Separately, browsers, edge functions, and Node.js services need to open envelopes that Python services seal.

Binding decision

interop/javascript/src/envelope-reader.mjs is a reader written from docs/v4/spec/envelope-format.md only, with no dependencies. All cryptography is WebCrypto (globalThis.crypto.subtle): HKDF-SHA-512, HMAC-SHA-256, SHA-512, and AES-256-GCM.

  • openEnvelope(bytes, { context, unwrap, limits }) performs open steps 1-4 of section 8.2 for both profiles and returns the plaintext only after the whole envelope verified.

  • unwrap(recipient, protectedBytes) is the caller’s provider: it returns the 32-byte data key, undefined to skip the recipient, or throws, and the next recipient is then tried, exactly as section 8.2 step 2. With no usable recipient the error is KEY_NOT_FOUND; otherwise the last failure is reported.

  • inspectEnvelope returns unauthenticated metadata; readPrefix, canonicalContext, deriveKeySchedule, and headerDigest expose the intermediate values the vectors list.

  • Errors are EnvelopeError with the section 10 codes; authentication and context failures share one message.

  • limits take the default policy’s values and may only lower section 9’s ceilings.

  • The reader does not seal, range-open, or rewrap.

Validation

  • interop/javascript/test/vectors.test.mjs opens every positive vector, checking every intermediate value, and rejects every negative vector with its listed code.

  • tests/contract/test_javascript_interop.py runs that suite and a differential test: Python seals random envelopes (sizes, record sizes, both profiles, one to three recipients, contexts that need NFC normalization), derives tampered, truncated, and extended variants and a wrong context, and the JavaScript reader must return the same plaintext or the same error code for every case. The test provider on the JavaScript side mirrors the Python one rule for rule so that any difference is a difference between the readers. The quality gate requires Node.js 20 or newer (CS_REQUIRE_NODE=1), and the weekly deep run raises the case count 25-fold.

The first differential run reported three disagreements. All three came from the JavaScript test provider not checking the wrapping algorithm, as the Python one does, rather than from either reader; mirroring the provider removed them, and about 9,000 cases at the raised budget agree.

Amendment: sealing

interop/javascript/src/envelope-sealer.mjs adds sealEnvelope(plaintext, { context, recipients, policyId, chunkSize, createdAt }), which writes one-shot envelopes by section 8.1. Each recipient is a caller-supplied async (dek, protectedBytes) => entry that must bind the wrapped key to the protected bytes (section 7); the first is the primary. The data key and message identifier come from crypto.getRandomValues, and the sealer’s copy of the data key is zeroed afterwards.

tests/contract/test_javascript_interop.py adds the reverse direction: JavaScript seals random envelopes (sizes, record sizes up to 1 KiB, one to three recipients in random order, Unicode contexts, and creation times up to the format maximum) and Python must open each through its last listed recipient and report the same metadata.

That test found a defect in the Python implementation: created_at was converted with datetime.fromtimestamp, which raises OSError on Windows for large values, so inspect and open failed with an untyped exception on a valid envelope whose creation time was the format’s maximum (9999-12-31T23:59:59Z). Python never writes such a time itself. The conversion now uses epoch arithmetic, with a regression test in tests/integration/test_protector.py.

Consequences

The specification is shown to be implementable from its text, and any divergence in the readers’ check order would surface as a different error code in CI.