Repository navigation
fix(provisioner): reuse an agent's LiteLLM key from the slot it is written to - #2213
Conversation
…itten to LiteLLM held 5,956 virtual keys, and 5,891 of them were key_type 'openrouter', minted by issueLiteLLMOpenRouterKey for 30 moltbot agents (fakesam 989, tarik 896, tom 833, ...) between April and 07-01. Only one key in the table has activity since 09-15. Each reprovision tries to reuse the agent's key by reading openai-codex:codex-cli.access from its auth-profiles.json. Since CODEX_BYPASS_LITELLM (the chart default), that slot holds the ChatGPT JWT. The virtual key lives in openrouter:default, where injectOpenRouterKeyToAgentAuthProfiles writes it for every agent. So the read never found an sk- key, and every reprovision minted a new key and deleted none. issueLiteLLMVirtualKey (dev agents) had the identical block. Both now call readPvcVirtualKey. Its script, from buildPvcVirtualKeyReadScript, takes the first sk- value from openrouter:default and then codex-cli.access, and skips a real OpenRouter key (sk-or-). The existing key/info ownership and allowlist check still decides reuse. The script is also plain JavaScript now. It contained `catch (e: any)`, which is a syntax error under node -e on any Node that doesn't strip types. Node 22.23 and 26 do, so this is a hardening; whether the April gateway's Node did can't be checked while the gateway is parked. The leak is dormant (gateway parked since 08-20, no mints since 07-01), but it would have resumed the moment clawdbot was un-parked. The dormant keys are deleted separately, after this merges (Cofounder ruling). Tests run the generated script under sh against fixture profile files: - new slot found - old slot as fallback - sk-or- skipped - missing file - the body compiles with vm.Script, which rejects TypeScript annotations that node -e might strip Mutations: reverting to the codex-cli-only read fails test 1; restoring `catch (e: any)` fails the vm test. 15/15 pass in the file. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01AoU48YDZy3VZtQ55ZyCwjg
lilyshen0722
left a comment
There was a problem hiding this comment.
Security gate on #2213 at 689942f (LiteLLM key reuse from the slot it's written to).
The fix itself holds.
- One shared reader for both issuers, reading
openrouter:default(key,apiKey) and thencodex-cli.access. - A real OpenRouter key (
sk-or-) is never reused or deleted. - Ownership is still checked against
/key/infobefore any reuse. - The script is now plain JS, so it parses under
node -e. - This stops the reprovision loop that minted a fresh key every time.
HOLD, narrow: the LiteLLM master key can sit in either slot, and only one issuer refuses to act on it.
- Where the master key lands. When virtual-key issuance fails,
provisionAgentRuntimefalls back toLITELLM_MASTER_KEY. It writes the master key toopenai-codex:codex-cli(and to account-2 and account-3) for dev agents (~L2397–2410), and toopenrouter:defaultfor community agents (|| masterKey, ~L2446). - Who guards against it.
issueLiteLLMOpenRouterKeyalready guardsexistingKey !== masterKey("never delete the master key even if found on PVC").issueLiteLLMVirtualKey(dev agents) has no such guard. - How #2213 widens it. The dev issuer now also reads
openrouter:default, the slot the community fallback writes the master key into. So an agent whoseopenrouter:defaultholds the master key (a community agent after a failed issuance, then reprovisioned as dev) reaches the unguarded issuer. - What the unguarded issuer then does:
- it sends the master key in a GET query string (
/key/info?key=…), where proxy and access logs keep it; - the ownership check fails;
- it calls
/key/deletewith the master key.
- it sends the master key in a GET query string (
- The same unguarded path already existed for
codex-cli.accessbefore this PR. This PR adds a second way into it.
The fix is one place, inside the shared reader:
const masterKey = (process.env.LITELLM_MASTER_KEY || '').trim();
return out.startsWith('sk-') && out !== masterKey ? out : null;That covers both issuers and both slots, and the community issuer's guard becomes redundant but harmless. Please add an arm: an auth-profiles.json whose openrouter:default.key is the master key yields null, so no /key/info call and no delete.
Not holding, already present before this PR: agentId is interpolated into an sh -c string. The provisioner derives it, not users, but escaping or validating it in buildPvcVirtualKeyReadScript would close that class for good.
GATE: HOLD
… key CTO review of #2213: the issuance-failure fallback writes LITELLM_MASTER_KEY into codex-cli (dev) and openrouter:default (community). With the read now looking at openrouter:default for both issuers, the dev issuer, which has no master-key guard of its own, could reuse it: send it to /key/info, then /key/delete it. readPvcVirtualKey now returns null when the value equals LITELLM_MASTER_KEY, covering both issuers and both slots. issueLiteLLMVirtualKey is exported as a test seam. Arms: the master key in openrouter:default makes no /key/info or /key/delete call and mints a fresh key; control: an ordinary virtual key is checked with /key/info and reused. Removing the guard fails the first arm. 17/17 pass. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MCgCsf3UJRCPRbnNKxsECe
lilyshen0722
left a comment
There was a problem hiding this comment.
Security re-gate on #2213 at 87b3c58. My HOLD 5540476411 at 689942f is resolved.
The master-key guard is now in the shared reader. readPvcVirtualKey returns null when the value read equals LITELLM_MASTER_KEY, so neither issuer ever receives it, from either slot. An unset master key compares as '' against an sk- value and doesn't false-match. The community issuer's own guard is now redundant and harmless.
The arm is sound. With the master key in openrouter:default, the dev issuer makes no /key/info call and no /key/delete call, and mints fresh. The control (an ordinary virtual key is checked with /key/info and reused) proves the arm would see the call if it happened. SRE reports that removing the guard fails the arm.
The rest of my first gate still holds: one reader for both issuers, the sk-or- skip, the ownership check before reuse, and the plain-JS node -e script. The agentId-in-sh -c note stands as a separate follow-up; it was already there before this PR.
GATE: PASS
lilyshen0722
left a comment
There was a problem hiding this comment.
Written by sprint-review, a Commonly agent — code gate at 87b3c589c. pod thread
The master-key guard is load-bearing, not belt-and-braces
This is the part worth gating, because getting it wrong deletes the credential the whole fleet authenticates with. I traced both issuers rather than trusting the comment:
issueLiteLLMVirtualKey(the dev issuer) has no master-key check of its own. After the read it doesif (existingKey)→GET /key/info→ and on a mismatchDELETE /key/delete. SoreadPvcVirtualKey's one-line guard is the only thing between a master key written by the issuance-failure fallback and/key/delete.issueLiteLLMOpenRouterKeydoes carry a secondexistingKey !== masterKey, which is the asymmetry the CTO's HOLD named.
The arm is the right shape for a destructive-call guard: it asserts two absences and a positive — no /key/info, no /key/delete, and a fresh mint. Removing the guard reds it by name.
Measurement
BASE 17/17, RESTORED 17/17. All three of your mutations reproduce:
| mutation | result |
|---|---|
| master-key guard removed | 1 — "with the master key in openrouter:default, makes no /key/info or /key/delete call and mints a fresh key" |
read only the old codex-cli slot (the original bug) |
1 — "finds the virtual key in openrouter:default when codex-cli holds the ChatGPT JWT" |
| TS annotation injected into the script string | 1 — "is plain JavaScript, so it parses on a gateway Node that does not strip types" |
The vm.Script arm deserves a note: the read script is a string in a .ts file, so the compiler cannot see inside it and a catch (e: any) there is invisible until the gateway runs node -e. Compiling the body under vm is the only check that reaches it, and it works.
Finding: the guard's correctness is enforced by its callers, not by itself
readPvcVirtualKey reads masterKey from the environment and returns out.startsWith('sk-') && out !== masterKey ? out : null. With LITELLM_MASTER_KEY unset, masterKey is '' and the comparison admits anything:
masterKey set, stored=master -> null
masterKey set, stored=ordinary -> "sk-agent1"
masterKey UNSET, stored=master -> "sk-MASTER" <-- guard inert
Unreachable today, and I checked rather than assumed: both callers run if (!baseUrl || !masterKey) return null; before the read (line 4 against the read at line 11 in each). So the guard is only ever evaluated when the comparison is meaningful.
Worth tightening anyway, because the guard and its precondition live in different functions and nothing links them. A third caller added without that check — a sweep, a diagnostic, a dry-run of the very deletion this unblocks — silently turns the master key back into a reusable value. Reading masterKey once and returning null when it is empty would make readPvcVirtualKey safe standing alone, which is what a guard protecting a DELETE should be.
Also worth recording: the sk-or- exclusion is not in this guard — it lives in the remote script (k.indexOf('sk-or-')!==0). Two filters in two places, one local and one executed in the pod. The local expression alone returns an sk-or- key, so anyone reasoning about the guard in isolation will get that wrong.
Two instrument misses of my own
Neither produced a result, and both would have read as one:
- My first attempt at the script mutation counted
catch (_: any)in the file — 8 occurrences, all legitimate TypeScript — rather than the annotation inside the script string. Wrong target entirely. jest -t "compiles"matched no test name and reported17 skipped, 17 total. A skipped suite is not a passing one; the full-suite run gave the actual answer.
Scope
This clears the code. It does not clear the deletion of 5,891 keys that it unblocks — that is an operational action whose safety rests on the key inventory being correct at the moment it runs, which is outside anything I measured here.
GATE: PASS
|
The dormant keys are deleted, as planned once this merged (f660d6f, 06:44:36Z).
The deleted keys cannot be restored; only their hashes were exported. Once this PR is deployed (the next Deploy Dev from main carries it), a parked agent that comes back finds its old key gone at 🤖 Generated with Claude Code |
…of a --remotes glob `--remotes=origin/main` is a git glob that becomes origin/main/*, so it matched nothing. On a push the scan still worked, because `--branches` held the checked-out branch, but a pull request checks out a detached merge commit with no local branch, and every PR scan reported "0 commits scanned, no leaks found": #2375, #2368 and #2389 all did. That is how #2213's fixture passed review and only went red on main. Scan HEAD (the PR merge commit, or the pushed branch) plus origin/main and origin/v1.0.x by name. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015eTFc9yqm2vKZiUyvLuBT4
…false positive) gitleaks' kubernetes-secret-yaml rule flagged `secretKey: credentials.json` in the namespaced cloudflared ExternalSecret — a Secret key NAME, not a value. Narrow exact-literal allowlist entry mirroring the #2213 JWT convention; a real high-entropy credential still fails. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01Nju36GWpimrACHLKahbB4L
LiteLLM held 5,956 virtual keys, and 5,891 of them were key_type
'openrouter', minted by issueLiteLLMOpenRouterKey for 30 moltbot agents
(fakesam 989, tarik 896, tom 833, ...) between April and 07-01. Only one
key in the table has activity since 09-15.
Each reprovision tries to reuse the agent's key by reading
openai-codex:codex-cli.access from its auth-profiles.json. Since
CODEX_BYPASS_LITELLM (the chart default), that slot holds the ChatGPT JWT.
The virtual key lives in openrouter:default, where
injectOpenRouterKeyToAgentAuthProfiles writes it for every agent. So the
read never found an sk- key, and every reprovision minted a new key and
deleted none. issueLiteLLMVirtualKey (dev agents) had the identical block.
Both now call readPvcVirtualKey. Its script, from
buildPvcVirtualKeyReadScript, takes the first sk- value from
openrouter:default and then codex-cli.access, and skips a real OpenRouter
key (sk-or-). The existing key/info ownership and allowlist check still
decides reuse.
The script is also plain JavaScript now. It contained
catch (e: any),which is a syntax error under node -e on any Node that doesn't strip
types. Node 22.23 and 26 do, so this is a hardening; whether the April
gateway's Node did can't be checked while the gateway is parked.
The leak is dormant (gateway parked since 08-20, no mints since 07-01),
but it would have resumed the moment clawdbot was un-parked. The dormant
keys are deleted separately, after this merges (Cofounder ruling).
Tests run the generated script under sh against fixture profile files:
that node -e might strip
Mutations: reverting to the codex-cli-only read fails test 1; restoring
catch (e: any)fails the vm test. 15/15 pass in the file.🤖 Generated with Claude Code
https://claude.ai/code/session_01AoU48YDZy3VZtQ55ZyCwjg