Repository navigation
fix(registry): only an identity's sole installer, or an admin, can mint, rotate or revoke its runtime token - #2165
Conversation
…nt, rotate or revoke its runtime token A runtime token acts as its agent in every pod where that identity is installed. The runtime-token issue and revoke routes, and the provision route that also mints, checked only that the caller belonged to the pod. The daemon routes already applied the sole-installer rule (Vera's ruling on #1315). - Move ownsAgent verbatim from routes/agentBinding.ts to services/runtimeIdentityOwnership.ts. The daemon routes keep their own messages and behaviour. - Apply it to POST and DELETE /pods/:podId/agents/:name/runtime-tokens and to /provision, before any token is revoked or issued. Admins are exempt, so operator seat renewals keep working. Tests: registry.runtimeTokenOwnership (8 arms, using the real rule against an in-memory installation set). The 5 refusal arms fail against main's routes; the 3 allow arms pass on both. The existing runtime-tokens suite gets owner fixtures. 20 suites and 126 tests pass, including the daemon adoption suites. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015NDyNbwmCco62PAAviSL1k
lilyshen0722
left a comment
There was a problem hiding this comment.
Sprint Review CODE GATE @ 51545934fe63fdbbc5245ab80249ef1c6a94e09d. The "verbatim" claim holds by string diff, the refusal arms are named and discriminating, and the admin exemption is pinned. The most useful thing in this review is a gap I chased and did NOT find — recorded so the next reviewer doesn't file it either. Verdict token is the last line.
"Moved verbatim" — checked as a string, not read
An authorization predicate that is almost the same is how this class of change goes wrong, so I diffed the function bodies rather than comparing them by eye:
main/routes/agentBinding.ts::ownsAgent vs pr2165/services/runtimeIdentityOwnership.ts::ownsRuntimeIdentity
→ IDENTICAL (declaration line and the eslint pragma excluded)
Both clauses, both collations ({locale:'en', strength:2} on each findOne — an asymmetric collation would let Default slip past the negative clause), and the $ne-matches-missing-field handling that maps to unknown_installer and refuses. Fail-closed on the legacy state.
And there is no leftover copy: const ownsAgent = async is gone from agentBinding.ts, which now imports the service. One rule, one implementation — so the daemon and token paths cannot drift.
The arms
| run | result |
|---|---|
BASE @ 51545934 |
17 passed / 17, 2 suites |
M1 — both route files replaced with origin/main's |
5 failed / 12 passed |
| the five, by name | a pod member who did not install the agent cannot mint or rotate its token, installing it yourself is not enough when another user also has it installed, an installation with no recorded installer blocks minting until repaired, a pod member who does not own the identity cannot revoke its tokens, a pod member who does not solely own the identity cannot provision it |
| RESTORED | 17 / 17, worktree clean |
Exactly as claimed: the new suite has 8 arms, the five that fail on main are the refusals, and they cover mint (all three failure modes), revoke and provision separately rather than through one shared helper test. The admin exemption is pinned by its own arm — an admin can mint for a shared identity (operators renew fleet seats) — so the exemption cannot be silently widened or dropped.
Both named routes carry the check: POST …/runtime-tokens at :166 and DELETE …/runtime-tokens/:tokenId at :237, plus the inline check in provision.ts:184-189. All three put isGlobalAdminUser first, so an admin never pays the two extra queries.
GET …/runtime-tokens is deliberately not gated, and that is the right call rather than an omission: serializeRuntimeTokens returns {id, label, createdAt, lastUsedAt} only — no token value — so listing metadata to a pod member leaks nothing a member cannot already see.
The gap I chased and did not find
I spent most of this gate on what looked like an uncovered sibling, and I am writing it up because the trail is convincing and wrong.
issueUserTokenForInstallation (same file, routes at :337 POST and :399 DELETE, not covered by credentialRefusal) mints through AgentIdentityService.getOrCreateAgentUser(agentName, {instanceId}) and stores the result on the agent's own User row as agentUser.apiToken. That reads exactly like the property this PR's docstring uses to justify the whole rule — a credential for the identity, not for one install — which would make the three user-token routes the same hole, nine lines of routing away.
It is not. Measured:
| result | |
|---|---|
apiToken occurrences in agentRuntimeAuth.ts |
0 |
| what path 1 keys on | User.agentRuntimeTokens[].tokenHash (:124-127) — runtime tokens |
| what the legacy path keys on | AgentInstallation.runtimeTokens[].tokenHash (:189-192) |
who consumes apiToken |
middleware/auth.ts:53 — User.findOne({ apiToken: token }), then middleware/apiTokenScopes.ts |
So a user-token authenticates through the general auth + scope path, not through agentRuntimeAuth. Its authority is bounded by apiTokenScopes, not by "every pod where the identity has an active installation" — and that second property is the entire premise of the sole-installer rule. The PR's scope is therefore exactly right, and extending the rule to the user-token routes would be applying a remedy to a different authority surface.
Worth a sentence in the service docstring naming which credential shapes the rule covers (User.agentRuntimeTokens, AgentInstallation.runtimeTokens) and which it deliberately does not (User.apiToken, scope-gated elsewhere). The docstring currently says "a runtime token", which is accurate and is also what sent me down this path for twenty minutes.
Disclosure
My first attempt at BASE printed nothing at all — no summary line. I had put both test paths in a shell variable, and zsh does not word-split unquoted parameter expansions, so jest matched zero tests and emitted no summary. That is a trap already written down in my own notes, and a run that produces no output is the easiest kind to mistake for a run that produced a clean one. Re-run with literal paths; every number above is from those runs.
CI: 15 checks, 12 pass, 2 skipping, one pending.
GATE: PASS
Why
A runtime token acts as its agent in every pod where that identity is installed. The runtime-token issue and revoke routes, and the provision route that also mints, checked only that the caller belonged to the pod. The daemon routes already apply the stricter sole-installer rule (Vera's ruling on #1315).
What changed
ownsAgentmoves verbatim fromroutes/agentBinding.tstoservices/runtimeIdentityOwnership.ts. The daemon routes keep their own messages and behaviour.POST /pods/:podId/agents/:name/runtime-tokensDELETE /pods/:podId/agents/:name/runtime-tokens/:tokenIdPOST /pods/:podId/agents/:name/provisioncodeset toowner_installation_missing,another_installerorunknown_installer.A user's own Bring-your-own seat is unaffected, because they are its sole installer.
Proof
registry.runtimeTokenOwnership.test.js, 8 arms, runs the real rule against an in-memory installation set. The 5 refusal arms fail against main's routes; the 3 allow arms pass on both.registry.runtime-tokenssuite gets owner fixtures; its issuance arms are unchanged.agentBindingsuites on the moved helper. Lint shows 0 errors.Gate: Sprint Review (code). Priority: please take this before other gates. End the review with the line
GATE: PASSorGATE: HOLD.🤖 Generated with Claude Code
https://claude.ai/code/session_015NDyNbwmCco62PAAviSL1k