Repository navigation
Encrypt new secrets with a versioned keyring - #384
Merged
Merged
Conversation
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
widefleet-docs | 16ad2b6 | Nov 25 2026, 03:42 PM |
This was referenced Nov 25, 2026
tnkuehne
force-pushed
the
secrets/keyring
branch
from
November 25, 2026 10:45
e7f7280 to
cd91f8b
Compare
This was referenced Nov 25, 2026
tnkuehne
force-pushed
the
secrets/keyring
branch
2 times, most recently
from
November 25, 2026 15:06
ef2b2b4 to
6f1ca31
Compare
New ciphertexts are `v1:<key ID>:<payload>`: AES-256-GCM through WebCrypto with a 96-bit random IV, under an HKDF-SHA256 subkey of the key version. Each version is 32 random bytes; parsing rejects anything else without repeating key material in errors. Unprefixed ciphertexts keep decrypting with the legacy key. PLATFORM_ENCRYPTION_KEYS or PLATFORM_ENCRYPTION_KEYS_FILE lists versions, active first. Without either, Node installations create encryption.keys in PLATFORM_STATE_DIRECTORY, and a configured keyring must keep its versions. Cloudflare installations read the keyring from a secret and keep writing the legacy format until it is set.
New secrets now always use the active key version; there is no longer a mode that writes the legacy format without a keyring. Cloudflare installations must set PLATFORM_ENCRYPTION_KEYS: configuration validation rejects a missing or malformed keyring at startup, and deploy-hosted requires it. Node installations keep the generated encryption.keys as the zero-setup default. Decrypting unprefixed ciphertexts and the legacy key chain (PLATFORM_ENCRYPTION_KEY, its _FILE variant, an existing encryption.key and the BETTER_AUTH_SECRET fallback) now live only in legacy-secret-migration.ts, marked as temporary. New installations no longer create encryption.key.
Stored secrets are now JWE compact serializations (RFC 7516) produced with jose: direct encryption with the active key version's 32 bytes, A256GCM, and the version's ID as kid. Decryption accepts only dir and A256GCM and rejects an unknown kid. Legacy values are recognized as bare hexadecimal. Key material must be exactly 32 bytes in canonical base64 (padded) or canonical base64url (unpadded); mixed alphabets are rejected.
PLATFORM_ENCRYPTION_KEYS, PLATFORM_ENCRYPTION_KEYS_FILE and the generated
encryption.keys now hold a JWK Set (RFC 7517) of symmetric keys, active key
first: {"keys":[{"kty":"oct","kid":"...","k":"..."}]}. Each key must have
kty "oct", a kid of 1 to 32 letters, digits, - or _, and a k that is
canonical unpadded base64url of exactly 32 bytes; kids and keys must be
unique. Keys are imported with jose's importJWK.
PLATFORM_ENCRYPTION_KEY is optional on Cloudflare and in deploy-hosted: only
the temporary legacy reader uses it, for stored values from earlier
releases.
Without PLATFORM_ENCRYPTION_KEYS in the local environment, the fallback threw, so seeding and the local CLI stopped. The local tool now loads the installation keys first, as the recovery command does.
tnkuehne
force-pushed
the
secrets/keyring
branch
from
November 25, 2026 15:41
6f1ca31 to
16ad2b6
Compare
tnkuehne
added a commit
that referenced
this pull request
Nov 25, 2026
Operators need a way to move every stored secret to the active key version before an earlier key, or the legacy format, can be removed. This adds one Node command for that, following `tools/recover.ts`. It replaces the closed #386, without UI, API endpoints, a Rust CLI command, a cursor protocol or pending-deployment counting. Based on #384. Review only the commits after #384. ## Usage ```sh # Self-hosted, inside the control-plane container docker compose ... exec control-plane node tools/secret-encryption.ts status docker compose ... exec control-plane node tools/secret-encryption.ts reencrypt # Hosted, from CI with explicit settings DATABASE_URL=... PLATFORM_ENCRYPTION_KEYS=... PLATFORM_ENCRYPTION_KEY=... \ node apps/control-plane/tools/secret-encryption.ts reencrypt ``` The command reads only `DATABASE_URL`, `PLATFORM_STATE_DIRECTORY`, the keyring settings and the legacy key settings. **It never generates keys.** It requires `PLATFORM_ENCRYPTION_KEYS`, `PLATFORM_ENCRYPTION_KEYS_FILE` or an existing `encryption.keys`, and fails otherwise. `installationKeys` keeps generating the keyring on first start; the command uses a new non-generating `readInstallationKeyring`. ## Behavior **Locations covered:** - `apps.secrets` - `organization_settings.directory_secret` and `app_client_secret` - `sso_provider.oidc_config` and `saml_config` - the client secret in a leftover pre-plugin `organization_settings.settings.identity` **How it works:** - **Bounded pages.** Rows are read in pages of 100 in key order. The page cursor follows the raw database rows, so rows without a secret never end a scan early. - **Compare-and-swap per row.** A row is replaced only if it still holds what was read, so a concurrent write wins and is counted as `changed`. - **Replacement by slot.** New ciphertexts are keyed by row and field or secret name, not by ciphertext string, so two slots holding the same ciphertext each get their own value. - **Active values are verified.** Values whose header names the active `kid` are decrypted, not skipped by their header. - **Secret revisions unchanged.** The secret revision is left alone, so nothing is redeployed. - **Output.** JSON with counts per key version, the legacy count and the failed count. Legacy values are recognized by their format, never by a key name, and values without a readable `kid` count only as failed. `reencrypt` also reports `reencrypted` and `changed`. - **Exit status.** Nonzero if a value does not decrypt, or if `reencrypt` leaves values under another key version or in the legacy format. ## Testing - `pnpm check` - `tests/secret-reencryption.integration.test.ts`: - every location moves from the legacy format and an earlier key version to the active one - counts before and after, and a second run changes nothing - readers find every value with matching contexts (context checks from #383 active) - two slots holding the same ciphertext get distinct values - an altered value under the active `kid` is counted as failed and left unchanged - a concurrent write between read and replace is kept - paging past 110 rows without a secret - key versions named `legacy`, `constructor` or `__proto__` are counted under their own ID, not as legacy values or inherited properties - the command itself: - fails without a keyring and creates no file - `reencrypt` exits 0 - `status` exits 1 while a value names an unknown `kid` - the state directory stays empty ## Stack 1. #383: context API, no behavior change 2. #384: JWE format, JWK Set keyring, temporary legacy reader 3. This PR: re-encryption command 4. #399: legacy cleanup (draft) GitHub stack: #400. Operator documentation (draft): #385 <!-- greptile_comment --> <!-- greptile_summary --> <h2><a href="https://app.greptile.com/api/retrigger?id=119521021&sig=fd953f268e72c3f3f0356af21af5928972e43ebac759c5e0d10aa0fc250ddc73"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/-/switch-tenant?tenant=mendra&next=%2Fwidefleet%2Fwidefleet%2Fpull%2F398&utm_source=greptile_review&utm_medium=github&utm_campaign=view_in_greptile"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 5/5 <sup><img alt="Tier: plus" title="Review tier: plus" src="https://greptile-static-assets.s3.amazonaws.com/badges/effort-plus.svg?v=3" height="12"></sup></h2> <!-- greptile-impact-verdict --> The PR appears safe to merge; no new actionable issues remain. <details open><summary><h3>Summary</h3></summary> Adds an operator command to inspect stored secrets and re-encrypt them under the active key. - Secret status counts stored values by key version and decrypt failures. - Readable stored secrets move to the active key without changing their values. - Operators can check or re-encrypt secrets with an existing keyring. Source and tests were inspected; tests were not run. </details> <details open><summary><h3>Diagram</h3></summary> ```mermaid %%{init: {'theme': 'neutral'}}%% flowchart TD A[Read existing keys] --> B{Keyring found?} B -->|No| C[Exit unsuccessfully] B -->|Yes| D{Requested action} D -->|status| I[Scan and decrypt every secret] D -->|reencrypt| E[Read next page] E --> F[Decrypt each secret] F --> G[Encrypt values not using active key] G --> H[Replace only unchanged rows] H --> I I --> J[Print counts and set exit status] ``` </details> <!-- greptile_confidence_score:5 --> <sub>Reviews (4) · Last reviewed commit: ["Count key versions without colliding wit..."](128555c) · [Reviewed by Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub> <!-- /greptile_comment -->
tnkuehne
added a commit
that referenced
this pull request
Nov 25, 2026
…ntrol plane (#406) Packages that compose the control plane, such as a hosted deployment, call settings, authentication and onboarding functions that now take a `SecretEncryption` service instead of a key string (#383, #384). They had no public way to create that service. This exports `./src/lib/server/secret-encryption.ts` as `@platform/control-plane/secret-encryption`, next to the other server module exports, so they can use `configuredSecretEncryption` or `createSecretEncryption` with `parseKeyring`. No behavior changes. <!-- greptile_comment --> <!-- greptile_summary --> <h2><a href="https://app.greptile.com/api/retrigger?id=119690632&sig=a25a78546129c2c3e39d57916111b56e7f417557b12196834e219bab420b5f70"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/-/switch-tenant?tenant=mendra&next=%2Fwidefleet%2Fwidefleet%2Fpull%2F406&utm_source=greptile_review&utm_medium=github&utm_campaign=view_in_greptile"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 5/5</h2> <!-- greptile-impact-verdict --> This export-only change appears safe to merge. <details open><summary><h3>Summary</h3></summary> Adds `@platform/control-plane/secret-encryption` so packages that compose the control plane can create the encryption service. - Composing packages can import the secret encryption service by name. </details> <!-- greptile_confidence_score:5 --> <sub>Reviews (1) · Last reviewed commit: ["Export the secret encryption service for..."](c2d9032) · [Reviewed by Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub> <!-- /greptile_comment -->
tnkuehne
added a commit
that referenced
this pull request
Nov 25, 2026
Removes the temporary legacy reader that #384 isolated, now that stored secrets are re-encrypted with the command from #398. ## Changes - **Legacy module deleted.** `legacy-secret-migration.ts` is gone. Stored secrets are now read only as JWE. - **Legacy key chain removed:** - `PLATFORM_ENCRYPTION_KEY` and `PLATFORM_ENCRYPTION_KEY_FILE` - reading an existing `encryption.key` - the `BETTER_AUTH_SECRET` fallback that `createAuthentication` used for encryption - **Configuration.** The legacy key is removed from the Node configuration, the Worker configuration, `deploy-hosted`, Compose and the example environment. - **Command output.** Legacy counting is removed from the re-encryption command. - **Guard migration `0044_require_jwe_secrets`.** Startup refuses to continue while any covered location still holds a value that is not a JWE compact serialization: - app secrets - the directory and app sign-in client secrets - SSO provider OIDC and SAML configurations - a leftover identity document's client secret Its message asks operators to run `node tools/secret-encryption.ts reencrypt` on the previous release first, then upgrade. ## Upgrade path No release contains #384 and #398 without this PR. An installation with secrets stored by an earlier release has two options: - Re-encrypt on a build of a commit between #398 and this PR: run `node tools/secret-encryption.ts reencrypt` until it exits 0, then upgrade. - Start with a fresh database. ## Testing - `pnpm check` - `tests/jwe-secret-guard.integration.test.ts`: for every checked location, a JWE value passes and a value in the earlier format aborts with the upgrade message. Locations: app secrets, the directory and app sign-in client secrets, a leftover identity document, and the SSO provider OIDC and SAML configurations. - `tests/secret-reencryption.integration.test.ts`, `tests/secret-encryption.test.ts`, `tests/installation-keys.test.ts` and `tests/cloudflare/initialization.test.ts`, without legacy cases. ## Stack 1. #383: context API, no behavior change 2. #384: JWE format, JWK Set keyring, temporary legacy reader 3. #398: re-encryption command 4. This PR: legacy cleanup Operator documentation (draft): #385 <!-- greptile_comment --> <!-- greptile_summary --> <h2><a href="https://app.greptile.com/api/retrigger?id=119521333&sig=2114cc615d730cbb335d4f7c6986c67edf164cfc2829706297076f2c957fb586"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/-/switch-tenant?tenant=mendra&next=%2Fwidefleet%2Fwidefleet%2Fpull%2F399&utm_source=greptile_review&utm_medium=github&utm_campaign=view_in_greptile"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 5/5 <sup><img alt="Tier: plus" title="Review tier: plus" src="https://greptile-static-assets.s3.amazonaws.com/badges/effort-plus.svg?v=3" height="12"></sup></h2> <!-- greptile-impact-verdict --> The PR appears safe to merge after the stated release and re-encryption prerequisites are met. <details open><summary><h3>Summary</h3></summary> Stored secrets now use JWE only. The PR removes the old reader and key settings, then adds `0044_require_jwe_secrets` to stop upgrades while older values remain. - Upgrades stop when covered secrets are not JWE. - The control plane reads saved secrets only with its JWK keyring. - The re-encryption status counts only JWE key versions. </details> <details open><summary><h3>Diagram</h3></summary> ```mermaid %%{init: {'theme': 'neutral'}}%% flowchart TD A["Previous release"] --> B["Run re-encryption until it succeeds"] B --> C["Upgrade"] C --> D["0044 checks stored secrets"] D -->|Older format remains| E["Stop with upgrade instructions"] D -->|All values pass| F["Start with JWE-only reads"] ``` </details> <!-- greptile_confidence_score:5 --> <sub>Reviews (3) · Last reviewed commit: ["Test the JWE guard for every stored secr..."](4389d27) · [Reviewed by Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub> <!-- /greptile_comment -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stored customer secrets used one unversioned key, so the key could not be rotated. This stores new secrets as standard JWE under a keyring of key versions. The first key encrypts; the others only decrypt values they wrote.
Based on #383. Review only the commits after #383.
Format
New values are JWE compact serializations (RFC 7516) produced with
jose, already a direct dependency at 6.2.10:importJWK.dirandA256GCMand looks the key up bykid. An unknownkidis rejected.SecretContextis still not in the header; binding it comes later and changes only this service.node:cryptoalone, independently ofjoseand our code path.Keyring
PLATFORM_ENCRYPTION_KEYS,PLATFORM_ENCRYPTION_KEYS_FILEand the generatedencryption.keyshold a private JWK Set (RFC 7517), active key first:{"keys":[{"kty":"oct","kid":"2026-11","k":"<32 random bytes, unpadded base64url>"}]}Validation:
ktymust beoct.kiduses 1 to 32 characters from[A-Za-z0-9_-], so no colon; a later key namespace depends on that.kmust be canonical unpadded base64url of exactly 32 bytes.The keyring is mandatory everywhere:
encryption.keysinPLATFORM_STATE_DIRECTORYon first start, atomically, and logs its path. If a configured keyring is set while that file exists, startup fails unless the configured set contains every generated key, so values written with it stay readable.PLATFORM_ENCRYPTION_KEYS, anddeploy-hostedrequires it.Legacy values (temporary)
Values from earlier releases are bare hexadecimal from better-auth's
symmetricEncrypt. Everything that exists only for them lives inlegacy-secret-migration.ts, marked as temporary migration code:PLATFORM_ENCRYPTION_KEY,PLATFORM_ENCRYPTION_KEY_FILE, an existingencryption.key, and theBETTER_AUTH_SECRETfallbackNew installations no longer create
encryption.key.PLATFORM_ENCRYPTION_KEYis optional in the Worker configuration anddeploy-hosted, since installations without legacy data don't need it.The re-encryption command in #398 moves legacy values to the keyring, and #399 removes this module behind a guard migration.
Testing
pnpm checktests/secret-encryption.test.ts:kid, otheralgandencvalues, a tampered tagtests/installation-keys.test.ts:tests/cloudflare/initialization.test.ts: the keyring is required and validated at startup; the legacy key is optional.Stack
GitHub stack: #400. Operator documentation (draft): #385


Confidence Score: 5/5 
This PR appears safe to merge; no blocking issue remains.Summary
New secrets use compact JWE with the first key in a validated JWK Set. Node installations can generate a private keyring; Cloudflare installations must supply one. A temporary reader keeps legacy values readable.
Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD N["Node: configured or generated JWK Set"] --> K["Validated keyring"] H["Cloudflare: required JWK Set"] --> K K --> E["New writes: JWE with active kid"] C["Stored value"] --> F{"Legacy hexadecimal?"} F -->|Yes| L["Temporary legacy reader"] F -->|No| J["Find kid and decrypt JWE"] K --> JReviews (7) · Last reviewed commit: "Pass the installation encryption to auth..." · Reviewed by Greptile