Skip to content

stack-encrypt cannot run as end users without a lazily built cipher and a task-local — defer the default keyset load and take the JWT per operation #1708

Description

@coderdan

Background

Stack Encrypt (packages/stack-encrypt) is the Rust crate that encrypts and decrypts values. For each value it asks ZeroKMS, the CipherStash key service, for a data key. Every request to ZeroKMS carries a service token, which an auth strategy from Stack Auth (packages/stack-auth) obtains.

Most strategies give the program its own identity: an access key, or the login saved by stash auth login. OidcFederationStrategy is different. It makes requests as an end user: it exchanges that user's JWT (a signed login token from an identity provider such as Supabase Auth or Clerk) for a CipherStash token naming that user. A web server uses it so that each decryption is made, and recorded, as the person who signed in.

A keyset is the group of keys a value is encrypted under. A cipher has a default keyset, and holds that keyset's index key, which is used to compute search terms.

Problem

A server that decrypts as its end users has to work around Stack Encrypt in two places. Both come from the same assumption: that the cipher has one identity, known when it is built.

1. Building the cipher needs a token, and at startup there is no user.

StackCipherBuilder::init() loads the default keyset straight away (packages/stack-encrypt/src/cipher.rs:1025, calling load_default_keyset at :812). That is a request to ZeroKMS, so it needs a token. With OidcFederationStrategy, a token only exists once a user's JWT is present. A server starting up has none, so init() fails.

The workaround is to build the cipher lazily, inside the first request, and keep it. That means a lock or a once-cell in every application, and an error path that appears on a user's request instead of at startup.

For strategies with their own identity this eager load is useful and deliberate: bad credentials fail at startup. The Go SDK's NewClient documents the same behaviour (languages/golang/encrypt/client.go:80).

2. An operation cannot say whose JWT to use.

The strategy learns the JWT from a callback, OidcProvider::fetch, which takes no arguments. Stack KMS calls the strategy from inside the ZeroKMS client (packages/stack-kms/src/client.rs:463, (&self.credentials).get_token()), with no request in hand. One cipher is shared by all requests, so the callback must find "the current user" from ambient state. In Rust that means a task-local variable: the request handler sets it, and the callback reads it.

So the application code that picks the identity is far from the code that uses it:

  • a task-local declared in one place,
  • a provider that reads it, given to the strategy at startup,
  • a scope(..) call wrapped around each operation in the handler.

Nothing ties these together. An operation that runs outside the scope (a spawned task, a background job) fails with "not authenticated", or worse, a provider with a fallback runs it as the wrong user.

OidcFederationStrategy::get_token_for_jwt(jwt) already exists (packages/stack-auth/src/oidc_federation_strategy.rs:367). It takes the JWT directly and shares the per-JWT token cache. It was added for the language bindings, which hit the same problem. But a cipher never calls it: there is no path from an operation to it.

Impact. Every Rust server that uses OIDC federation: the main case for identity-aware encryption. A demo web app on the coderdan/secrets-demo branch needed five wrapper types across three crates to hide the task-local and the lazy build, and the result was hard to follow.

Proposal

  1. Defer the default keyset load when the strategy has no identity of its own.

    • A strategy says whether it can issue a token with no caller present. Suggested shape: one method on the auth strategy trait with a default of "yes", so existing and custom strategies keep today's behaviour. OidcFederationStrategy answers "no".
    • init() loads the default keyset eagerly when the answer is "yes", exactly as today, so an access key or a saved login still fails fast.
    • When the answer is "no", init() makes no request. The default keyset loads on the first operation that needs it, and is then cached.
    • Document that init() checks credentials for one kind of strategy and not the other. Consider an explicit "check now" method for callers who hold a JWT at startup, such as a command-line tool.
  2. Let an operation carry the caller's JWT, for a strategy that supports it. For example:

    // once, at startup: no request is made
    let cipher = StackCipher::builder()
        .auth_strategy(OidcFederationStrategy::new(crn, /* no provider needed */)?)
        .init()
        .await?;
    
    // per request
    let value = cipher
        .default_keyset()
        .as_user(jwt)
        .decrypt_with_context(ciphertext, context)
        .await?;
    • The JWT travels with the operation to the key request, and the ZeroKMS client asks the strategy for the token for that JWT (what get_token_for_jwt does today).
    • It must be available only for a strategy that supports it. Calling it with an access-key strategy should not compile, or should fail with a clear error, never be silently ignored.
    • The existing callback path keeps working.
    • The names and the type-level shape are for the implementer to settle.
  3. Tests with no network: a fake strategy that records which JWT each key request was made for; two users interleaved on one cipher each get their own token; a cipher built with a caller-dependent strategy makes no request at init(); an access-key cipher still fails at init() on bad credentials.

Step 1 can ship without step 2 and already removes the lazy build from applications. Step 2 removes the task-local.

To decide, not solved here: with step 1 the default keyset's index key is still loaded once, as whichever user arrives first, and reused for everyone. Data key requests are made as each user; that one index key load is not. Whether index keys should be loaded per user is a separate question about what the audit trail must show for searchable fields.

Evidence

Checked on next at 3aedcd1eb:

  • packages/stack-encrypt/src/cipher.rs:1024-1025 — init() calls load_default_keyset(&self.kms).await? before returning a cipher.
  • packages/stack-auth/src/lib.rs:320 — AuthStrategy::get_token(self) takes no caller.
  • packages/stack-kms/src/client.rs:463 — the only way the client asks for a token.
  • packages/stack-auth/src/oidc_federation_strategy.rs:367 — get_token_for_jwt exists and is public.

Verified by running: a cipher built with OidcFederationStrategy on the first request of a web app, with the JWT in a task-local, decrypts as each signed-in user against live ZeroKMS. Not verified: how much of the Go SDK and its WebAssembly guest would need to change for step 2.

Relationship to other work

Activity

  1. added
    enhancement
    rust
    on Nov 18, 2026
  2. self-assigned this
    on Nov 18, 2026
  3. added 2 commits that reference this issue on Nov 18, 2026
    9ed27a3
    eb8ff9d
  4. coderdan commented on Nov 19, 2026

    @coderdan
    ContributorAuthor

    A third problem has the same cause. It was found in the review of #1701: #1701 (comment)

    3. A user's grant on a keyset does not limit the query terms they can make.

    The cipher caches each keyset's index key once, for every user of the cipher. StackCipher::keyset and keyset_state (packages/stack-encrypt/src/cipher.rs) return a cached keyset without asking ZeroKMS. So with a per-user strategy:

    • user A selects keyset X, and the cipher loads X's index key with A's token;
    • user B, who has no grant on X, selects X later and gets the cached keyset;
    • B can now make equality and order query terms under X, and so match X's stored values.

    B still cannot encrypt or decrypt under X. The cipher requests data keys with each user's own token, and ZeroKMS refuses B's request.

    The default keyset is the same case. init loads its index key once, with the token of whoever called init.

    #1701 documents this in auth_strategy, StackCipher::keyset and the crate docs. It does not change the behaviour. Today, a caller who needs grants to limit queries must build one cipher for each user.

    The fix belongs with this issue. Once an operation carries the caller's JWT (point 2), the cipher knows who the caller is. Either of these then works:

    • Cache index keys per caller, keyed by the subject of the caller's token. Each user loads a keyset's index key once, with their own grant.
    • Check the grant on a cache hit for a strategy with no identity of its own. That is one request to ZeroKMS for each selection, so it needs its own short-lived cache.

    A strategy with its own identity, such as an access key or a saved login, has one caller, so it keeps the shared cache as it is today.

  5. added 2 commits that reference this issue on Nov 20, 2026
    2f1c781
    574ddf0
  6. 20 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

SDKenhancementrust

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions