Skip to content

Report the upstream row deleted after a failed persist - #7324

Merged
jhrozek merged 1 commit into
mainfrom
refresher-report-deleted-row
Nov 26, 2026
Merged

jhrozek merged 1 commit into
mainfrom
refresher-report-deleted-row

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

When the IdP rotates a refresh token during an upstream refresh and the new token cannot be saved, the refresher deletes the stale row: the old refresh token is dead, and keeping it would trip the IdP's reuse detection on the next refresh. The error it returns did not say that the row was deleted, so callers that classify a refresh failure by its cause could not tell this case from a transient failure on a row that still exists.

  • The refresher now returns an error that wraps both storage.ErrNotFound and the persist error, once its delete succeeded. If the delete fails too, the row still exists and the error is unchanged.
  • The refresh grant guard already refuses a refresh whose row was deleted while it refreshed (storage.ErrNotFound); it now also covers this case.

Split out of #7323 at review, so it can be reviewed and reverted on its own. The per-user reader in #7323 classifies this error as "needs authorization"; it works without this change, but reports the case as an infrastructure error until this lands.

Type of change

  • Bug fix

Test plan

  • Unit tests, scoped to pkg/authserver with -race
  • Linting (golangci-lint on pkg/authserver)

Refresher tests cover both outcomes of the delete: when it succeeds, the error matches storage.ErrNotFound and the persist error; when it fails, it does not match storage.ErrNotFound. A guard test feeds the real error shape and checks that the guard refuses without revoking.

Does this introduce a user-facing change?

Yes, in the refresh grant guard, which runs whenever an upstream is configured:

  • Before: if a refresh rotated the upstream refresh token but the new one could not be saved, the guard treated the failure as transient and granted the refresh. The client got a new token whose upstream credential was already gone.
  • After: the guard refuses that refresh with invalid_grant and keeps the refresh token, the same as when the row is deleted by anything else while the guard refreshes it.

The guard table in docs/arch/11-auth-server-storage.md lists the case under the deleted-row refusal.

When the IdP rotated the refresh token but the new one could not be
saved, the refresher deletes the stale row, but its error did not say
so. Callers that classify by cause could not tell this from a
transient failure on a row that still exists.

The refresher now returns an error that wraps both storage.ErrNotFound
and the persist error once the delete succeeded. If the delete fails
too, the row still exists and the error is unchanged.

Observable change: the refresh grant guard now refuses (without
revoking) one refresh grant earlier, when a rotated refresh token could
not be saved and its row was deleted. Before, that case looked
transient and the grant was allowed, and the client got a token whose
upstream credential was already gone. The guard table in
docs/arch/11-auth-server-storage.md lists the case under the
deleted-row refusal.

Co-authored-by: Mecatl <[email protected]>
@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 26, 2026 10:52
@jhrozek jhrozek mentioned this pull request Nov 26, 2026
3 tasks done
@github-actions github-actions Bot added the size/XS Extra small PR: < 100 lines changed label Nov 26, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approve: the change is made at the one place that knows the upstream row was deleted, and it has no breaking side effects beyond the stated one.

Design: refreshAndStore is the only code that knows the delete succeeded after a failed persist, so wrapping storage.ErrNotFound there (and only in the delete-succeeded branch) is the right owner and the smallest fix. All callers go through that function. The refresh grant guard now refuses without revoking, which is correct because the row is really gone. GetValidTokens and the handler treat any error as a failed refresh or a re-prompt, so they behave as before. The only upgrade-visible change is that a refresh that rotated the upstream token and then hit a storage failure is refused with a non-revoking invalid_grant instead of being granted with no upstream credential behind it. The PR body states this.

🤖 Reviewed with Claude Code

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.17%. Comparing base (ac27d5e) to head (4e11ba3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7324      +/-   ##
==========================================
- Coverage   82.18%   82.17%   -0.01%     
==========================================
  Files         932      932              
  Lines       94564    94568       +4     
==========================================
- Hits        77715    77714       -1     
- Misses      16844    16849       +5     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek
jhrozek merged commit 84ea6bb into main Nov 26, 2026
48 checks passed
@jhrozek
jhrozek deleted the refresher-report-deleted-row branch November 26, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Extra small PR: < 100 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants