Skip to content

Bind control-plane secrets to a key per kind and their context - #435

Merged
tnkuehne merged 5 commits into
feat/runtime-purpose-keysfrom
feat/control-plane-purpose-keys
Nov 27, 2026
Merged

tnkuehne merged 5 commits into
feat/runtime-purpose-keysfrom
feat/control-plane-purpose-keys

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #429. Every control-plane ciphertext (app secrets, the directory and app sign-in client secrets, SSO provider configurations) used its key version's 32 bytes directly. The SecretContext callers pass was not bound to the ciphertext, so a value copied to another row, app, organization or kind still decrypted.

Changes

  • Keys per kind (secret-encryption.ts). Each version derives one AES-256-GCM key per kind of value with HKDF-SHA256 (RFC 5869): a fixed 32-byte zero salt and the label widefleet control-plane <kind> v1.
  • One sealing format. The runtime already seals this way with its own root keys and labels (widefleet runtime <purpose> v1), so the format moves to createSealing in @platform/contracts/keyring. Both sides pass their namespace and keep only their earlier-format reader; the runtime's ciphertexts do not change.
  • Key IDs. kid is <version>:<kind>. Decryption accepts only the kind of the caller's context. Version IDs could not contain a colon before, so the scheme is unambiguous.
  • Context binding. The protected header carries the canonical context as ctx: the kind and its fields in a fixed order, for example ["app-secret", organizationId, appId, name]. Decryption requires an exact match. SSO configurations bind only their field (oidcConfig or samlConfig), because Better Auth's field transforms receive no provider identity.
  • Re-encryption. A value counts as current only under the active version's key for its kind. status reports earlierFormat, the values still in the earlier format, and is incomplete while any remain.
  • Earlier values. Ciphertexts whose kid names only a version decrypt with the version's bytes and no context check until reencrypt moves them. A later PR removes this path and earlierFormat.

Upgrade

After deploying, run node tools/secret-encryption.ts reencrypt and check that status reports earlierFormat: 0.

Testing

  • pnpm check
  • packages/app-runtime: secrets.test.ts, rekey.test.ts, keys.test.ts, oauth.test.ts, workload-keys.test.ts against the shared format; app-services-runtime.test.ts on workerd
  • secret-encryption.test.ts: header and key per kind, checked with node:crypto alone (HKDF and AES-GCM); a recorded value in the new format; rejection for another app secret name, app, organization or kind; a recorded value in the earlier format
  • secret-reencryption.integration.test.ts: fixtures in the earlier format are counted, moved and read with their contexts
  • installation-keys.test.ts, secret-contexts.integration.test.ts, jwe-secret-guard.integration.test.ts, organization-settings.integration.test.ts, organization-storage.integration.test.ts, app-services.integration.test.ts, company-sso-plugin.integration.test.ts, required-secrets.integration.test.ts, template-installs.integration.test.ts, settings.integration.test.ts, cloudflare-operations.integration.test.ts
  • cloudflare/initialization.test.ts, cloudflare/deployment.test.ts -t secrets

@tnkuehne
tnkuehne added this pull request to stack #430 November 26, 2026 18:36
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 06507d5 Nov 27 2026, 08:31 AM

@tnkuehne
tnkuehne force-pushed the feat/control-plane-purpose-keys branch 4 times, most recently from f2e859e to 6dc8045 Compare November 26, 2026 21:14
@tnkuehne
tnkuehne force-pushed the feat/control-plane-purpose-keys branch from 6dc8045 to e4ea4ba Compare November 26, 2026 22:05
@tnkuehne
tnkuehne force-pushed the feat/control-plane-purpose-keys branch from e4ea4ba to 54cc127 Compare November 26, 2026 22:17
@tnkuehne
tnkuehne force-pushed the feat/control-plane-purpose-keys branch 2 times, most recently from 2b693d3 to f6fb208 Compare November 27, 2026 01:41
Every control-plane ciphertext used its key version's bytes directly and did
not bind the context its callers already passed, so a value could be moved to
another row, organization or kind without failing to decrypt.

Each key version now derives one key per kind of value with HKDF-SHA256. The
JWE names version and kind as `kid` and carries the canonical context as a
protected `ctx`, and decryption requires both to match the caller's context.
Values in the earlier format stay readable until `reencrypt` moves them; the
status reports how many remain.
Both seal with the same JWE format and per-purpose HKDF keys, so the format
now lives once in the contracts keyring module; each side keeps only its
earlier-format reader. The test wrapper that compared contexts is gone, since
ciphertexts bind their context, and so is the test for a key ID that only a
corrupted row could hold.
@tnkuehne
tnkuehne force-pushed the feat/control-plane-purpose-keys branch from f6fb208 to 06507d5 Compare November 27, 2026 08:30
@tnkuehne
tnkuehne merged commit 3214842 into main Nov 27, 2026
17 of 32 checks passed
@tnkuehne
tnkuehne deleted the feat/control-plane-purpose-keys branch November 27, 2026 08:48
tnkuehne added a commit that referenced this pull request Nov 27, 2026
Values stored before the runtime keyring (#428) and before the control
plane's keys per kind (#435) stayed readable through temporary readers
until they were rewritten. This removes those readers.

## Changes

- **Runtime.** `legacy-runtime-ciphertext.ts` goes: values sealed with
the raw key and app keys wrapped with AES-KW no longer open. Stored
values and app keys accept only the keyring format, so the rewrite no
longer special-cases app key records.
- **Control plane.** The reader for JWEs whose `kid` names only a key
version goes, together with `earlierFormat` in `secret-encryption
status`. A value still in that format counts as `failed`.
- **Tests.** The fixtures and cases for the earlier formats go. The
workerd test of the rewrite route now moves a value from a read-only key
version to the active one. Rejection and missing-key cases use values in
the current format, so each fails only for the property it tests.

## Upgrade

Rewrite both stores on the previous release before upgrading:

- A runtime deployment reports `0 rewritten, 0 unreadable, 0 conflicts`.
- `node tools/secret-encryption.ts status` reports `earlierFormat: 0`
and `failed: 0`.

Managed OAuth values that clients still hold from before the runtime
keyring are no longer accepted; those connections need a new
authorization.

## Testing

- `svelte-check` (control plane), `tsc` (app runtime), `oxlint`, `oxfmt`
- `packages/app-runtime`: `secrets.test.ts`, `keys.test.ts`,
`rekey.test.ts`, `oauth.test.ts`, `workload-keys.test.ts`
- `apps/control-plane`: `secret-encryption.test.ts`,
`secret-reencryption.integration.test.ts`, `runtime-rekey.test.ts`,
`app-services-runtime.test.ts` with `RUN_SERVICE_TESTS=1` (workerd and
celld), `cloudflare/deployment.test.ts -t "removes an undeployed app"`
with `RUN_CLOUDFLARE_TESTS=1`
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.

1 participant