Commit Graph

2 Commits

Author SHA1 Message Date
rcourtman
7996b40272 Read diagnostics token inventory under the config lock
The diagnostics API token section iterated config.APITokens and read
HasAPITokens without holding config.Mu. Token writers change that slice
in place under the write lock: scope edits, delete-then-append and
rotation in the token handlers, and ValidateAPIToken on every
token-authenticated request (LastUsedAt and scope normalisation).
Since diagnostics began filtering the token section per caller, that
unlocked read runs on every request from an organisation-bound token,
not only on a cache rebuild, so a scoped token polling diagnostics
while other tokens are in use or edited is a data race. A torn record
can misjudge which tokens the caller may see.

buildAPITokenDiagnostic now iterates a deep copy taken under the read
lock, which covers both the per-caller view and the cached instance
view. The copy helper already existed for Agent Doctor; it is renamed
from actionRunnerCredentialSnapshot to snapshotAPITokenRecords since
it is not specific to action runners. No caller of the builder holds
config.Mu, so taking the read lock there cannot self-deadlock.
Visibility rules are unchanged.

Contract-Neutral: locking fix; diagnostics and Agent Doctor payloads and token visibility are unchanged
2026-10-17 00:18:10 +01:00
rcourtman
74504427df Show diagnostics callers only the API tokens they may manage
/api/diagnostics is readable with settings:read and lists the whole token
inventory: every token's ID, name, timestamps, key hint and which agents
use it, across organizations. A token bound to one organization could
read the other organizations' credential metadata, which the token
management endpoints already refuse to show it.

The diagnostics result is computed once per organization scope and cached
for every caller, so the filter runs after the cache: an API-token caller
that is not instance-wide gets an API token section rebuilt over the
tokens callerTokenMayManage allows, with counts, notes and agent usage
computed over that view. Instance-wide tokens and browser sessions get the
full section, and the cache itself still holds the full result.

Tests cover the builder (tokens, counts, usage) and the handler on the
cached path, including that a scoped caller served from the cache does
not change what the next caller sees. Each filter fails when removed.

Contract-Neutral: Behavioural fix that applies the token visibility rule to diagnostics; no new API surface or contract.
2026-10-11 20:29:30 +01:00