Skip to content

fix(registry): only an identity's sole installer, or an admin, can mint, rotate or revoke its runtime token - #2165

Merged
lilyshen0722 merged 1 commit into
mainfrom
fix/runtime-token-owner-only
Oct 14, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
fix/runtime-token-owner-only

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

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

  • ownsAgent moves verbatim from routes/agentBinding.ts to services/runtimeIdentityOwnership.ts. The daemon routes keep their own messages and behaviour.
  • The same rule now guards these routes, before anything is revoked or issued. Admins are exempt, so operator seat renewals keep working.
    • POST /pods/:podId/agents/:name/runtime-tokens
    • DELETE /pods/:podId/agents/:name/runtime-tokens/:tokenId
    • POST /pods/:podId/agents/:name/provision
  • A refusal is a 403 with code set to owner_installation_missing, another_installer or unknown_installer.

A user's own Bring-your-own seat is unaffected, because they are its sole installer.

Proof

  • New suite 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.
  • The existing registry.runtime-tokens suite gets owner fixtures; its issuance arms are unchanged.
  • 20 unit suites and 126 tests that load these routes pass, including all three agentBinding suites 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: PASS or GATE: HOLD.

🤖 Generated with Claude Code

https://claude.ai/code/session_015NDyNbwmCco62PAAviSL1k

…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 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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