Skip to content

refactor(stack-encrypt)!: remove the lone term methods from the public API - #1766

Merged
coderdan merged 4 commits into
nextfrom
refactor/stack-encrypt-private-term-methods
Nov 23, 2026
Merged

coderdan merged 4 commits into
nextfrom
refactor/stack-encrypt-private-term-methods

Conversation

@coderdan

@coderdan coderdan commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Five KeysetCipher methods are removed: equality_term, match_terms, ore_term, ope_term, and ore_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_terms filter 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

  • stack-encrypt library. The five methods are removed. No library code called them, and each was a copy of its target's Term::encrypt_from. The dynamic path keeps two helpers of its own: equality_term_normalized and match_terms_under. Both are now cfg(feature = "dynamic"). That replaces an allow(dead_code).
  • The dynamic tests in dynamic/term.rs and dynamic/record.rs compare each dynamic term with the typed term from encrypt_into_with_context, so each comparison still uses two separate paths.
  • Integration tests in 15 stack-encrypt files now use the public paths. A stored term comes from encrypt_into_with_context or KeysetCipher::run. A query comes from a field reference or target::matching_query.
  • The EQL encryption test text_eq.rs and the Go guest's native tests native_ops.rs use the same public paths.
  • Docs. 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 dated 23 November 2026.
  • CHANGELOG. A Breaking entry gives the upgrade steps and an example. OreTerm<T> and OpeTerm<T> cannot take jiff dates, jiff timestamps, or rust_decimal values. For those, the entry gives two routes that compile. Convert to value::Date, value::Timestamp, or value::Decimal. Or run target::ope or target::ore with a borrowed value.

Verification

I ran each of these on the branch, and each passed:

  • stack-encrypt: cargo fmt --check, and cargo clippy --all-targets -- -D warnings with all features, with no default features, and with the default features.
  • stack-encrypt tests: cargo test --all-features passed 1,676 tests, including tests/ui. cargo test --no-default-features passed 1,071 tests.
  • stack-encrypt rustdoc: RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features.
  • The frozen byte pins in term_bytes.rs and frozen_bytes.rs pass with the same expected bytes.
  • EQL: cargo test -p eql-encryption-tests and cargo clippy -p eql-encryption-tests --all-targets -- -D warnings.
  • Go guest: cargo test (32 tests in native_ops.rs) and cargo clippy --all-targets -- -D warnings. I rebuilt all four stack-encrypt guests and the auth guest, then ran go test ./... and go generate ./... in languages/golang. Both passed, and go generate changed no file.
  • cargo mutants --in-diff on this commit's diff found no mutants. The changed library lines are docs, cfg attributes, 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

  • The methods are removed, not made crate-private. No library code called them, and each copied its target's Term::encrypt_from. The issue says the dynamic path keeps using them. In fact only its tests did. The dynamic library code uses equality_term_normalized, match_terms_under, and order_pending.
  • ore_block_term is 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_block with a borrowed value takes jiff dates, jiff timestamps, and rust_decimal values, as target::ope does. 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.
  • The target path takes text as a String, not a &str, because EncryptInto needs Zeroize. Tests that passed a &str now pass a String. The CHANGELOG says so.
  • One test was deleted: target.rs equality_leaf_agrees_with_the_descriptor_api. It compared the target with a method that no longer exists.
  • The calibration test in match_short_words.rs keeps the stored cut for its query probes on purpose. The control query "ab cd" needs its two word tokens.
  • ADR-0009 and ADR-0014 still name the methods in their historical text. I left those as records of the time.

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
@coderdan
coderdan requested a review from a team as a code owner November 23, 2026 02:18
@coderdan
coderdan requested a balanced review from Copilot November 23, 2026 02:18
@coderabbitai

coderabbitai Bot commented Nov 23, 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: fb296924-3491-4762-87b7-bdbe48bfa44a

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

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: ea7135c

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

@github-actions

Copy link
Copy Markdown
Contributor

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

No mutants were generated for the changed lines.

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.

🟢 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 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 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) minor ore_block_term stays public (acknowledged in the PR body). Hiding it later would not close the gap on its own: the public OreBlockTerm<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-Zeroize Orderable types, so the follow-up needs the same migration note as CHANGELOG.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

Comment thread packages/stack-encrypt/CHANGELOG.md Resolved
Comment thread packages/stack-encrypt/src/sem/mod.rs Resolved
Comment thread packages/stack-encrypt/src/sem/mod.rs Resolved
Comment thread packages/stack-encrypt/src/lib.rs Resolved
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.
@coderdan coderdan changed the title refactor(stack-encrypt)!: make the lone term methods crate-private refactor(stack-encrypt)!: remove the lone term methods from the public API Nov 23, 2026
@coderdan

Copy link
Copy Markdown
Contributor Author

Review 6015918939: the ADR-0009 pointer is added in 19f4519. The KeysetCipher::match_terms bullet in ADR-0009's #1398 amendment now ends: "ADR-0004's #1760 amendment removes the method from the public API." The ore_block_term note is left for a separate decision. Because the four methods are now removed rather than crate-private, the PR title and body are updated to match.

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 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.

Humanpass. LGTM.

@coderdan
coderdan merged commit e62420d into next Nov 23, 2026
58 checks passed
@coderdan
coderdan deleted the refactor/stack-encrypt-private-term-methods branch November 23, 2026 10:27
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: make the standalone term methods (equality_term, match_terms, ore_term, ope_term) crate-private

3 participants