airi/server/apps
RainbowBird b3a6d60718
Some checks failed
CI / Detect changes (push) Has been cancelled
Cloudflare Pages (Auth UI) / Deploy - ui-server-auth (push) Has been cancelled
Cloudflare Workers / Build - docs (push) Has been cancelled
E2E Test - Tamagotchi (Linux) / X11 window render (Xvfb) (push) Has been cancelled
E2E Test - Tamagotchi (Linux) / Wayland window render (weston headless) (push) Has been cancelled
CI / Build Test (stage-tamagotchi) (push) Has been cancelled
CI / Unit Test (stage-tamagotchi) (push) Has been cancelled
CI / Unit Test (stage-ui-1) (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Build Test (stage-web) (push) Has been cancelled
CI / Build Test (ui-playgrounds) (push) Has been cancelled
CI / Build Test (stage-tamagotchi-godot) (push) Has been cancelled
CI / Unit Test (others) (push) Has been cancelled
CI / Unit Test (api-server-1) (push) Has been cancelled
CI / Unit Test (api-server-2) (push) Has been cancelled
CI / Unit Test (stage-ui-2) (push) Has been cancelled
CI / Unit Test (push) Has been cancelled
CI / Type Check (others) (push) Has been cancelled
CI / Type Check (desktop) (push) Has been cancelled
CI / Type Check (web) (push) Has been cancelled
CI / Type Check (push) Has been cancelled
CI / Check Provenance (push) Has been cancelled
Cloudflare Workers / Deploy - stage-web (push) Has been cancelled
feat(api-server): add a script to rotate the LLM router master key (#3545)
## Summary

- Add `server/apps/api/src/scripts/rotate-llm-router-master-key`. It
re-encrypts stored provider keys from `LLM_ROUTER_MASTER_KEY_PREVIOUS`
to `LLM_ROUTER_MASTER_KEY`.
- Document the `LLM_ROUTER_MASTER_KEY` rotation in the API README.
- Document the `BETTER_AUTH_SECRET` rotation in the Auth README. It
needs no code: Better Auth reads `BETTER_AUTH_SECRETS` itself, and
expiring the old JWKS keys makes the jwt plugin create a key under the
new secret.

## Context

The maintainer asked for a script to rotate `LLM_ROUTER_MASTER_KEY` and
`BETTER_AUTH_SECRET`, plus a rotation plan.

Replacing `BETTER_AUTH_SECRET` alone breaks OIDC. The jwt plugin
encrypts the JWKS private keys with the secret, so token signing fails
after the change. Better Auth 1.6.25 supports versioned `secrets` for
this case.

This PR does not change any deployed variable. The rotation itself is a
separate operator step.

## Change map

| Module | Before | After |
| --- | --- | --- |
| API envelope crypto users | AAD helpers were private |
`providerConfigAad` and `STREAM_MODEL_LABEL_FALLBACK` are exported for
the script |
| API rotation script | none | Re-encrypts `config_kv`
`LLM_ROUTER_CONFIG`, `UNSPEECH_UPSTREAM`, and
`user_provider_configs.config` |
| Auth README | no rotation steps | `BETTER_AUTH_SECRETS` plus JWKS key
expiry, no code change |

## Architecture and behavior

```mermaid
sequenceDiagram
  participant Op as Operator
  participant API as API container
  participant PG as Postgres
  participant R as Redis
  Op->>API: rotate-llm-router-master-key --apply
  API->>PG: SELECT config_kv ... FOR UPDATE
  API->>API: decrypt with previous key, encrypt with current key (same AAD)
  API->>PG: UPDATE config_kv
  API->>R: DEL config:<key>, PUBLISH configkv:invalidate
  API->>PG: UPDATE user_provider_configs WHERE id = ? AND config = <old>
  API->>PG: second read-only pass
  API-->>Op: exit 0 / 1 / 2
```

Exit codes of the script: `0` means the old key can be removed. `1`
means some values open with no configured key. `2` means some values
still need the old key.

## Boundaries and risks

| Invariant or boundary | Failure mode | Protection | Evidence or gap |
| --- | --- | --- | --- |
| AAD must match each reader | Re-encrypted key cannot be read | Uses
the model map key, `streaming-tts`, and `providerConfigAad`, the same as
the readers | Unit tests in `rotation.test.ts` |
| Concurrent admin write to ConfigKV | Script overwrites a new config |
Row lock in one transaction | Not run against a real database |
| Concurrent user edit of a provider config | Script overwrites the edit
| Update matches the old ciphertext. A miss counts as a conflict | Not
run against a real database |
| Other ConfigKV fields | Rewrite drops or adds fields | `looseObject`
schemas keep unknown fields and add no defaults | Unit tests check
`headers`, `provider`, and `defaults` |
| Wrong old key | Data is lost | Values that neither key opens stay
unchanged and give exit code 1 | Unit tests |
| JWKS keys after an auth secret rotation | Removing
`BETTER_AUTH_SECRETS` later breaks decryption of new keys | README says
to keep `BETTER_AUTH_SECRETS` | Documented only |

Rotating the auth secret signs out every cookie and bearer session. This
is Better Auth behavior: it signs cookies with the current secret only.
OIDC refresh tokens are stored as SHA-256 hashes and stay valid.

## Verification

Run in an rc Workspace on the first commit of this branch. The second
commit removes the auth script and only changes the Auth README.

- `pnpm -F @proj-airi/api-server typecheck`: pass
- `pnpm -F @proj-airi/api-server exec vitest run`: 995 pass, 5 fail. The
failures are PGlite 5 s timeouts in `mock-db.test.ts` and
`flux-usage-migration.test.ts`. They fail the same way on the merge base
in the same Workspace. `audio-speech-ws/route.test.ts` failed once in
the full run and passed alone.
- `pnpm exec eslint` on the changed files: no errors

Not verified: running the script against a real Postgres and Redis.

## Agent

- Harness: Claude Code
- Model: claude-opus-5-5

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-11-18 13:49:09 +08:00
..
api feat(api-server): add a script to rotate the LLM router master key (#3545) 2026-11-18 13:49:09 +08:00
auth feat(api-server): add a script to rotate the LLM router master key (#3545) 2026-11-18 13:49:09 +08:00