Repository navigation
refactor(stack-encrypt)!: remove the lone term methods from the public API - #1766
Conversation
KeysetCipher::equality_term, match_terms, ore_term, and ope_term each built one term on its own, outside the target path and the query path. A term built that way had nothing tying it to the context of the ciphertext stored beside it. For most indexes it was also the wrong query term: with MatchOptions::short_words on, the match_terms filter of "Mike Ng" misses "Mike Nguyen". The four methods are now pub(crate) and #[cfg(test)]. No library code called them. The dynamic path derives through equality_term_normalized, match_terms_under, and order_pending, and only its tests called the four methods. So the methods stay, as the terms that this crate's tests compare other paths against. New unit tests in sem::lone_terms check that each method derives its target's bytes. The tests run with the keyset loaded, and with it not yet loaded. equality_term_normalized and match_terms_under are now cfg(any(test, feature = "dynamic")). That replaces an allow(dead_code). Callers outside the crate now use the public paths: - A stored term comes from encrypting the value into its term type with encrypt_into_with_context, or from KeysetCipher::run with an operation. The term types are EqualityTerm, MatchTerms<O>, OreTerm<T>, and OpeTerm<T>. - A query comes from a field reference (AccountTerms::EMAIL.eq) or from target::matching_query run on its own. The change covers 13 stack-encrypt integration test files, the EQL encryption test text_eq.rs, and the Go guest's native_ops.rs. The frozen byte pins in term_bytes.rs and frozen_bytes.rs keep the same expected bytes. The check in target.rs that the target's equality term equals the method's moved into sem::lone_terms. The rustdoc that named the methods now says how to get each term. The EqualityTerm docs take the normalisation rules from equality_term. ADR-0004 decision 5 has an amendment. The CHANGELOG has a Breaking entry with the upgrade steps. ore_block_term stays public. The issue did not name it, though it also builds a term outside a target. No stored byte changes. Closes #1760
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
|
Mutation testing (cargo-mutants,
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the Rust API narrowing and accompanying migrations.
No open issues 🎉
What changed in this PR
This breaking change narrows Stack Encrypt’s Rust API to steer callers toward targets for stored terms and field references for queries. It addresses #1760 and follows the match-query corrections in #1753, without changing stored-byte formats.
Changes:
- Makes four standalone term methods crate-private and test-only, while retaining helpers needed by the dynamic path.
- Migrates Stack Encrypt, EQL, and Go guest tests to public APIs; adds internal parity tests and retains frozen-byte checks.
- Updates rustdoc, ADR-0004, and the changelog with replacement APIs and upgrade examples.
| File | Description |
|---|---|
packages/stack-encrypt/tests/value_kinds.rs |
Uses a borrowed equality target for typed comparisons. |
packages/stack-encrypt/tests/transcode.rs |
Derives comparison terms through public targets. |
packages/stack-encrypt/tests/term_bytes.rs |
Migrates derivation while retaining frozen term-byte expectations. |
packages/stack-encrypt/tests/target.rs |
Removes relocated parity coverage and migrates keyset comparisons. |
packages/stack-encrypt/tests/source_mode.rs |
Uses target-derived terms in ownership and cleanup checks. |
packages/stack-encrypt/tests/sem_terms.rs |
Separates stored-term derivation from match-query derivation. |
packages/stack-encrypt/tests/match_short_words.rs |
Migrates calibration while preserving its intentional stored-filter probes. |
packages/stack-encrypt/tests/keysets.rs |
Exercises keyset identity through public term targets. |
packages/stack-encrypt/tests/heap_hygiene.rs |
Checks plaintext cleanup through replacement public APIs. |
packages/stack-encrypt/tests/frozen_bytes.rs |
Keeps encoding pins while switching to targets. |
packages/stack-encrypt/tests/credential.rs |
Migrates deferred, credentialed term checks to targets. |
packages/stack-encrypt/tests/context_conformance.rs |
Uses a target for context-bound equality comparisons. |
packages/stack-encrypt/tests/audit.rs |
Builds audited equality queries from field references. |
packages/stack-encrypt/src/sem/mod.rs |
Restricts standalone methods, gates helpers, and adds parity tests. |
packages/stack-encrypt/src/lib.rs |
Updates documentation for remaining borrowed-input options. |
packages/stack-encrypt/src/dynamic/term.rs |
Updates documentation and its typed equality example. |
packages/stack-encrypt/src/dynamic/mod.rs |
Describes cross-language parity using the target API. |
packages/stack-encrypt/docs/adr/0004-one-context-per-target-threaded-through-the-declaration-tree.md |
Records the API restriction and replacement paths. |
packages/stack-encrypt/CHANGELOG.md |
Documents the breaking change and migration steps. |
packages/eql/tests/encryption/tests/text_eq.rs |
Uses equality targets for native EQL comparisons. |
languages/golang/encrypt/guest/tests/native_ops.rs |
Migrates native reference terms used in guest conformance checks. |
Review effort: Balanced
cipherstash-agent
left a comment
There was a problem hiding this comment.
Review attestation
Reviewed e4cb011 · posted from Claude Code (scheduled routine) · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; rust-security · claude-opus-5-5; mattpocock code-review (standards, spec) · claude-opus-5-5; verified by review-cross-check · 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 (6): 0 critical, 0 major, 6 minor — 5 inline, 1 below.
packages/stack-encrypt/src/sem/mod.rs:2192(outside diff) minorore_block_termstays public (acknowledged in the PR body). Hiding it later would not close the gap on its own: the publicOreBlockTerm<S>target (src/target/operations.rs:1787) has the same any-context freedom as the inline ADR-0004 comment describes, and the method is the only owned block-ORE route for non-ZeroizeOrderabletypes, so the follow-up needs the same migration note asCHANGELOG.md:495.
Not posted: one finding left for the author (whether ADR-0009's same-day #1398 amendment at line 1379, which reads as current guidance on KeysetCipher::match_terms, should point at the #1760 amendment), and one refuted on verification.
Generated by Claude Code
The test-only KeysetCipher::equality_term, match_terms, ore_term, and ope_term were copies of the target path. Each repeated the derivation that the term type's Term::encrypt_from already makes. So the lone_terms tests compared each copy with its original. The four methods and the lone_terms tests are removed. The dynamic tests that compared a dynamic term with a typed one now derive the typed term through its target, with encrypt_into_with_context. That keeps two separate paths in each comparison. The test file `tests/credential.rs` already checks that a term built before the keyset loads is the term built after it. The helpers equality_term_normalized and match_terms_under now compile only with the dynamic feature, because only that path calls them. The docs no longer say that the compiler enforces one context for a term and its ciphertext. A bare term target still takes any context. The term shares its ciphertext's context only as a field of a derived record, or beside its ciphertext in Encrypted<(…)>. The wording changes in ADR-0004's amendment, the CHANGELOG, and the sem module and impl docs. Each also says that a bare MatchTerms with short words on is a stored filter, not a query. The CHANGELOG upgrade note now covers jiff's civil::Date and Timestamp, and rust_decimal's Decimal. OreTerm<T> and OpeTerm<T> need T: Zeroize, so those types take value::Date, value::Timestamp, or value::Decimal, or the borrowed operation target::ope::<_, _, Borrowed>(). A scratch test checked that both routes compile and give the same bytes. The sem module docs say that a target and a field query's encrypt return a Pending, since a field reference's operator returns a FieldQuery. ADR-0009's match_terms bullet points to ADR-0004's amendment. Two paragraphs are rewrapped.
|
Review 6015918939: the ADR-0009 pointer is added in 19f4519. The |
KeysetCipher::ore_block_term built a block order-revealing encryption (ORE) term outside a target, as the four term methods removed earlier on this branch did. Nothing tied that term to the context of the ciphertext stored beside it. The method is removed. The term type OreBlockTerm<T>, the operation target::ore_block, and the index OreBlock stay public. No helper becomes dead. order_pending, ore_block, and ore_block_error still serve the OreBlockTerm target through the order_term macro. The tests that called the method now build each term through its target, with encrypt_into_with_context. The borrowed-value test runs target::ore_block with a Borrowed source through KeysetCipher::run. The credential test still checks that a term built before the keyset loads equals one built after it. A block ORE term contains a random nonce, so the test compares by the scheme's comparison. The pinned left halves pass with the same bytes. A scratch test, since deleted, checked the upgrade route for the three types that are not Zeroize: jiff's civil::Date and Timestamp, and rust_decimal's Decimal. KeysetCipher::run with target::ore_block::<_, _, Borrowed>() compiles for each. The term it builds has the same left half as the term of value::Date, value::Timestamp, or value::Decimal built with encrypt_into_with_context. The CHANGELOG Breaking entry and the Orderable rustdoc in lib.rs now name that route. The amendment to architecture decision record 0004 now says that the method is removed.
The earlier text said the methods were removed because nothing tied a term to its ciphertext's context, and that most indexes send a query term unlike the term they store. Neither is the reason. A term does not have to share its ciphertext's context; a type that needs that ties the two together, as an EQL type does with its one context field. And of the indexes behind these methods, only match sends a different query term. The reason is that a query must be built in the same domain as the stored term it searches. A field reference does this from the field's own context and options, and the removed methods copied their target's code without that tie. The CHANGELOG entry, the ADR-0004 amendment and the sem docs now say so.
coderdan
left a comment
There was a problem hiding this comment.
Humanpass. LGTM.
Summary
Five
KeysetCiphermethods are removed:equality_term,match_terms,ore_term,ope_term, andore_block_term. Each method built one search term on its own, outside the target path and the query path, and copied its target's code.A query must be built in the same domain as the stored term it searches. A field reference does this from the field's own context and options. A query built with one of these methods was not tied to the field. Not all query terms are the same as the stored terms: with short words on, the
match_termsfilter of "Mike Ng" misses "Mike Nguyen".Apps now build a stored term by encrypting the value into its term type, and a query from the field reference. A term does not have to share its ciphertext's context. A type that needs that ties the two together, as an EQL type does. No stored bytes change.
Changes
Term::encrypt_from. Thedynamicpath keeps two helpers of its own:equality_term_normalizedandmatch_terms_under. Both are nowcfg(feature = "dynamic"). That replaces anallow(dead_code).dynamictests indynamic/term.rsanddynamic/record.rscompare each dynamic term with the typed term fromencrypt_into_with_context, so each comparison still uses two separate paths.encrypt_into_with_contextorKeysetCipher::run. A query comes from a field reference ortarget::matching_query.text_eq.rsand the Go guest's native testsnative_ops.rsuse the same public paths.EqualityTermdocs take the normalisation rules fromequality_term. ADR-0004 decision 5 has an amendment dated 23 November 2026.OreTerm<T>andOpeTerm<T>cannot take jiff dates, jiff timestamps, orrust_decimalvalues. For those, the entry gives two routes that compile. Convert tovalue::Date,value::Timestamp, orvalue::Decimal. Or runtarget::opeortarget::orewith a borrowed value.Verification
I ran each of these on the branch, and each passed:
cargo fmt --check, andcargo clippy --all-targets -- -D warningswith all features, with no default features, and with the default features.cargo test --all-featurespassed 1,676 tests, includingtests/ui.cargo test --no-default-featurespassed 1,071 tests.RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features.term_bytes.rsandfrozen_bytes.rspass with the same expected bytes.cargo test -p eql-encryption-testsandcargo clippy -p eql-encryption-tests --all-targets -- -D warnings.cargo test(32 tests innative_ops.rs) andcargo clippy --all-targets -- -D warnings. I rebuilt all four stack-encrypt guests and the auth guest, then rango test ./...andgo generate ./...inlanguages/golang. Both passed, andgo generatechanged no file.cargo mutants --in-diffon this commit's diff found no mutants. The changed library lines are docs,cfgattributes, and test-only code.I did not run the SQLx suite. That suite needs a database, and this change does not touch it.
Related
Closes #1760. Follows #1753, now merged. That pull request changed the match query path.
Review notes
Term::encrypt_from. The issue says thedynamicpath keeps using them. In fact only its tests did. Thedynamiclibrary code usesequality_term_normalized,match_terms_under, andorder_pending.ore_block_termis removed too. The issue did not name it, but it also built a term outside a target. A block order-revealing encryption (ORE) query term is the full ciphertext, the same as the stored one, so the query problem does not apply. The context problem does.target::ore_blockwith a borrowed value takes jiff dates, jiff timestamps, andrust_decimalvalues, astarget::opedoes. A block ORE term contains a random nonce. So a test that compares two block ORE terms built separately uses the scheme's comparison or the left half of each term.String, not a&str, becauseEncryptIntoneedsZeroize. Tests that passed a&strnow pass aString. The CHANGELOG says so.target.rsequality_leaf_agrees_with_the_descriptor_api. It compared the target with a method that no longer exists.match_short_words.rskeeps the stored cut for its query probes on purpose. The control query "ab cd" needs its two word tokens.