Repository navigation
Description
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
-
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.
OidcFederationStrategyanswers "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.
- 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.
-
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_jwtdoes 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.
- The JWT travels with the operation to the key request, and the ZeroKMS client asks the strategy for the token for that JWT (what
-
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 atinit()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()callsload_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_jwtexists 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
- feat(stack-encrypt): name the auth strategy and the key provider on StackCipherBuilder #1701 (closes
StackCiphercannot take an auth strategy without re-implementing client key loading #1700) addsStackCipherBuilder::auth_strategy. It documents the eager load as a known limitation and defers it. This issue is that follow-up. stack-encryptcannot send an unverified context to ZeroKMS, so the audit log cannot say why a key was requested #1705 adds a per-call unverified context. A per-call JWT travels the same route from the operation to the key request, so the two should share one mechanism for per-call options.@cipherstash/stacknative entry runsgetJwtoutside the caller's request, so a JWT read fromAsyncLocalStorageis lost — take the JWT per call #1565 is the same need in the TypeScript package: itsgetJwtcallback runs outside the caller's request, and the fix proposed there is also to take the JWT per call.
Activity
feat(stack-encrypt): take a credential per call and defer the first r…
docs(stack-encrypt): say that grants do not limit a cached keyset's q…
coderdan commentedon Nov 19, 2026
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::keysetandkeyset_state(packages/stack-encrypt/src/cipher.rs) return a cached keyset without asking ZeroKMS. So with a per-user strategy: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.
initloads its index key once, with the token of whoever calledinit.#1701 documents this in
auth_strategy,StackCipher::keysetand 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:
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.
docs(stack-encrypt): say that grants do not limit a cached keyset's q…
feat(stack-encrypt): take a credential per call and defer the first r…
feat(stack-encrypt): init_deferred after auth_strategy
feat(stack-encrypt)!: take a credential per call, and load the defaul…
feat(stack-encrypt)!: take a credential per call, and load the defaul…
feat(stack-encrypt)!: take a credential per call, and load the defaul…
20 remaining items
feat(stack-auth)!: take the JWT of a call from the call, with `Creden…
feat(stack-kms): make key requests with a credential, with `Credentia…
feat(stack-encrypt)!: attach a credential to a call, and load a keyse…
feat(stack-kms)!: stop a key source type from being both kinds, with …
feat(stack-auth)!: take the JWT of a call from the call, with `Creden…
feat(stack-kms): make key requests with a credential, with `Credentia…
feat(stack-encrypt)!: attach a credential to a call, and load a keyse…
feat(stack-kms)!: stop a key source type from being both kinds, with …
feat(stack-auth)!: take the JWT of a call from the call, with `Creden…
feat(stack-kms): make key requests with a credential, with `Credentia…
feat(stack-encrypt)!: attach a credential to a call, and load a keyse…
feat(stack-kms)!: stop a key source type from being both kinds, with …
feat(stack-kms)!: stop a key source type from being both kinds, with …