Skip to content

fix(stack-encrypt): wipe the fields a refused OpaqueBlob read before the refusal - #1750

Merged
coderdan merged 2 commits into
nextfrom
fix/stack-encrypt-wipe-partial-serde-decode
Nov 22, 2026
Merged

coderdan merged 2 commits into
nextfrom
fix/stack-encrypt-wipe-partial-serde-decode

Conversation

@coderdan

@coderdan coderdan commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Decrypting an OpaqueBlob<T> can fail part-way. A blob can be a valid ciphertext of the wrong shape: a later field is missing or has the wrong type, and it's refused after some fields were already decrypted and read. Those fields used to be freed with the plaintext still in them. Decrypting now reads the blob into a T::default() that is wiped if the blob is refused, so the fields read before the refusal are wiped too.

Two costs need your decision:

  • Decrypting a blob now needs T: Default. OpaqueBlob is unreleased, so this breaks nothing that has shipped.
  • stack-encrypt turns on serde_derive's deserialize_in_place feature. Cargo applies a feature to the whole build, so every #[derive(Deserialize)] in a build that includes stack-encrypt also generates that method. deserialize itself is unchanged.

Changes

  • src/blob.rs:
    • OpaqueBlob::decode reads through a private ReadInPlace<T>, which calls T::deserialize_in_place on a Zeroizing::new(T::default()).
    • The decode impls gain T: Default.
    • The module docs say what is wiped and what is not, and that a #[serde(skip)] field keeps T::default()'s value.
  • Cargo.toml: serde_derive is now a root [workspace.dependencies] entry, and stack-encrypt uses it with features = ["deserialize_in_place"]. A comment explains why, and a cargo-udeps ignore covers it because no code names the crate.
  • Four lockfiles (root, Go guest, packages/eql, stack-encrypt/fuzz): one serde_derive line each.
  • Tests:
    • heap_hygiene.rs: the should_panic control for this gap becomes opaque_blob::given_a_blob_refused_after_fields_are_built, with two real heap assertions and one new control for the narrower gap that remains.
    • opaque_blob.rs: a drop-witness test, a_field_read_before_a_refusal_is_wiped, that runs on every platform.
    • #[derive(Default)] on the blob types the tests, example and docs decrypt.
    • The UI snapshot opaque_blob_requires_zeroize.stderr is refreshed; only the bound line changes.
  • Conformance:
    • lifecycle-partial-output-v1 now enrolls the two heap tests and the drop witness.
    • The known gap native-partial-decode-fields is replaced by the narrower native-partial-decode-inside-whole-values.
  • Docs: ADR-0011 sections on cleanup and complete erasure, and a CHANGELOG entry with upgrade steps.

Verification

  • Without the fix, all three new tests fail:
    • the_fields_built_before_it_are_wiped: 3 heap blocks freed with plaintext still in them.
    • a_nested_field_and_whole_elements_built_before_it_are_wiped: 4 blocks.
    • a_field_read_before_a_refusal_is_wiped: the value "secret" is never wiped.
  • With the fix, all three pass. The new control still panics, on the one block that remains: the failing sequence element's String.
  • cargo test -p stack-encrypt --all-features: all test targets and doc tests pass.
  • cargo fmt --check: clean.
  • cargo clippy with -D warnings: clean for stack-encrypt and for the whole root workspace with --locked, which checks every derive with the new feature on.
  • cargo doc with -D warnings: clean.
  • EQL: cargo test --locked -p eql-encryption-tests --all-features passes.
  • The 28 heap and opaque tests that the lifecycle enrollment names pass under nextest.
  • scripts/__tests__/stack-lifecycle-conformance.test.mjs: passes.
  • Go: all guests were rebuilt. go test ./... passes, and go generate ./... produces no drift.
  • cargo-mutants --in-diff: of the two mutants, one is caught and the other doesn't compile.

Related

Closes #1676. Stacked on #1749.

The cases this PR can't reach are tracked in #1754: values serde builds whole (sequence elements, map entries, Option, Box, enums), the derive shapes it doesn't read in place, and the 1 MiB sequence regrowth.

Review notes

  • Why read in place: a type's derived Deserialize holds the fields it has read as locals and drops them on an error, where nothing outside the type can reach them. Reading in place is the only mechanism that puts those fields where the library can wipe them. Serde documents deserialize_in_place as stable public API, although it is hidden from rustdoc.

  • Now covered: fields of the struct, fields of nested structs, tuples and arrays, and Vec elements stored before the refusal.

  • Still a known gap (native-partial-decode-inside-whole-values, tracked in stack-encrypt: wipe what serde reads whole when an OpaqueBlob decrypt is refused #1754):

    • a refusal inside a value serde builds whole before storing it: a sequence element, a map entry, an Option, a Box or an enum;
    • a field read with deserialize_with or with;
    • a struct that is transparent, from or try_from, has a flatten field, or has deserialize_with on every field;
    • a hand-written Deserialize without deserialize_in_place;
    • a sequence longer than serde's 1 MiB up-front reservation, which regrows and frees its old buffers unwiped.

    Negative controls pin both the whole-value case and the transparent case. The workaround is unchanged: a field type that wipes itself on drop, such as Zeroizing<String>.

  • One change on success: a field that serde skips keeps T::default()'s value, so a #[serde(default = "...")] on it isn't used. This is documented and pinned by a test (given_a_field_serde_skips). A tests/ui case pins the new T: Default bound.

  • eql-bindings needs no change: it never decodes plaintext through Serde, and its JSON plaintext goes through stack-encrypt's own parser, which already wipes a part-built document.

@coderdan
coderdan requested a review from a team as a code owner November 22, 2026 14:18
@changeset-bot

changeset-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 375e611

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d0b1e9f4-166b-46f6-845c-5962c4feaeb9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

@github-actions

Copy link
Copy Markdown
Contributor

Mutation testing (cargo-mutants, --in-diff, stack-auth + stack-encrypt + stack-encrypt-jsonpath)

caught missed unviable timeout
1 0 1 0

Every mutant in the changed lines was caught by a test.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The plaintext-wiping guarantee overstates coverage for derived Serde structs that fall back to whole-value decoding, requiring qualified documentation and a negative-control test.

1 open issue
What changed in this PR

This PR narrows the partial-decoding cleanup gap in Stack Encrypt’s Rust OpaqueBlob<T> support, addressing #1676 by wiping fields already stored when a later field is refused.

Changes:

  • Decode into a guarded T::default() using Serde’s in-place reader.
  • Add heap checks and a wipe-witness test, and enroll them in lifecycle conformance.
  • Document the new Default requirement and remaining cleanup limits.
File Description
packages/​stack-encrypt/​tests/​ui/​opaque_blob_requires_zeroize.stderr Refreshes the diagnostic’s decode bound.
packages/​stack-encrypt/​tests/​opaque_blob.rs Adds a partial-read wipe witness and defaultable fixtures.
packages/​stack-encrypt/​tests/​heap_hygiene.rs Checks partial-read cleanup and the remaining sequence-element gap.
packages/​stack-encrypt/​tests/​exchange_conformance.rs Makes the blob fixture defaultable.
packages/​stack-encrypt/​src/​lib.rs Updates the documentation example’s derives.
packages/​stack-encrypt/​src/​blob.rs Adds guarded in-place decoding and cleanup documentation.
packages/​stack-encrypt/​fuzz/​Cargo.lock Records the direct serde_derive dependency.
packages/​stack-encrypt/​examples/​opaque_blob.rs Adapts example types to the Default requirement.
packages/​stack-encrypt/​docs/​adr/​0011-a-whole-value-is-one-storage-blob-sealed-as-one-leaf.md Records the cleanup approach and remaining limits.
packages/​stack-encrypt/​CHANGELOG.md Explains the behavior change and upgrade steps.
packages/​stack-encrypt/​Cargo.toml Enables Serde’s in-place derive feature.
packages/​eql/​Cargo.lock Records the dependency for the EQL workspace.
languages/​golang/​encrypt/​guest/​Cargo.lock Records the dependency for the Go encryption guest.
conformance/​stack-encrypt/​v1/​lifecycle-v1.enrollment.json Enrolls cleanup tests and renames the known gap.
conformance/​stack-encrypt/​lifecycle-contract.md Updates cleanup coverage and its limitations.
Cargo.lock Records the dependency for the root workspace.

Review effort: Balanced

Comment thread packages/stack-encrypt/src/blob.rs Resolved

@cipherstash-agent cipherstash-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review attestation

Reviewed aafa693 · posted from Claude Code (scheduled routine) · claude-opus-5-5 · code-review effort medium
Sources: code-review · claude-opus-5-5; rust-security agent · claude-opus-5-5; mattpocock code-review (standards, spec) · claude-opus-5-5
Skills: code-review, company-skills:rust-security, mattpocock-skills:code-review, company-skills:review-cross-check, company-skills:review-post

Findings (11): 1 major, 10 minor. 10 are inline and 1 is below. Every finding was checked against the code at aafa693 with a repro or a trace. Two were refuted and are not posted: the lifecycle-contract.md:16 wording, and the ADR-0011 amendment marker.

  • docs/plans/2026-10-24-stack-encrypt-api-plan.md:2148 (outside diff) minor This still says "Issue #1676 is open for the partial Serde decode". Record the struct-field case as done, and name the remaining native-partial-decode-inside-whole-values gap and its tracking issue.

Generated by Claude Code

Comment thread packages/stack-encrypt/src/blob.rs Resolved
Comment thread packages/stack-encrypt/src/blob.rs Resolved
Comment thread packages/stack-encrypt/src/blob.rs Resolved
Comment thread packages/stack-encrypt/src/blob.rs Resolved
Comment thread packages/stack-encrypt/src/blob.rs Resolved
Comment thread packages/stack-encrypt/Cargo.toml Resolved
…the refusal

A valid blob of another shape can be refused after a derived Deserialize
has read some of its fields. serde holds those fields as locals until the
whole T is built, so a refusal dropped them as ordinary values, unwiped:
the lifecycle profile carried this as the native-partial-decode-fields
known gap.

Decrypting an OpaqueBlob<T> now reads the blob in place, through serde's
Deserialize::deserialize_in_place, into a T::default() held in a
Zeroizing<T>. A derived Deserialize for a struct stores each field in that
value as it reads it, so a refusal drops the Zeroizing and wipes every
field read before it: the fields of the struct, of structs nested in it,
and the elements of a Vec stored before the refusal. This needs
serde_derive's deserialize_in_place feature, which stack-encrypt now turns
on (Cargo unifies it across the build; deserialize itself is unchanged),
and a T: Default bound on decrypting a blob. OpaqueBlob is unreleased, so
the bound changes no released API.

serde still builds an element of a sequence, a map entry, an Option, a
Box and an enum whole. A refusal inside one of those drops what it built
unwiped. The lifecycle profile now enrolls the closed case as
lifecycle-partial-output-v1 evidence (two heap observations and a drop
witness) and carries the rest as the narrower
native-partial-decode-inside-whole-values known gap, with its own
should_panic negative control in place of the old one.

eql-bindings needs no change: it decodes no plaintext through Serde. Its
JSON plaintext goes through stack-encrypt's own JSON parser, which already
wipes a part-built document.

Closes #1676
serde_derive writes no deserialize_in_place for a struct marked
transparent, from or try_from, one with a flatten field, or one whose
every field uses deserialize_with. Such a struct is read whole wherever it
appears, so a refusal of its later field drops the fields read before it
unwiped, even when it is T itself. The blob docs, ADR-0011, the CHANGELOG
and the lifecycle contract now list those shapes, and a field read with
deserialize_with, among the values serde reads whole, in place of the
wording "a type whose Deserialize is not derived for a struct" (String,
Vec, tuples and arrays are read in place). A second should_panic control
opens a blob as a transparent struct, and the
native-partial-decode-inside-whole-values enrollment names every case,
lists both controls and carries a tracking placeholder for the follow-up
issue.

The promise for the elements of a sequence is qualified: serde reserves
room for at most 1 MiB of elements before it reads them, and a longer
sequence regrows its buffer and frees each old one unwiped.

A field serde skips when reading keeps the value T::default() gives it,
because the in-place read never assigns it, so a #[serde(default = "...")]
on it is not used. The docs say so, and a test pins it against serde's
whole-value read of the same bytes.

A tests/ui case decrypts and decodes a type that is Zeroize but not
Default, so relaxing the bound, and with it the in-place wipe, fails a
test.

a_field_read_before_a_refusal_is_wiped encrypted a Witness, which moved
WITNESS_WIPES while is_wiped_on_drop_and_into_inner_keeps_it_guarded
checked exact deltas of it, so threaded cargo test failed now and then
(7 of 200 runs). It encrypts a Tracked source now: 0 of 200.

serde_derive comes from [workspace.dependencies]; stack-encrypt turns on
deserialize_in_place there. The heap tests share a Sealed struct and one
is_a_mismatch assertion. The API plan records the closed case and the gap
that remains.
@coderdan
coderdan force-pushed the fix/stack-encrypt-wipe-partial-serde-decode branch from aafa693 to 375e611 Compare November 22, 2026 15:15
@coderdan

Copy link
Copy Markdown
Contributor Author

Review round 1, addressed in 375e611. The branch was also rebased onto #1749's new head and force-pushed.

Checks at 375e611:

  • cargo fmt --check passes.
  • clippy with -D warnings passes with --all-features, the default features and --no-default-features.
  • cargo test -p stack-encrypt --all-features passes, including tests/ui. Both negative controls still panic as intended.
  • cargo test -p stack-encrypt --no-default-features with -D warnings passes.
  • rustdoc with -D warnings passes.
  • The lifecycle conformance script test passes.
  • On the whole PR diff, cargo-mutants finds 2 mutants: 1 caught, 1 unviable.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The proposed Default bound for blob decryption, skipped-field default changes, and build-wide serde_derive deserialize_in_place feature require maintainer judgment on API and dependency trade-offs.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Base automatically changed from refactor/stack-encrypt-remove-unraised-plan-errors to next November 22, 2026 22:52

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Human pass. LGTM!

@coderdan
coderdan merged commit 5949db0 into next Nov 22, 2026
60 checks passed
@coderdan
coderdan deleted the fix/stack-encrypt-wipe-partial-serde-decode branch November 22, 2026 22:59
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.

stack-encrypt: a Serde value that fails part-way through decoding leaves the decoded parts unwiped

3 participants