Skip to content

fix(cloudflare): Probe upstream before forcing token re-auth on expiry#856

Merged
dcramer merged 7 commits intomainfrom
dcramer/fix/probe-upstream-before-token-expiry
Mar 24, 2026
Merged

fix(cloudflare): Probe upstream before forcing token re-auth on expiry#856
dcramer merged 7 commits intomainfrom
dcramer/fix/probe-upstream-before-token-expiry

Conversation

@dcramer
Copy link
Copy Markdown
Member

@dcramer dcramer commented Mar 23, 2026

Instead of immediately forcing re-authentication when the local clock
says the OAuth token is expired, probe GET /api/0/users/me/ to verify
the token is actually invalid. This avoids unnecessary logouts when the
stored expiry timestamp is stale or the clock is skewed.

Restores the env parameter to tokenExchangeCallback (removed in
b2d46e2 when it became unused) so the callback can read SENTRY_HOST.

Adds a success_probed metric outcome to distinguish probe-verified
tokens from clock-verified ones. Network errors during the probe fail
closed (force re-auth).

Instead of immediately forcing re-authentication when the local clock
says the token is expired, probe GET /api/0/users/me/ to verify the
token is actually invalid. This avoids unnecessary logouts when the
stored expiry is stale or the clock is skewed.

Restores the env parameter to tokenExchangeCallback for SENTRY_HOST
access. Adds a new "success_probed" metric outcome to distinguish
probe-verified tokens from clock-verified ones.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace raw fetch with SentryApiService.getAuthenticatedUser() for the
upstream token validity check. Merge probed-success into the regular
success metric outcome, and extend the probe-issued TTL to 1 hour
since a 401 from Sentry will trigger re-auth anyway.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Log unexpected errors (network failures, server errors) when probing
upstream token validity, while silently handling expected 401s.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dcramer dcramer marked this pull request as ready for review March 24, 2026 16:18
dcramer and others added 3 commits March 24, 2026 09:24
Add end-to-end tests that exercise the full refresh token flow:
issue tokens → refresh → use refreshed token for MCP request.

Tests both paths: clock-valid refresh (fast path) and clock-expired
refresh with upstream probe (slow path). Both verify the refreshed
token actually works for subsequent MCP requests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove the fast-path and probe-success unit tests from helpers.test.ts
since the integration tests in mcp.test.ts now cover these flows
end-to-end. Keep the edge case unit tests (no refresh token, network
error, safety window, missing expiry) that integration tests don't
exercise.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use outcome "success_probed" for tokens verified via upstream probe,
keeping "success" for the clock-verified fast path. This makes the
two paths distinguishable in metrics dashboards.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Add refreshToken to DEFAULT_WORKER_PROPS so tokenExchangeCallback
doesn't early-return before reaching the probe logic. Add assertion
that the upstream API probe was actually called during the expired-clock
refresh test.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dcramer dcramer merged commit e98836b into main Mar 24, 2026
15 checks passed
@dcramer dcramer deleted the dcramer/fix/probe-upstream-before-token-expiry branch March 24, 2026 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant