Skip to content

stack-encrypt: a developer cannot see the context a call will use — show it, name the parameter context, and accept it by reference #1568

Closed

Description

@coderdan

Background

Stack Encrypt (packages/stack-encrypt) is the Rust engine that every CipherStash language SDK runs on. Each value it encrypts is sealed under a context. A context is a list of parts, such as a table name, a column name, a tenant ID, or a row ID. A value opens only under the same context. A query finds a value only if its search terms were derived under the same query context.

Problem

A wrong context loses data silently. A query under the wrong context matches nothing and gives no error. Today a developer cannot see the context a call will use. A test cannot state it either. The API around context also has two smaller problems:

  • Six public methods take the context through a parameter named aad. That name describes how the context is used inside the engine, not what the caller passes.
  • Every method takes the context by value. A program that uses one column context for a write and a query must clone it for each call.

Proposal

This issue covers three items in the Stack Encrypt API plan, merged in pull request #1365.

1. Show the context

The plan item is Show the context.

  • contexts() on a Plan lists each field's query context and stored context. Each part the caller must supply appears as a typed placeholder.
  • context() on a prepared call shows the exact contexts the call will use, with the caller's values filled in.
  • Both use the same code that derives the key descriptor and the AAD, so what a developer sees is what the engine uses.
  • The dynamic interface has both forms, so stashgen can print a Go record's contexts.
  • A testing feature adds assert_context! over a field marker, a plan, or a prepared call. It names the first part that differs. The Go module gets the same in a testing package, such as encrypttest.

2. Name context parameters context

The plan item is Name context parameters context. Rename the aad parameter to context on these methods in packages/stack-encrypt/src/cipher.rs:

  • KeysetCipher::encrypt_tree and KeysetCipher::decrypt_tree;
  • StackCipher::decrypt;
  • PendingStackCipherText::plan, seal, and into_pending.

Renaming Vitamin C's IntoAad trait is a follow-up in Vitamin C.

3. Pass context by reference

The plan item is Pass context by reference. Every method that takes a context also accepts a borrowed context, such as &Identifier. The request borrows the context for as long as it runs. eql-bindings needs IntoContext for &Identifier.

None of these changes the stored format.

Acceptance

Narrowed on 2026-11-10 to the declarations that exist on next. #1590 holds the rest: the scenarios that need scope(..) and bind(..), the field-marker form, and the per-scenario Go tests.

  • A test pins the output of contexts() for each declaration that exists today: a record with a static context (scenario 1), a record that stores its context, a nested record, a target that takes the caller's context, a collection, a plan under each of its context sources, a data plan, and an EQL type (scenario 8).
  • Each of those has an assert_context! test in Rust. A Go test pins a generated record with encrypttest.AssertContexts.
  • A conformance case shows that Rust and Go print the same context for the same declaration.
  • No public method has a parameter named aad.
  • A context passes by reference where the types allow it: &NonEmpty<T> for CallerContext, AeadContext, and DeclaredContext, and &Identifier as a context. A stored EQL type still takes its NonEmpty<Identifier> by value, because From<&NonEmpty<T>> for NonEmpty<T> belongs in Vitamin C.

Relationship to other work

Plans: the Stack Encrypt API plan.

Metadata

Metadata

Assignees

Labels

RustSDKenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions