Skip to content

Commit 16a9c0a

Browse files
committed
refactor(stack-encrypt)!: hide ExpectedContext from the API
ExpectedContext is the check that a record with a context field runs on decrypt. The caller never needs to name it: the paired entry points take the NonEmpty<T> the caller expects, or nothing, and convert it. The plan for issue 1488 says it stops being a public type. It cannot be private to the crate. The derives emit it as the decrypt input of a context-field record in the caller's crate, and the EQL JSON document names it in its impl. So it is now hidden from the documentation, with a note that says why it is still reachable. ValuePlan::decryption_with_context took an ExpectedContext<C>. It now takes an Option<NonEmpty<C>>, so its signature names no hidden type. The tests that named the type pass a NonEmpty or () through Into.
1 parent a51efdc commit 16a9c0a

13 files changed

Lines changed: 57 additions & 42 deletions

File tree

‎docs/plans/2026-10-24-stack-encrypt-api-plan.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,10 @@ records only the work.
17391739
and with it every chain state and its `.context(..)`, `.extend(..)`,
17401740
and `.keyset(..)`. A stored part passed on decrypt is checked against
17411741
the row before any key is fetched. That check is what `ExpectedContext`
1742-
does today. It stops being a public type.
1742+
does today. It stops being a public type. Done in the third pull
1743+
request for #1488: it is hidden from the documentation, and is public
1744+
only so that code the derives emit can name it. A caller passes a
1745+
`NonEmpty<T>` or nothing.
17431746
- **Why.** The typed API already pairs its entry points (#1404). A value
17441747
for the context reads the same on both layers. A builder state for the
17451748
context, and a part added later in a chain, are the mistakes the context

‎packages/eql/tests/encryption/tests/json_tree_data_plan.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use eql_bindings::Identifier;
2828
use serde_json::{json, Value};
2929
use stack_encrypt::dynamic::{self, record, TargetError, ValueFrame};
3030
use stack_encrypt::json::{JsonDocument, JsonOptions, JsonQuery};
31-
use stack_encrypt::target::{ExpectedContext, Layout};
31+
use stack_encrypt::target::Layout;
3232
use stack_encrypt::{CipherText, Encrypt, KeysetCipher, Label, NonEmpty};
3333

3434
/// The options the documents are sealed under on both paths.
@@ -231,7 +231,7 @@ async fn the_data_plan_and_the_typed_layout_agree() {
231231

232232
// The typed path opens the data plan's document...
233233
let opened: Value = cipher
234-
.decrypt_as::<Value, JsonTreeDocument>(data, ExpectedContext::from(column()))
234+
.decrypt_as::<Value, JsonTreeDocument>(data, column().into())
235235
.await
236236
.unwrap();
237237
assert_eq!(opened, document);

‎packages/stack-encrypt-derive/docs/attributes.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ terms do not match new query terms either. Re-encrypt that data.
7676

7777
| Record | Encryption needs | Decryption needs |
7878
|---|---|---|
79-
| With a `context_field` of type `T` | A `NonEmpty<T>`, which the record stores in that field | An `ExpectedContext<T>`. See [Check where a record with a `context_field` came from](#check-where-a-record-with-a-context_field-came-from) |
79+
| With a `context_field` of type `T` | A `NonEmpty<T>`, which the record stores in that field | Nothing, or the `NonEmpty<T>` the caller expects. See [Check where a record with a `context_field` came from](#check-where-a-record-with-a-context_field-came-from) |
8080
| `struct = ..` | No context from the caller. Each field has its own | The same |
8181
| `plaintext = ..` | A `CallerContext`, built from a `NonEmpty<T>` or an integer | The same |
8282

@@ -108,7 +108,7 @@ pass one, or must not pass one. Its `ContextInput` type says which:
108108
|---|---|---|---|
109109
| `struct = User, scope = "users"` | `DeclaredScopes`, which takes no context from the caller | The same as for encryption | `user.encrypt_into(&keyset)` |
110110
| `plaintext = T` | `RequiredContext<CallerContext>`, or `RequiredContext<AeadContext>` with `context_type = AeadContext` | The same as for encryption | `value.encrypt_into_with_context(&keyset, nonempty!("users/email"))` |
111-
| With a `context_field` of type `C` | `RequiredContext<NonEmpty<C>>` | `ExpectedContext<C>` | `T::decrypt_from_with_context(record, &cipher, nonempty!(..))`, which checks the stored context before it decrypts |
111+
| With a `context_field` of type `C` | `RequiredContext<NonEmpty<C>>` | Nothing, or the `NonEmpty<C>` the caller expects | `T::decrypt_from_with_context(record, &cipher, nonempty!(..))`, which checks the stored context before it decrypts |
112112

113113
#### Check where a record with a `context_field` came from
114114

‎packages/stack-encrypt/CHANGELOG.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8989

9090
### Breaking
9191

92+
- **`target::ExpectedContext` is no longer part of the API** (#1488). It
93+
is the check that a `#[stash(context_field)]` record runs on decrypt:
94+
the stored context must equal the one the caller expects, if the caller
95+
names one. It is now hidden from the documentation. It stays public only
96+
so that code the derives emit can name it.
97+
- To upgrade, do not name the type. Pass the `NonEmpty<T>` you expect,
98+
or `()` for no check, and let `Into` convert it:
99+
`cipher.decrypt_as(record, expected.into())`, or
100+
`T::decrypt_from_with_context(record, &cipher, expected)`.
101+
- `ValuePlan::decryption_with_context` takes an `Option<NonEmpty<C>>`
102+
in place of an `ExpectedContext<C>`. `None` checks only that the
103+
stored context is not empty.
104+
92105
- **A data plan refuses an order index on bytes** (#1577).
93106
`dynamic::admits` no longer admits `ore`, `ope` or `ore_block` on a
94107
`bytes` field, so `record::plan` refuses the declaration with

‎packages/stack-encrypt/src/cipher.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ pub enum Error {
296296
#[diagnostic(code(stack_encrypt::unsupported_shape))]
297297
UnsupportedShape,
298298
/// A record carrying its context in storage (`#[stash(context_field)]`)
299-
/// was opened with an [`ExpectedContext`](crate::target::ExpectedContext)
300-
/// naming a different one. Refused before any key is retrieved.
299+
/// was opened with an expected context that differs from the stored
300+
/// one. Refused before any key is retrieved.
301301
///
302302
/// `stored` is the stored context's descriptor, rendered as ZeroKMS
303303
/// would log it, for a caller in this process. The message gives only

‎packages/stack-encrypt/src/plan/build.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ impl<S: 'static, K: 'static> FieldsBuilder<S, K> {
667667
/// context field says otherwise with
668668
/// [`Error::ContextMismatch`](crate::Error::ContextMismatch), before any
669669
/// key is requested. That is the derive's `#[stash(context_field)]`
670-
/// check ([`ExpectedContext`]). A context field changed in storage
670+
/// check. A context field changed in storage
671671
/// opens nothing either way: every field was sealed under the original.
672672
///
673673
/// The field's value must be a plain label (`"tenants/acme"`), read

‎packages/stack-encrypt/src/plan/value.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ impl<S> ValueStart<S> {
193193
/// is: what protects the record is that the target was sealed under it.
194194
/// When the record is opened
195195
/// ([`decryption_with_context`](ValuePlan::decryption_with_context)), the
196-
/// stored context is checked first against the one the caller expects
197-
/// ([`ExpectedContext`]): a mismatch is
196+
/// stored context is checked first against the one the caller expects,
197+
/// if the caller names one: a mismatch is
198198
/// [`Error::ContextMismatch`](crate::Error::ContextMismatch), before any
199199
/// key is requested.
200200
///
@@ -319,13 +319,13 @@ impl<S, C, T> ValuePlan<S, Stored<C, T>> {
319319
}
320320

321321
/// The opening of a stored `(C, T)`: the stored context checked against
322-
/// `expected` (which [`ExpectedContext::default`] leaves unchecked beyond
323-
/// being nonempty), before any key is requested, then `T` opened under
324-
/// it through its own [`DecryptInto`].
322+
/// `expected` (`None` leaves it unchecked beyond being nonempty), before
323+
/// any key is requested, then `T` opened under it through its own
324+
/// [`DecryptInto`].
325325
pub fn decryption_with_context<K: 'static>(
326326
&self,
327327
record: (C, T),
328-
expected: ExpectedContext<C>,
328+
expected: Option<NonEmpty<C>>,
329329
) -> Decryption<S, K>
330330
where
331331
S: 'static,
@@ -334,6 +334,7 @@ impl<S, C, T> ValuePlan<S, Stored<C, T>> {
334334
C: MaybeEmpty + PartialEq + IntoContext<'static>,
335335
{
336336
let (stored, target) = record;
337+
let expected = expected.map_or_else(ExpectedContext::default, ExpectedContext::from);
337338
match expected.validate(stored) {
338339
Ok(context) => target.decryption(context.into()),
339340
Err(error) => Decryption::failed(error),

‎packages/stack-encrypt/src/target/input.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! | [`RequiredContext<C>`] | Refused | `C` | The caller supplies the context, or the part of it that the declaration selects. |
77
//! | [`OptionalContext<C>`] | Allowed | `C` | For an opaque scalar. A context left out is the empty context. |
88
//! | [`DeclaredScopes`] | Allowed | Refused | A derived `struct = ..` record. Each field has its own context. A record that contains it, or a plan, passes that field's context. |
9-
//! | [`ExpectedContext<T>`](super::ExpectedContext) | Allowed | `NonEmpty<T>` | A context left out opens under the stored context, unchecked. A context passed is checked against the stored one. |
9+
//! | The decrypt input of a `#[stash(context_field)]` record | Allowed | `NonEmpty<T>` | A context left out opens under the stored context, unchecked. A context passed is checked against the stored one. |
1010
//!
1111
//! A trait bound decides whether a method is available ([`AllowsNoContext`]
1212
//! or [`AcceptsContext`]). It is never inferred from `C: Default`. For
@@ -200,8 +200,7 @@ impl<C: Into<CallerContext>> From<RequiredContext<C>> for CallerContext {
200200
/// expects, or none.
201201
///
202202
/// - If the caller passes a `NonEmpty<T>`, the stored context must equal it. A
203-
/// different stored context is refused before any key is retrieved
204-
/// ([`ExpectedContext::validate`]).
203+
/// different stored context is refused before any key is retrieved.
205204
/// - If the caller leaves the context out, the record opens under whatever
206205
/// context it stores, after a check that the context is not empty.
207206
///

‎packages/stack-encrypt/src/target/mod.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
//! the one context the caller supplies. That context is a
6565
//! [`RequiredContext<CallerContext>`], or a `RequiredContext<AeadContext>`
6666
//! with `context_type = AeadContext`. A `#[stash(context_field)]` record
67-
//! requires the `NonEmpty<T>` it stores, and opens under an
68-
//! [`ExpectedContext`].
67+
//! requires the `NonEmpty<T>` it stores. On decrypt the caller may pass that
68+
//! `NonEmpty<T>` again, and the stored value must then equal it.
6969
//!
7070
//! Both derives also implement the structural traits ([`EncryptFrom`],
7171
//! [`DecryptInto`] and [`DecryptField`]). So an owned record can be a field
@@ -92,7 +92,7 @@
9292
//!
9393
//! The derives compose each semantic field's declaration. `#[stash(context_field)]`
9494
//! on an identifier of type `T` makes the encryption context `NonEmpty<T>` and
95-
//! stores its inner value. Opening takes an [`ExpectedContext`]: by default it checks only that the stored identifier is nonempty and opens under it as stored; a `NonEmpty<T>` also requires it to equal the destination the caller names, and a mismatch is refused before any key is retrieved.
95+
//! stores its inner value. Opening takes the context the caller expects, or none: by default it checks only that the stored identifier is nonempty and opens under it as stored; a `NonEmpty<T>` also requires it to equal the destination the caller names, and a mismatch is refused before any key is retrieved.
9696
//! Record envelope names do not add cryptographic map keys.
9797
//!
9898
//! Generic targets use [`CallerContext`] (ciphertext and terms) or [`AeadContext`]
@@ -184,9 +184,15 @@ pub mod transcode;
184184
mod tuples;
185185

186186
pub(crate) use self::core::{bind_pending, seal_pending};
187+
/// The check a `#[stash(context_field)]` record runs on decrypt. It is not
188+
/// part of the API: the derives and the EQL types name it, and a caller
189+
/// passes a `NonEmpty<T>` or nothing. It is public only so that code the
190+
/// derives emit in another crate can name it.
191+
#[doc(hidden)]
192+
pub use context::ExpectedContext;
187193
pub use context::{
188-
AeadContext, Bind, BindPart, CallerContext, CheckedInput, DeclaredContext, ExpectedContext,
189-
Extends, NoBind, NoScope, Parts, Scope, ScopePart, StoredParts,
194+
AeadContext, Bind, BindPart, CallerContext, CheckedInput, DeclaredContext, Extends, NoBind,
195+
NoScope, Parts, Scope, ScopePart, StoredParts,
190196
};
191197
pub use index::{
192198
indexed, At, Encrypted, Equality, Index, IndexSpec, Indexes, Json, Match, Ope, Ore, OreBlock,

‎packages/stack-encrypt/src/target/operations.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ pub trait EncryptFrom<S>: Sized + 'static {
6666
/// ciphertext and its context, and [`open`] describes the rest. A query-only
6767
/// target (terms alone) has no implementation.
6868
pub trait DecryptInto<P>: Sized {
69-
/// What a caller supplies to open the target. For a record that stores
70-
/// its context this is an [`ExpectedContext`](super::ExpectedContext),
71-
/// which may name the destination the caller believes it is opening.
69+
/// What a caller supplies to open the target. A record that stores its
70+
/// context converts from `()` or from the `NonEmpty<T>` the caller
71+
/// believes it is opening.
7272
type Context;
7373
/// The description the cipher executes to recover `P`.
7474
fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<P, K>;

0 commit comments

Comments
 (0)