Repository navigation
fix(coderd): skip validation for unexpired chat git tokens - #31921
Conversation
06f5bab to
b28f38f
Compare
This comment has been minimized.
This comment has been minimized.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
This PR makes resolveChatGitAccessToken skip Config.RefreshToken for stored tokens that pass OAuthToken().Valid(), which removes one provider validation request per call. Findings: 7 P3, 1 P4, 2 Nit.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/exp_chats.go:5014: whenoriginmatches config A and the chat owner has no usable link for A, the fallback loop returns the owner's token for another config B, andgetChatDiffContents(exp_chats.go:4767) andresolveChatDiffReference(exp_chats.go:4828) send that token to A's provider API, for example a github.com token sent to a GitHub Enterprise host.coderd/exp_chats.go:5049: when refreshing an expired token fails, the resolver returns the expired token, so the caller's next provider API request fails with a provider error instead of the caller receivinggitsync.ErrNoTokenAvailable.
Notes:
coderd/exp_chats.go:5045:hasConfigis always true, becauseconfigsandproviderIDsare built fromapi.ExternalAuthConfigsin the same loop.coderd/exp_chats_internal_test.go:49: withexp_chats.gorestored to the base SHA, theUnexpiredandNoExpirysubtests fail with one provider validation request; at head all 8 subtests pass.coderd/exp_chats.go:5000: the resolver now repeats thetoken.Valid()check thatRefreshTokenuses to decide whether to refresh (externalauth.go:285). IfRefreshTokenchanges that trigger, this resolver keeps skipping tokens thatRefreshTokenwould refresh.
🤖 This review was automatically generated with Coder Agents.
| if !link.OAuthToken().Valid() { | ||
| //nolint:gocritic // System context carried through for token refresh. | ||
| refreshed, refreshErr := config.RefreshToken(dbauthz.AsSystemRestricted(ctx), api.Database, link) | ||
| if refreshErr == nil { |
There was a problem hiding this comment.
P3 [CRF-4] When token refresh succeeds and provider validation then fails, the resolver returns the expired pre-refresh token although the refreshed token is already stored. (Mafuuu, Mafu-san, Leorio, Knov)
refreshAndValidateToken (coderd/externalauth/externalauth.go:485-509) writes the new token, then returns updatedAuthLink, err when validateWithRetry fails, and both loops assign link = refreshed only when refreshErr == nil (also exp_chats.go:5056). A scratch test with an expired stored token, a succeeding /token, and a /validate returning 401 gave returned=stored-access-token stored=refreshed-access-token. After this PR every RefreshToken call in this function is a refresh, and a rate-limited validation endpoint, the condition this PR targets, makes this case more likely. The decision log defers this without a ticket; in both loops, assign link = refreshed when refreshed.OAuthToken().Valid(), whatever refreshErr is.
🤖
There was a problem hiding this comment.
The observed returned-token behavior is real, but both loops already do this on main. The PR intentionally preserves that policy, and its decision log describes the new-token-stored/old-token-returned effect as a separate follow-up. Adopting the refreshed link despite a validation error would change production token selection beyond removing unnecessary provider validation, so that behavior remains unchanged here.
🤖 Posted using
/amend-reviewskill via Coder Agents.
There was a problem hiding this comment.
Moved to out of scope: main has the same outcome, and this PR only re-indents the if refreshErr == nil lines. It still has no ticket, so a human needs to file one or accept the behavior. The doc comment describes this case incorrectly; that is on the doc comment thread.
🤖
There was a problem hiding this comment.
Agreed that this is inherited behavior outside this PR's scope, as your latest reply confirms. The existing decision log explicitly records the returned-token effect as a separate follow-up. Keeping production token selection unchanged here and leaving this thread open.
🤖 Posted using
/amend-reviewskill via Coder Agents.
b28f38f to
6f90bb2
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Chat git token reads validate unexpired tokens even though the resolver returns the stored token when validation fails. These provider requests consume GitHub quota without changing the token used for git requests. Refresh only tokens that oauth2 considers expired. Keep shared token validation and refresh-failure behavior unchanged.
Chat token consumers make provider API requests, not git transport requests. Name that access-check boundary in the resolver comment.
Keep chat token tests focused on provider traffic and returned tokens. Shared refresh validation counts and lease calls are not chat contracts. Use one PostgreSQL fixture with isolated users to check persisted tokens without scripting the shared refresh implementation.
Provider selection and the local expiry check are separate decisions. Keep their conditions explicit without changing token refresh or failure handling.
Keep provider setup outside the resolver subtests so token selection, token refresh requests and persistence can be read together. Name the OAuth configuration and token response fields explicitly.
The chat token fixture accepts a context, so place it first to follow the repository's Go API convention enforced by revive.
Pin the stored-token fallback for invalid-grant errors in both resolver lookup paths. Keep token selection and provider validation policy unchanged while documenting that contract at the resolver boundary.
RefreshToken can persist a new access token before provider validation returns an error. Name the returned value by when it was read instead of implying that it always matches the current database row.
6f90bb2 to
fb8d7a1
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Round 3 reviews fb8d7a1, which rewrites the resolver doc comment; no new findings. The doc comment now matches both loops for every RefreshToken error path, and no in-scope findings remain open.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/exp_chats.go:5003: when refresh succeeds and validation fails, both loops return the expired pre-refresh token while the row holds the refreshed one, so one chat git request fails per occurrence. The decision log calls it a follow-up but links no ticket.coderd/exp_chats.go:5014: when the owner has no usable link for the origin's provider, the fallback returns another provider's token, whichgetChatDiffContentsandresolveChatDiffReferencesend to the origin's provider API.coderd/exp_chats.go:5049: when refreshing an expired token fails, the resolver returns the expired token, so callers get a provider error instead ofgitsync.ErrNoTokenAvailable.coderd/exp_chats.go:5046: the origin and fallback loops duplicate the read, refresh, and return sequence and differ in how they logrefreshErr, so any token-selection change must be made in both.
🤖 This review was automatically generated with Coder Agents.
Distinguish provider validation from the local expiry check and describe its ten-second and no-expiry rules explicitly. Keep token selection and error handling unchanged.
On
main,resolveChatGitAccessTokencallsConfig.RefreshTokenfor every stored access token it reads. WhenOAuthToken().Valid()is true,RefreshTokenstill sends a provider validation request if a validation URL is configured. The resolver returns the stored access token whether validation succeeds or fails, so these requests count against the provider's API rate limit without changing token selection.Skip
RefreshTokenwhenOAuthToken().Valid()is true. The check treats a token within 10 seconds of expiry as expired. A token with no expiry counts as valid if its access token is non-empty. OtherRefreshTokencallers still perform provider validation.Verified with:
TestResolveChatGitAccessToken,TestGetChatDiffStatus,TestGetChatDiffContents,TestRefreshToken/SaveBeforeValidateandmake pre-commit.Part of CODAGT-1543.
Decision log
RefreshTokencallers depend on provider validation. DirectTokenSourcetoken refresh would bypass existing coordination and persistence.OAuthToken().Valid()send no provider validation requests. Provider validation after token refresh is covered bycoderd/externalauthtests, includingTestRefreshToken/SaveBeforeValidate.RefreshTokenhas already stored the new access token, yet the resolver returns the expired pre-refresh access token. This PR preserves both behaviors. The matching-origin path does not logRefreshTokenerrors, while the fallback path logs them at debug level; leave that difference for this follow-up.