Skip to content

feat(stack-kms)!: stop a key source type from being both kinds, with KeySourceKind - #1738

Merged
coderdan merged 1 commit into
nextfrom
feat/stack-kms-key-source-kind
Nov 22, 2026
Merged

coderdan merged 1 commit into
nextfrom
feat/stack-kms-key-source-kind

Conversation

@coderdan

@coderdan coderdan commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR was stacked on #1709, which is now merged into next. It is one commit on top of next.

A key source is what a Stack Encrypt cipher asks for keys. There are two kinds. One has an identity of its own, such as an access key, and makes every request as itself. The other takes a credential with each call, such as an end user's JWT, and makes each request as that caller.

The docs of stack-kms said a key source is one kind and never both. Nothing checked it. This PR makes a key source of both kinds a compile error.

Why it matters: with a key source of both kinds, a request handler that forgot .with_credential(..) still compiled, and its call was made as the key source itself, not as the user. #1709 closed that for real ZeroKMS clients. This closes it for a key source that someone writes by hand, such as a test double.

The problem, in code

On #1709 this compiles:

struct Both { /* .. */ }
impl DataKeySource for Both { /* requests as itself */ }
impl IndexKeySource for Both { /* .. */ }
impl CredentialKeySource for Both { /* requests as the caller */ }

let cipher = StackCipher::builder().kms(both).init::<OwnIdentity>().await?;
let keyset = cipher.default_keyset();

keyset.encrypt(1i64).await?;                             // made as the source itself
keyset.encrypt(1i64).with_credential(alice_jwt).await?;  // made as Alice

The first encrypt is the forgotten credential. I ran this as a test against #1709, and it passed.

The change

stack-kms has a new trait, KeySourceKind. A key source states its kind once, and each key source trait requires its own kind:

pub trait KeySourceKind {
    type Kind; // OwnIdentity or CredentialPerCall
}

pub trait DataKeySource:       KeySourceKind<Kind = OwnIdentity>       { /* as before */ }
pub trait IndexKeySource:      KeySourceKind<Kind = OwnIdentity>       { /* as before */ }
pub trait CredentialKeySource: KeySourceKind<Kind = CredentialPerCall> { /* as before */ }

A type implements KeySourceKind once, so it has one Kind. Both now fails:

error[E0271]: type mismatch resolving `<Both as KeySourceKind>::Kind == CredentialPerCall`
   | impl CredentialKeySource for Both {
note: expected this to be `CredentialPerCall`
   |     type Kind = OwnIdentity;

StackKms, the real ZeroKMS client, does not choose its kind. It takes the kind of its auth strategy, from stack_auth::StrategyKind (added in #1709):

impl<C, Conn> KeySourceKind for StackKms<C, Conn>
where
    C: stack_auth::AuthStrategyBounds,
    &'static C: stack_auth::StrategyKind,
{
    type Kind = <&'static C as stack_auth::StrategyKind>::Kind;
}

Changes

stack-kms

  • KeySourceKind, and the three key source traits require it.
  • StackKms takes its kind from its auth strategy.
  • FakeDataKeySource, DeterministicSource and FakeCredentialKeySource state their kinds.
  • Re-exports CredentialPerCall beside OwnIdentity, so a key source needs no direct dependency on stack-auth to state its kind.
  • A compiler message of its own for a key source that states no kind.

Every other key source in the repository

  • One new impl each, and nothing else: 16 test doubles in stack-encrypt, 2 in the EQL encryption tests, 2 in the Go guest's tests.

Docs

  • The stack-kms and stack-encrypt changelogs list the change under Breaking, with the impl to add.

Breaking change

A key source written outside stack-kms needs one new impl:

impl stack_kms::KeySourceKind for MyKeySource {
    type Kind = stack_kms::OwnIdentity; // or CredentialPerCall
}

Without it the compiler says "MyKeySource does not say which kind of key source it is", and names the impl for each kind.

A cipher built with auth_strategy(..), or over StackKms or the test doubles of stack-kms, needs no change.

stack-auth's StrategyKind did not need this from existing strategies, because one blanket impl covers every AuthStrategy. Here the kind with an identity of its own is two traits (DataKeySource and IndexKeySource), and Rust allows a blanket impl over only one of them.

Verification

#1687 and #1709 are merged into next, and this PR is rebased onto next: the head is 2cf6e1e28, on the merge commit of #1709 (271b16f49). Neither merge changed a file of next beyond the PR's own commits, so every file at the head is the same as at 81aaaa192 and at 3052a7ec4, two earlier pushes (git diff between them is empty). The local runs below are from 3052a7ec4 and were not repeated. CI passed on the head, 2cf6e1e28: 50 checks passed, 7 skipped. The skipped checks are the TypeScript test jobs and the long stack-auth soak, which this PR's files do not reach, the nightly fuzz campaign, and the review job.

Local runs on 3052a7ec4, which sat on #1709 at 39908044b and #1687 at fa2a15849:

  • cargo nextest run -p stack-auth -p stack-kms -p stack-encrypt -p stack-encrypt-derive -p stack-encrypt-jsonpath -p stack-auth-node -p stack-auth-wasm --all-features, which includes the compile tests and the tests that watch freed memory for plaintext: 2051 passed, 2 failed, 2 skipped. The two failures are stack-auth's test_poll_for_token_clamps_an_oversized_device_code_lifetime and test_poll_for_token_clamps_an_oversized_token_lifetime. They are not caused by this PR: they fail the same way on feat(stack-encrypt,eql-bindings): typed JSON queries from a field reference #1687's head with neither this PR nor feat(stack-encrypt)!: attach a credential to a call, and load a keyset when a call needs it #1709 applied (7 of 8 runs of the two tests), and neither PR changes the device-code files. They passed in every earlier full run here and in CI on 3052a7ec4, and the machine was under heavy load from other builds during this run. The compiler is the list of key sources that need the new impl, and it found none missing.
  • cargo clippy --workspace --all-features --all-targets -- -D warnings and cargo fmt --all -- --check: clean.
  • Doc tests with every feature on: stack-kms 17 passed, stack-encrypt 81. Two of stack-kms's are new: a key source of your own, and a key source of both kinds, which must not compile.
  • EQL: cargo test -p eql-bindings -p eql-encryption-tests --all-features: 414 passed, 64 ignored. The ignored tests need a PostgreSQL database.

Local runs on e8a057444, two pushes before 3052a7ec4, before #1687 gained three commits that change none of this PR's files:

  • Doc tests of stack-auth: 30 passed.
  • cargo test --no-default-features: stack-kms 106 passed, stack-encrypt 1008 passed.
  • cargo doc with warnings as errors, mise run wasm:wasi-check, and cargo check -p stack-auth-wasm --target wasm32-unknown-unknown: clean.
  • Go guest: cargo test --features eql on the native build of the encrypt guest, 77 passed. cargo check --target wasm32-wasip1 for the encrypt guest with eql and for the auth guest: clean.

Not run locally: the mutation gate, which covers stack-auth and stack-encrypt. This PR changes no code of either outside test modules and one doc comment. CI runs the gate, and it passed on the head.

Related

Refs #1708. Builds on #1709, merged.

Review notes

  • Read packages/stack-kms/src/key_source.rs first: the trait, the three supertraits, and the StackKms impl. The rest of the diff is the one-line impl repeated.
  • Stack Encrypt's code does not change. Its own KeySource<I> trait stays as it is. Only its test doubles and one doc sentence change.
  • KeySourceKind::Kind has no bound. A Kind other than the two is useless, because none of the three traits accepts it.
  • The rule is tested in stack-kms, by two doc tests on KeySourceKind: a key source of your own, and a key source of both kinds that must not compile. Stack Encrypt adds no test of it.
  • No changeset: no npm package ships stack-kms.

@changeset-bot

changeset-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2cf6e1e

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

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The KMS crate adds KeySourceKind to distinguish own-identity sources from per-call credential sources. The key-source traits require their corresponding kind, and built-in and test implementations now declare one.

Changes: Key-source kind contract

Layer / File(s) Summary
Define and export key-source kinds
packages/stack-kms/src/key_source.rs, packages/stack-kms/src/lib.rs, packages/stack-kms/CHANGELOG.md, packages/stack-encrypt/CHANGELOG.md, packages/stack-encrypt/src/credential.rs
KeySourceKind distinguishes OwnIdentity and CredentialPerCall. DataKeySource and IndexKeySource require OwnIdentity; CredentialKeySource requires CredentialPerCall. The crate exports the trait and credential marker, and the documentation describes the contract and migration.
Declare kinds on source implementations
languages/golang/encrypt/guest/tests/*, packages/eql/tests/encryption/tests/*, packages/stack-encrypt/src/dynamic/record.rs, packages/stack-encrypt/src/target/pending.rs, packages/stack-encrypt/tests/*
Test and support key sources now declare their associated kind. The declarations use OwnIdentity or, for credential-based sources, CredentialPerCall.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: claude, auxesis, freshtonic

Merge Risk: Minimal · up to 81aaa

The change adds a compile-time check so a key source cannot support both own-identity and per-call-credential requests. Built-in and test sources already declare their kind, and the breaking change is documented in the changelogs. No concrete merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a new KeySourceKind trait in stack-kms that stops one key source type from declaring both OwnIdentity and CredentialPerCall kinds. The changeset adds KeySourceKind, up…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: a new KeySourceKind trait in stack-kms that stops one key source type from declaring both OwnIdentity and CredentialPerCall kinds. The changeset adds KeySourceKind, updates DataKeySource, IndexKeySource and CredentialKeySource, re-exports the trait, updates test doubles, and documents the breaking change. The title marks the breaking change with '!' and the feat scope. It is specific and concise.

Recent review info

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: a14bd3e5-14dc-4a94-8078-22dbd0da53bf
Commits

Reviewing files that changed from the base of the PR and between 271b16f and 81aaaa1.

Files selected for processing: 21

  • languages/golang/encrypt/guest/tests/leak.rs
  • languages/golang/encrypt/guest/tests/native_ops.rs
  • packages/eql/tests/encryption/tests/common/mod.rs
  • packages/eql/tests/encryption/tests/heap_hygiene.rs
  • packages/stack-encrypt/CHANGELOG.md
  • packages/stack-encrypt/src/credential.rs
  • packages/stack-encrypt/src/dynamic/record.rs
  • packages/stack-encrypt/src/target/pending.rs
  • packages/stack-encrypt/tests/audit.rs
  • packages/stack-encrypt/tests/common/mod.rs
  • packages/stack-encrypt/tests/credential.rs
  • packages/stack-encrypt/tests/frozen_bytes.rs
  • packages/stack-encrypt/tests/heap_hygiene.rs
  • packages/stack-encrypt/tests/json_operators.rs
  • packages/stack-encrypt/tests/keysets.rs
  • packages/stack-encrypt/tests/lifecycle_conformance.rs
  • packages/stack-encrypt/tests/opaque_blob.rs
  • packages/stack-encrypt/tests/scalar.rs
  • packages/stack-kms/CHANGELOG.md
  • packages/stack-kms/src/key_source.rs
  • packages/stack-kms/src/lib.rs

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@github-actions

github-actions Bot commented Nov 21, 2026 •

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.

@coderdan
coderdan force-pushed the coderdan/stack-encrypt-cannot-run-as-end-users-without-a branch from b09754a to 7d3a79d Compare November 21, 2026 07:21
@coderdan
coderdan force-pushed the feat/stack-kms-key-source-kind branch from 7df19c0 to 2e6d3bc Compare November 21, 2026 07:21
@coderdan
coderdan force-pushed the coderdan/stack-encrypt-cannot-run-as-end-users-without-a branch 2 times, most recently from c6abfcc to 47b2cc3 Compare November 21, 2026 15:02
@coderdan
coderdan force-pushed the feat/stack-kms-key-source-kind branch from 2e6d3bc to 5c3a651 Compare November 21, 2026 15:14
@coderdan
coderdan force-pushed the coderdan/stack-encrypt-cannot-run-as-end-users-without-a branch from 47b2cc3 to 3990804 Compare November 22, 2026 01:38
@coderdan
coderdan force-pushed the feat/stack-kms-key-source-kind branch from 5c3a651 to 3052a7e Compare November 22, 2026 01:49
@coderdan
coderdan force-pushed the coderdan/stack-encrypt-cannot-run-as-end-users-without-a branch from 3990804 to 0156249 Compare November 22, 2026 02:34
@coderdan
coderdan force-pushed the feat/stack-kms-key-source-kind branch from 3052a7e to 81aaaa1 Compare November 22, 2026 02:34

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

Human pass. LGTM.

@coderdan
coderdan marked this pull request as ready for review November 22, 2026 03:10
@coderdan
coderdan requested a review from a team as a code owner November 22, 2026 03:10
Base automatically changed from coderdan/stack-encrypt-cannot-run-as-end-users-without-a to next November 22, 2026 03:12
…`KeySourceKind`

A key source makes every request as itself (`DataKeySource` and
`IndexKeySource`), or as the caller whose credential comes with the call
(`CredentialKeySource`). The docs said a type is one kind and never
both. Nothing checked it. A type that implemented all three traits
compiled, and Stack Encrypt then accepted it as both kinds: after
`init::<OwnIdentity>()`, a call through it could be awaited with no
credential, and was made as the key source itself. That is a forgotten
credential that compiles.

`KeySourceKind` is the check. A key source states its kind once:

    impl KeySourceKind for MyKeySource {
        type Kind = OwnIdentity;
    }

`DataKeySource` and `IndexKeySource` require `Kind = OwnIdentity`, and
`CredentialKeySource` requires `Kind = CredentialPerCall`. A type has
one `Kind`, so a key source of both kinds does not compile (E0271).

`StackKms` does not choose its kind. It is the kind of its auth
strategy, read from `stack_auth::StrategyKind`, so stack-auth decides
it.

Unlike `StrategyKind`, no blanket impl gives existing key sources their
kind. The kind with an identity of its own is two traits, and a blanket
impl can be written over only one. So every key source adds the impl
above. That is a breaking change for a key source written outside this
crate, most often a test double. The compiler names the missing impl.

The key sources in this repository each gain the impl: the three test
doubles of this crate, and the test doubles in stack-encrypt, the EQL
encryption tests and the Go guest. No behaviour changes.

Refs #1708
@coderdan
coderdan force-pushed the feat/stack-kms-key-source-kind branch from 81aaaa1 to 2cf6e1e Compare November 22, 2026 03:24
@coderdan
coderdan requested a balanced review from Copilot November 22, 2026 03:25

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 are recorded for the changes extending credential-safety guarantees to custom key sources.

No open issues 🎉

What changed in this PR

This PR extends #1709’s credential-safety guarantees to custom key sources. KeySourceKind makes implementing both own-identity and per-call-credential key-source traits a compile error, without changing Stack Encrypt’s runtime logic.

Changes:

  • Adds kind constraints, strategy-derived classification for StackKms, compiler guidance, and positive/compile-fail doctests.
  • Declares kinds for built-in sources and test doubles across Stack Encrypt, EQL, and the Go guest.
  • Documents the breaking change and the implementation custom key sources must add.
File Description
packages/​stack-kms/​src/​lib.rs Exports KeySourceKind and both kind markers.
packages/​stack-kms/​src/​key_source.rs Enforces exclusive kinds and adds doctests.
packages/​stack-kms/​CHANGELOG.md Documents the breaking change and migration.
packages/​stack-encrypt/​tests/​scalar.rs Classifies the fixed-key test source.
packages/​stack-encrypt/​tests/​opaque_blob.rs Classifies the opaque-blob test source.
packages/​stack-encrypt/​tests/​lifecycle_conformance.rs Classifies the lifecycle test source.
packages/​stack-encrypt/​tests/​keysets.rs Classifies both keyset test sources.
packages/​stack-encrypt/​tests/​json_operators.rs Marks the shared source as credential-per-call.
packages/​stack-encrypt/​tests/​heap_hygiene.rs Classifies memory-hygiene test sources.
packages/​stack-encrypt/​tests/​frozen_bytes.rs Classifies the oversized-tag test source.
packages/​stack-encrypt/​tests/​credential.rs Declares each credential test source’s kind.
packages/​stack-encrypt/​tests/​common/​mod.rs Classifies shared counting and recording sources.
packages/​stack-encrypt/​tests/​audit.rs Classifies the audit recording source.
packages/​stack-encrypt/​src/​target/​pending.rs Classifies a unit-test source only.
packages/​stack-encrypt/​src/​dynamic/​record.rs Classifies a unit-test source only.
packages/​stack-encrypt/​src/​credential.rs Clarifies the key-source contract in documentation.
packages/​stack-encrypt/​CHANGELOG.md Explains migration through the kms re-export.
packages/​eql/​tests/​encryption/​tests/​heap_hygiene.rs Classifies the stalling encryption test source.
packages/​eql/​tests/​encryption/​tests/​common/​mod.rs Classifies the shared recording source.
languages/​golang/​encrypt/​guest/​tests/​native_ops.rs Classifies the native-operation counting source.
languages/​golang/​encrypt/​guest/​tests/​leak.rs Classifies the failure-path test source.

Review effort: Balanced

@coderdan
coderdan merged commit 56bc8b6 into next Nov 22, 2026
58 checks passed
@coderdan
coderdan deleted the feat/stack-kms-key-source-kind branch November 22, 2026 03:46
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 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

2 participants