Repository navigation
feat(filecoin-encryption-envelope): add AbortSignal support, ciphertext length errors, and sizing exports - #1186
Conversation
filecoin-encryption-envelope
@filoz/keysmith
@filoz/synapse-core
@filoz/synapse-react
@filoz/synapse-sdk
commit: |
There was a problem hiding this comment.
Important
Please fix two cancellation paths before merging: a range abort can leave the caller waiting on adapter cleanup, and resolver cleanup can make an aborted read report normal EOF.
Approach: Reusing the existing sizing logic, extending the existing length error, and giving KeyResolver a stream-owned signal are the right boundaries; the cancellation ordering needs two fixes.
Reviewed changes across the streaming and range APIs, recipient recovery, public exports, docs, and tests.
- Sizing: Exposes the detached ciphertext calculation and the constants needed for full-object preflight checks.
- Length errors: Distinguishes short and excess ciphertext against a declared
plaintext_lengthwhile preservinginstanceof InvalidCiphertextLengthError. - Cancellation and key ownership: Adds external signals to streaming and range decryption, forwards them to range sources and unwrappers, and notifies the resolver without mutating or importing late keys.
- Verification: Build passed; Node tests had 756 passing and Chromium had 744 passing with 9 pending. Separate checks reproduced both cancellation findings.
gpt-6.2-sol | 𝕏
|
Docs preview: https://pr-1186-synapse-dev.filoz.workers.dev Built from dee292d. |
There was a problem hiding this comment.
Important
The previous cancellation fixes work, but the new paths can still leave reads pending after abort, replace a truncation error, or report success after cancellation.
Reviewed changes in d6f6d77, the commit added since the previous review.
- Hardened cancellation by observing dependent signals, settling stream errors before cleanup, and no longer waiting for adapter teardown during range setup or failure.
- Expanded signal coverage to
parse(), streaming unwrappers, and ciphertext range opens. - Moved truncation detection to writable EOF so a short input can stop pending key recovery.
- Added key cleanup for temporary AES-KW buffers while preserving ownership of keys returned by custom callbacks.
- Verified the package build, lint, and Node and Chromium suites; reproduced the four findings separately in Node.
gpt-6.2-sol | 𝕏
There was a problem hiding this comment.
Important
The cancellation fixes work. The new strong observer also keeps discarded streams and their input buffers alive under a shared signal, so callers need an explicit cleanup contract.
Reviewed changes in b2f94b6, the commit added since the last Pullfrog review.
- Kept pending operations abortable when only the read or
writer.closedpromise survives garbage collection. - Preserved the original writable error when resolver cleanup re-enters
writer.abort()after short or excess input. - Rechecked cancellation after envelope probe cleanup starts and after each built-in AES-KW unwrap attempt, wiping any discarded key bytes.
- Verified build and lint, 774 passing Node tests with forced GC, and 763 passing Chromium tests. A separate Node check confirmed the retention described inline.
gpt-6.2-sol | 𝕏
…E, and ciphertextLengthForPlaintext
…ciphertext errors
…nd document key ownership
…, and key cleanup
…ch sink errors, and recheck cancellation in parse and A256KW unwrap
b2f94b6 to
2cf43f9
Compare
|
Reviewed at 2cf43f9. Closes #1172, #1173, #1177 and #1178 as they asked; nothing blocking. A few notes:
|
…arify cancellation and truncation docs
|
Thanks for the review @BravoNatalie! Addressed in
|
There was a problem hiding this comment.
Important
The simplified abort observer can reject a live decrypt stream or leave a cancelled operation pending. The new native API requirements also reject valid signals on browsers that lack AbortSignal.any().
Approach: The smaller helper surface is useful, but cancellation still needs to follow the signal’s state rather than the delivery of a public event.
Reviewed changes in dee292d, the only substantive commit since the previous Pullfrog review.
- Simplified cancellation by attaching listeners directly to the supplied signals, forwarding the internal range signal to adapters, and switching to native signal methods.
- Clarified ownership and errors by documenting stream retention under shared signals, ownership of discarded keys, and what truncation means in streaming versus range reads.
- Verified that build, lint, 773 Node tests with forced GC, and 762 Chromium tests pass; separate Node checks confirmed the regressions inline.
gpt-6.2-sol | 𝕏
There was a problem hiding this comment.
Thanks, re-reviewed at dee292d. All four addressed; agreed on keeping MalformedEnvelopeError per the package's error convention. The note about long-lived signals is a good addition.

Summary
Combines four
filecoin-encryption-envelopeimprovements addressingfilecoin-project/agents#181as four cleanly separated Conventional Commits:1. Sizing exports and preflight helper (
#1178)MAX_ENVELOPE_SIZEandTAG_SIZEfromconstants(src/public-constants.ts).ciphertextLengthForPlaintext(plaintextLength, chunkSize)from the package root (src/index.ts) for preflight encoded-size checks.README.mdand cover public exports and boundary cases intest/public-surface.test.ts.2. Distinct truncated and excess ciphertext length errors (
#1177)TruncatedCiphertextErrorandExcessCiphertextError(both extendingInvalidCiphertextLengthError) insrc/errors.ts.src/aes-gcm-stream.ts) and range planning (src/range/plan.ts) when observed/declared ciphertext lengths are shorter or longer than expected.README.md,docs/implementation-guide.md, and test suites (test/aes-gcm-stream-decrypt.test.ts,test/range-plan.test.ts,test/range-decrypt.test.ts,test/public-surface.test.ts).3.
AbortSignalsupport across streaming and range decryption (#1172)options?: DecryptOptions({ signal?: AbortSignal }) todecrypt()anddecryptWith(), andsignal?: AbortSignaltoRangeDecryptOptionsfordecryptRange()anddecryptRangeWith().src/internal/abort.ts(assertOptionalAbortSignal,throwIfAborted,raceAbort) to preserve exactsignal.reasonidentity, reject promptly on pre-aborted or mid-flight aborts, unblock pending writes/reads, cancel active or late-arriving range streams, and clean up'abort'listeners across all exit paths.options.signaltoRandomAccessSource.openRangeandUnwrapper(including stopping remaining unwrap attempts increateA256KWUnwrapper).test/abort-signal.test.ts.4.
AbortSignalforKeyResolverand key ownership guarantees (#1173)KeyResolverto always receive(info: EnvelopeInfo, signal: AbortSignal)backed by an internalAbortControllerincreateDecryptStream(existing 1-argument resolvers remain assignable).failInput(reason)on readable cancellation, writable abort, stream failure, andoptions.signalabort (forwarding exactoptions.signal.reason), while leavingsignal.aborted === falseon successful EOF.assertAes256Key) andimportAesGcmKeyand are left untouched (never zeroed by FEE).KeyResolverabort semantics inREADME.md,docs/implementation-guide.md, and JSDoc, with unit and integration coverage intest/aes-gcm-stream-decrypt-resolver.test.ts,test/key-resolver-flow.test.ts, andtest/abort-signal.test.ts.Verification
pnpm run lint:fix— passed (0 errors)pnpm turbo build --filter=filecoin-encryption-envelope --force— passedpnpm turbo lint --filter=filecoin-encryption-envelope --force— passed (67 files checked)pnpm turbo test --filter=filecoin-encryption-envelope --force— passed (Node: 756 passing; browser: 744 passing, 9 pending)pnpm turbo test --filter=@filoz/keysmith— passed (Node: 179 passing; browser: 179 passing)pnpm exec markdownlint-cli2 --config .github/.markdownlint-cli2.jsonc 'packages/filecoin-encryption-envelope/**/*.md'— passed (0 issues in 3 files)Related: filecoin-project/agents#181
Closes #1172
Closes #1173
Closes #1177
Closes #1178