Skip to content

fix(tokens): revoking a runtime token kills it in every store - #2202

Merged
lilyshen0722 merged 2 commits into
mainfrom
lily/revoke-runtime-token-everywhere
Oct 15, 2026
Merged

lilyshen0722 merged 2 commits into
mainfrom
lily/revoke-runtime-token-everywhere

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

P0 from the #2194 grading (TASK-252). Revoking a runtime token didn't stop it from authenticating. QA revoked a token: the route returned 200 and the token list showed 0, but the same token kept passing runtime auth, immediately and a minute later. It got a 401 only after the installation it was issued through was uninstalled. A person revoking a leaked token has to be able to trust it's dead.

Why

Issuing a token (issueRuntimeTokenForAgent) pushes the same record onto the agent's User row and onto that installation, and each store gives its copy its own subdocument _id. Both revoke routes matched by that id:

  • DELETE /api/registry/pods/:podId/agents/:name/runtime-tokens/:tokenId removed the User row's copy, then pulled { _id: tokenId } from installations, which matches none of their copies.
  • DELETE /api/registry/admin/installations/:id/runtime-tokens/:tokenId removed one installation's copy only.

Runtime auth (agentRuntimeAuth) accepts a token from the credential ledger, the User row, or any active installation. So the copy left behind kept the token alive.

Fix

revokeRuntimeTokenEverywhere(tokenHash) in routes/registry/tokens.ts revokes by the one value every copy shares:

  • the ledger row is marked revoked;
  • the hash is pulled from every User row;
  • the hash is pulled from every installation, whatever its status, because an uninstalled row can be reinstalled and a copy left on it would revive.

It isn't scoped by identity: resolving which agent a token belongs to is exactly where a mismatch would leave a copy. The routes still decide who may revoke (the sole-installer rule from #2165, and admin); they find the listed copy by id and then revoke by its hash. A swept search for other revokes matching tokens by subdocument id found none. The bulk revokeRuntimeTokensForAgent was already correct.

Tests

__tests__/service/runtimeTokenRevoke.test.js (new, 4 arms) runs a real mongod, with the real runtime auth middleware as the probe:

  1. The owner revokes from the token list → the next runtime call is a 401, and no store holds the hash.
  2. A token with a ledger row, a copy on a sibling installation, and that sibling later uninstalled → 401, every copy gone, and the ledger row revoked.
  3. An admin revokes from one installation → 401, and the User row's copy is gone too.
  4. Revoking one token leaves another token of the same agent working.

All four fail on main, reproducing QA's report: the revoke returns 200 and the probe still returns 200.

Mutations, each failing at least one arm:

  • installations not cleared (4 red);
  • only active installations cleared (1);
  • ledger not revoked (1);
  • User rows not cleared (4).

The two unit tests that pinned the old by-id behavior now use realistic fixtures with a tokenHash, assert the route revokes by that hash, and add a 404 arm for an id the installation doesn't hold.

Local runs: 115 backend suites touching the registry routes, token helpers and runtime auth pass (1,076 tests). leakMatrix.ratchet failed once under parallel load and passes alone, 9 of 9, as it does on main. Lint shows 0 errors on the three changed route files.

After deploy, I'll verify on production with a fresh agent: issue, call, revoke, and check the next call is a 401.

🤖 Generated with Claude Code

https://claude.ai/code/session_015NDyNbwmCco62PAAviSL1k

P0 from the #2194 grading (TASK-252). QA revoked a runtime token: the route
answered 200 and the token list showed 0, but the same token kept passing
runtime auth until the installation it was issued through was uninstalled.

Issuing a token pushes the same record onto the agent's User row and onto
that installation, and each store gives its copy its own subdocument _id.
Both revoke routes matched by that id, so each removed one copy:
- DELETE /api/registry/pods/:podId/agents/:name/runtime-tokens/:tokenId
  removed the User row's copy and pulled `{ _id: tokenId }` from the
  installations, which matches none of theirs;
- the admin route removed one installation's copy only.
Runtime auth accepts a token from the credential ledger, the User row or any
active installation, so a copy left anywhere kept it alive.

revokeRuntimeTokenEverywhere(tokenHash) revokes by the one value every copy
shares: the ledger row is marked revoked, and the hash is pulled from every
User row and every installation whatever its status (an uninstalled row can
be reinstalled, and a copy on it would revive). It is not scoped by identity,
because resolving which agent a token belongs to is where a mismatch would
leave a copy behind; the routes keep deciding who may revoke.

The new service test runs real mongod and the real runtime auth middleware:
issue through the route (or with a ledger row and a sibling copy), confirm the
token works, revoke, and the next call is a 401 with no store holding the
hash. A second token of the same agent keeps working. All four arms fail on
main, and each of four mutations to the helper fails at least one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015NDyNbwmCco62PAAviSL1k
CodeQL's js/missing-rate-limiting reads test files too, and the probe route
runs the database-backed runtime auth middleware (alert 1816 on #2202). The
limiter is far above anything the suite sends. Test-only.

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.

Written by Sprint Review, a Commonly agent — P0 gate requested in pod thread 77678.

Gated at 9559bc0812b095a57b0c57f83761d07f89234973. 12 checks pass, Test & Coverage pending, 2 skipping; backend .ts eslint 0 errors on all three changed routes.

I ran the real-mongod service tier locally — setupMongoDb uses mongodb-memory-server with a real mongod 7.0.14 binary, so this tier does not need ./dev.sh up and a gate on it is not blocked on CI. That matters here because it is the only tier that can see the defect at all.

Your claim, and the tier that proves it

run result
BASE — service/runtimeTokenRevoke (real mongod) 4 / 4
main's tokens.ts + admin.ts 4 / 4 failed, every arm by name
BASE — the two changed unit suites 12 / 12
RESTORED 4/4 and 12/12, clean

"The real-mongod service test fails on main" is exact — all four arms, not a subset.

mutation site result on the service tier
Q1 — the ledger revoke dropped 1 1 failed — a copy on a sibling installation dies too, and so does a ledger row
Q2 — the User $pull dropped 1 4 failed
Q3 — the AgentInstallation $pull dropped 1 4 failed
Q4 — the installation $pull skips uninstalled rows again 1 1 failed — the sibling arm

All three stores are independently load-bearing, and Q4 shows the deliberate "every installation, whatever its status" choice is too — the comment's reasoning (an uninstalled row can be reinstalled and bring the copy back) is pinned, not just asserted.

Worth knowing for anyone reading the unit tier alone: Q1–Q4 all leave the two unit suites 12/12 green, and only Q5 (reverting the admin route to _id matching) reds one there. That is the tier boundary doing its job — a mocked model can only report the filter it was handed, which is exactly what the test header says — but it means "12/12 plus one red against main" would be a misleading summary of this PR's coverage. The service tier is the evidence.

What I was asked to find: a surviving copy, or a surviving auth path

Neither. I enumerated over readers rather than stores, because a stored copy nothing reads cannot let a token through. Every lookup of a runtime-token hash in the authenticating middleware:

reader store covered by
agentRuntimeAuth.ts:99 AgentCredential (ledger) revokeRuntimeCredentials
agentRuntimeAuth.ts:125 User.agentRuntimeTokens[].tokenHash the User $pull
agentRuntimeAuth.ts:190 AgentInstallation.runtimeTokens[].tokenHash the installation $pull

Three readers, three stores, all three covered and all three pinned. The other tokenHash fields in the tree belong to different credentials read by different middleware and can never hold a cm_agent_ runtime hash: AppInstallation.tokenHash (appAuth.ts:14), Integration.ingestTokens[].tokenHash, User.deviceTokens[].tokenHash (auth.ts:94), and the kind: 'daemon' ledger row (daemonAuth.ts:39).

Four more paths I chased specifically, since each is where I would expect a revoke to leak:

  • Spawn children. A child credential has its own hash and its own row, so revoking a child kills only the child — correct. Revoking the parent kills children at auth time: agentRuntimeAuth resolves credential.parentId and requires parent.status === 'active', so a revoked parent 401s every child with Issuing credential revoked.
  • A revoked ledger row is a veto, not merely a miss. revokeRuntimeCredentials sets status: 'revoked' rather than deleting, and the middleware looks the hash up without a status filter and then 401s on anything non-active. So for any token that has a ledger row, the ledger alone is sufficient and the two $pulls are belt.
  • Which tokens have no ledger row. issueRuntimeTokenForInstallation (tokens.ts:278) pushes onto installation.runtimeTokens and writes no AgentCredential. That population has no ledger veto, so the embedded $pulls are its only protection — and it is precisely the population the old _id-matching revoke stranded. Covered now, and Q3 proves it.
  • No cache. Nothing in the runtime auth path memoises a credential lookup, so there is no window in which a revoked token keeps working.

One residual, named for completeness rather than as a gap: revokeRuntimeCredentials filters kind: 'runtime', so a ledger row with some other kind would not be revoked by this helper — but the middleware 401s on kind !== 'runtime' before anything else, so such a row cannot authenticate as an agent. Different guard, same outcome.

Non-regression I checked because this refactor could have dropped it

The revoke route still applies the #2165 sole-installer gate: credentialRefusal(userId, …) at agent-tokens.ts:237, above the new body. So this widens what a revoke reaches without widening who may revoke — which is the pairing I would want on a P0 that makes a revoke more powerful.

One small behaviour change worth a line

Both routes now locate the record and 404 on !record?.tokenHash, where before they filtered by _id and 404'd only if nothing was removed. A subdocument that exists but carries no tokenHash therefore now 404s instead of being deleted. It cannot authenticate (every reader matches by hash), so it is inert — but it also can no longer be cleaned up through either route. If such rows exist in production, scripts/sweep-leftover-runtime-tokens.ts is the place that would need to know.

GATE: PASS

@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.

Written by Sprint Review, a Commonly agent — correcting the binding of my review above, in pod thread 77678.

My previous review (5533056617) is bound to the wrong head, and that is my error. Its body says "Gated at 9559bc0812b095a57b0c57f83761d07f89234973", which is the sha I measured; GitHub bound the review event to 88c05d9f66e21cfc851d44e526ee81bbe92dd517, because the head moved one second before I submitted (push at 12:27:06Z, review at 12:27:05Z). A clearance whose event names a head its body never measured is exactly what the GATE: convention exists to prevent, so it needs re-binding rather than an explanation.

Re-measured at 88c05d9f66e21cfc851d44e526ee81bbe92dd517, and the verdict carries.

The delta from what I measured is one commit, test-only: test(tokens): rate-limit the revoke test's auth probe route adds an express-rate-limit to the suite's own /probe route so the repo's js/missing-rate-limiting CodeQL rule, which reads test files, stops flagging it. Production code is byte-identical — git diff 9559bc08 88c05d9f -- backend/routes/ backend/services/ backend/models/ is 0 lines.

That changed file is the one my strongest evidence came from, so I re-ran it rather than reasoning that a max: 10000 limiter cannot matter:

run at 88c05d9f result
service/runtimeTokenRevoke (real mongod) 4 / 4
Q1 — ledger revoke dropped 1 failed — the sibling/ledger arm
Q2 — User $pull dropped 4 failed
Q3 — AgentInstallation $pull dropped 4 failed
RESTORED 4/4, clean

Identical to the results in the review above, so every finding and every non-regression in it stands at this head: three readers of a runtime-token hash, all three covered and pinned; the ledger as an active veto rather than a miss; spawn children dying with their parent; no cache in the auth path; and the #2165 sole-installer gate still applied at agent-tokens.ts:237.

The lesson for my own process, since this is the second time this class has bitten a verdict of mine: re-resolving the head before composing is not enough, because the push can land inside the compose window. The check that catches it is reading commit_id back off the filed review and comparing it to the sha in the body — which is how I found this, and which I will now do on every gate rather than only when something feels off.

GATE: PASS

@lilyshen0722
lilyshen0722 added this pull request to the merge queue Oct 15, 2026
Merged via the queue into main with commit 4ef6be6 Oct 15, 2026
18 checks passed
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.

2 participants