Skip to content

fix(git-auth): fall back to still-valid token on near-expiry refresh failure - #1213

Open
detail-app[bot] wants to merge 3 commits into
mainfrom
detail/bug-fix/fix-git-auth-fall-back-to-still-valid-token-on-nea-f0268a
Open

detail-app[bot] wants to merge 3 commits into
mainfrom
detail/bug-fix/fix-git-auth-fall-back-to-still-valid-token-on-nea-f0268a

Conversation

@detail-app

@detail-app detail-app Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Keep Git authentication working after token refresh fails when the saved access token still has more than 30 seconds of life. Apply this fallback at the refresh failure source only for the Git credential exchange; leave other callers unchanged. Add regression coverage for fallback, the expiry limit, and successful refresh.


Note

Medium Risk
Narrows auth failure handling for Git only, but still allows sending a near-expiry bearer when refresh fails; incorrect lifetime logic could accept tokens too close to expiry.

Overview
Git credential exchange can keep working when OAuth refresh fails but the on-disk access token still has meaningful time left.

Auth: get_valid_token_with_sources accepts an optional refresh_fallback_lifetime. After a failed refresh, if that lifetime is set and expires_at is still more than that many seconds away, the existing access token is returned instead of Not authenticated. Default callers pass None, so behavior is unchanged elsewhere. get_api_token_with_refresh_fallback exposes this only for scoped use.

Git: The /api/git/credentials call uses a 30s HTTP timeout and obtains the bearer via the refresh-fallback path (fallback window = same 30s), then applies it with apply_bearer_auth rather than the standard API auth helper.

Tests: Unit tests updated for the new parameter; integration test near_expiry_token_refresh covers refresh 503 with >30s left (reuse token), ≤30s left (quit), and successful refresh.

Reviewed by Cursor Bugbot for commit 1e86c22. Bugbot is set up for automated code reviews on this repo. Configure here.

@detail-app
detail-app Bot requested a review from LK September 6, 2026 02:26
@detail-app detail-app Bot assigned LK Sep 6, 2026
@detail-app detail-app Bot added the detail label Sep 6, 2026

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note

This report is out of date. Scroll down for Devin Review's latest report on this PR.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale comment

Not approved: this is a functional pb auth git / credential-exchange change, not a small fixup, so it still needs human review. Cursor Bugbot and Cursor Security Agent both completed with no findings that need attention; no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

@akhilles
akhilles force-pushed the detail/bug-fix/fix-git-auth-fall-back-to-still-valid-token-on-nea-f0268a branch from 926f8c6 to 63c31ad Compare September 7, 2026 04:27

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale comment

Not approved: this remains a functional pb auth git / token-refresh fallback change, not a small fixup, so it still needs human review. Cursor Bugbot completed with no findings that need attention; Cursor Security Agent was not running on this update. No reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

@akhilles
akhilles force-pushed the detail/bug-fix/fix-git-auth-fall-back-to-still-valid-token-on-nea-f0268a branch from 63c31ad to ac8bdaf Compare September 7, 2026 14:26

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Service-account fallback is bypassed

With 31–60 seconds remaining, saved_token renews a service-account token and propagates any failure. The Git exchange never receives its still-valid token.

(Refers to this code)

Prompt for agents
Extend the Git-only refresh fallback to saved service-account authentication. In crates/pcb-diode-api/src/auth.rs, get_valid_token_with_sources returns from service_account::saved_token before the human-token fallback logic. AccessToken::is_valid uses a 60-second margin, so a renewal failure with 31–60 seconds remaining rejects a token that still covers the Git exchange's 30-second timeout. Preserve the existing behavior for normal callers and environment credentials, while allowing the Git-specific helper to reuse the saved service-account token only when its post-failure remaining lifetime exceeds the requested fallback lifetime. Add coverage for failed and successful service-account renewal around this window.
Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The observation is correct, but this saved-service-account behavior also exists on current origin/main. This PR fixes the human OAuth refresh-token failure path; it leaves service-account renewal and other callers unchanged. Extending fallback into saved_token would change a separate client-credentials path that re-reads credentials under a lock, so it is not included as a regression fix here. This thread remains open for a human scope/follow-up decision; no service-account behavior change is proposed in this PR.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale comment

Not approved: this remains a functional pb auth git / token-refresh fallback change, not a small fixup, so it still needs human review. Cursor Bugbot completed with no findings that need attention; Cursor Security Agent was not running on this update. No reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

@akhilles
akhilles force-pushed the detail/bug-fix/fix-git-auth-fall-back-to-still-valid-token-on-nea-f0268a branch from ac8bdaf to decaef5 Compare September 7, 2026 15:22

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale comment

Not approved: this remains a functional pb auth git / token-refresh fallback change, not a small fixup, so it still needs human review. Cursor Bugbot completed with no findings that need attention; Cursor Security Agent was not running on this update. No reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

@akhilles
akhilles force-pushed the detail/bug-fix/fix-git-auth-fall-back-to-still-valid-token-on-nea-f0268a branch from decaef5 to 1e86c22 Compare September 7, 2026 16:07

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not approved: this remains a functional pb auth git / token-refresh fallback change, not a small fixup, so it still needs human review. Cursor Bugbot completed with no findings that need attention; Cursor Security Agent was not running on this update. No reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants