Skip to content

Derive one runtime key per purpose - #429

Merged
tnkuehne merged 3 commits into
feat/runtime-keyringfrom
feat/runtime-purpose-keys
Nov 27, 2026
Merged

tnkuehne merged 3 commits into
feat/runtime-keyringfrom
feat/runtime-purpose-keys

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #428. Each runtime keyring version encrypted every kind of value with its own 32 bytes: app secret snapshots, the keys behind WIDEFLEET_KEYS, the workload signing ring and the managed OAuth access, refresh and state values. A weakness in how one of them uses the key, for example a nonce reuse, would then reach all of them.

Changes

  • Purpose keys (secrets.ts). Each version derives one AES-256-GCM key per purpose with HKDF-SHA256 (RFC 5869): a fixed 32-byte zero salt and the label widefleet runtime <purpose> v1. The purposes are a closed list: app-secrets, app-keys, workload-keys, oauth-access, oauth-refresh and oauth-state.
  • Key IDs. kid is <version>:<purpose>. Opening accepts only the purpose the caller expects, and never the root key itself. Version IDs cannot contain a colon, so the scheme is unambiguous.
  • Callers. Snapshot preparation and reads, app keys, the workload ring, managed OAuth and the rewrite pass their purpose. The rewrite counts a value as current only under the active version's key for its purpose.
  • Docs are in Document the runtime keyring, rotation and upgrade #431.

This PR ships in the same release as #428, so no deployment writes the root-key format of #428 and the rewrite runs once.

Testing

  • pnpm check
  • packages/app-runtime: secrets.test.ts (a value of one purpose does not open for another, also with the purpose renamed in its header; the root key opens nothing), rekey.test.ts, keys.test.ts, oauth.test.ts, workload-keys.test.ts, oauth-metadata-document.test.ts, template-oauth.test.ts
  • apps/control-plane: app-services-runtime.test.ts on workerd, managed-oauth-runtime.test.ts, cloudflare/runtime.test.ts -t secrets

Confidence Score: 5/5 · The PR appears safe to merge; no blocking issue remains.RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

The PR appears safe to merge; no blocking issue remains.

Summary

Derives a separate encryption key for each runtime purpose and includes that purpose in kid.

  • Runtime values use separate encryption keys for each purpose.
  • The storage rewrite checks each value against its purpose key.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Root["Runtime key version"] --> HKDF["HKDF-SHA256 with purpose label"]
  Purpose["Caller supplies purpose"] --> HKDF
  HKDF --> Key["Purpose-specific AES-GCM key"]
  Key --> Seal["Encrypt value"]
  Context["Expected context"] --> Seal
  Seal --> Value["JWE with version:purpose and context"]
  Value --> Open["Open with expected purpose and context"]
  Key --> Open
Loading

Reviews (8) · Last reviewed commit: "Drop an import the removed refresh test ..." · Reviewed by Greptile

@tnkuehne
tnkuehne added this pull request to stack #430 November 26, 2026 16:54
@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 e6eae1d Nov 27 2026, 08:31 AM

@tnkuehne
tnkuehne force-pushed the feat/runtime-purpose-keys branch from 3b4c4d1 to a51abe2 Compare November 26, 2026 16:57
Comment thread packages/app-runtime/src/oauth.test.ts Resolved
@tnkuehne
tnkuehne force-pushed the feat/runtime-purpose-keys branch 6 times, most recently from b77abfd to c88d32c Compare November 27, 2026 01:41
Every runtime keyring version used its 32 bytes directly for app secret
snapshots, app keys, the workload signing ring and managed OAuth values. Each
version now derives a key per purpose with HKDF-SHA256, and `kid` names version
and purpose as `<version>:<purpose>`, so a weakness in one use does not reach
the others.
@tnkuehne
tnkuehne force-pushed the feat/runtime-purpose-keys branch from c88d32c to e6eae1d Compare November 27, 2026 08:30
@tnkuehne
tnkuehne merged commit 0efb6c7 into main Nov 27, 2026
17 of 32 checks passed
@tnkuehne
tnkuehne deleted the feat/runtime-purpose-keys branch November 27, 2026 08:48
tnkuehne added a commit that referenced this pull request Nov 27, 2026
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`
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