Repository navigation
Service accounts: "Last used" counts only the requests that run as the account - #914
Merged
Merged
Conversation
…e account The trusted-header realm recorded a use of the account its headers name when the middleware resolved the principal, before the handler read a credential of the request's own in its place: a Bearer API token on /repository, a registry token on /v2. The middleware now records the use after the handler has answered, unless the handler says another credential replaced the principal (or was refused). Fixes #586 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012e6DJacez28ygnEtA1z3mF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #586
The bug
The trusted-header realm recorded a use of the account its headers name (
touch_last_authenticated) inTrustedHeaderSource::signed_in, when the principal middleware resolved the principal. Two handlers read a credential of the request's own after that and run the request as someone else:/repository:with_bearer(Trusted-header realms: a Bearer API token is ignored on /repository when the proxy's headers name the caller #555) resolves a Bearer API token (npm's_authToken);/v2:Registry::callerresolves a registry Bearer token.So behind a proxy that names a network's service account, every npm or docker request with its own token moved that account's "Last used".
The fix
TrustedHeaderSourceno longer writes when it resolves the principal. The write isTrustedHeaders::record_use, a small trait overPrincipalSourcethat the middleware'strusted_headersource now implements.NamedAccountUseextension on a request whose principal the headers named, and records the use after the handler has answered, unless the handler marked the principal as replaced./repositoryand/v2mark it when the principal they run the request as is not the one the headers named, or when the request's own token is refused (the request then runs as nobody). A registry token that names nobody (the one the headers alone get from/v2/token) leaves the request running as the header account, and that is still recorded.I looked for other places a later credential replaces the trusted-header principal and found none: every other handler takes the middleware's principal as it is, and the token-exchange and device-grant endpoints skip the credential chain altogether.
Tests
crates/server/tests/trusted_header.rs,a_request_with_its_own_credential_is_no_use_of_the_account_the_headers_name: through a real trusted-header realm, a feed request with a Bearer token, one with a refused token,docker login, a registry token and a refused registry token leavelast_auth_source_atof the header account unset; the headers alone (feed,/v2/,/v2/token, and beside the anonymous registry token) set it. Before the fix it failed at the first assertion ("a feed's Bearer token").middleware::principal::tests::a_use_of_the_named_account_is_recorded_unless_the_handler_replaced_it: the middleware's side with a stub source.Ran locally:
cargo fmt --all --check,cargo clippy --workspace --all-targets -- -D warnings,cargo xtask changelog --check,cargo xtask doc-check,cargo xtask unused,cargo xtask docs --check, andcargo test -p forge-server --lib --test trusted_header --test service_accounts --test registry --test alias_base_urls(all green).Neighbouring issues, not fixed here
UPDATE): this moves thatUPDATEfrom before the handler to after it and drops it for requests with their own credential; a request that runs as the header account still sends one. Users: GET /admin/users groups the whole token table, and every trusted-header request sends a last-authenticated UPDATE #608 stays open./v2): such a request still runs as the account the headers name, so it is still recorded as a use of it. Whatever Registry: behind a trusted-header realm's proxy, a Bearer that is no registry token is ignored on /v2, and the request runs as the account the headers name #610 decides should markNamedAccountUsethe same way.Web UI
No admin API change: Admin → Service accounts shows the same
last_used_at, now with the right value.Generated by Claude Code