Skip to content

Fix prod connector: floor the OAuth token TTL so the grant hint can't mint a born-invalid token - #45

Closed
aterga wants to merge 1 commit into
mainfrom
claude/mcp-spec-compliance-9202ka
Closed

aterga wants to merge 1 commit into
mainfrom
claude/mcp-spec-compliance-9202ka

Conversation

@aterga

@aterga aterga commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Incident

The production connector (id.ai, on v1) intermittently failed to complete OAuth: the browser reached ChatGPT's callback (…/connector/oauth/…?code=mcp-code-…&state=…) but never redirected back — it stuck on "Redirecting back to the agent…". Retrying usually worked ("worked on the 2nd attempt").

Root cause

Not the Phase-2 registration-delegation work — that is flag-gated off for prod, so the connect protocol is unchanged. The regression is in the shared OAuth token path.

The access-token lifetime was changed to track the II grant expiration (so the session duration the user picks is honored instead of a flat 1h). But that expiration arrives on the connect-completion POST — a best-effort, unauthenticated message that races the token exchange — and it was used verbatim, with no floor, for both the client's expires_in and the server's internal token-validity gate (TokenInfo.ttl, checked at auth.rs info.created.elapsed() < info.ttl):

Some(exp_ns) => Duration::from_nanos(exp_ns.saturating_sub(now_ns))

When that value lands at/near/behind the server clock (clock skew, or a stale/dropped POST that won the race), saturating_sub collapses to ~0, so the token is born invalid. ChatGPT's first authenticated MCP call then 401s and the flow never leaves the callback page. When the completion POST instead lost the race, the TTL fell back to the default 1h and the connect succeeded — hence the intermittent "2nd attempt works."

The pre-change code couldn't hit this: the internal TTL was always a solid 1h, never derived from the racy hint.

Fix

Make the grant expiration asymmetric in token_ttl:

Some(exp_ns) if exp_ns > now_ns => Duration::from_nanos(exp_ns - now_ns).max(default),
_ => default,
  • It may extend the token beyond the default (a week-long grant still mints a week-long token — the fixed-1h-cap removal is preserved).
  • It may never shorten the token below the default. An at/near/behind-now expiration now falls back to the default instead of zero, so a token is never born invalid.
  • The grant remains the hard ceiling at II (a signed call 401s once it truly lapses), which drives the clean re-auth the shortening was meant to achieve.

Behavior note

One deliberate change from the previous behavior: a grant shorter than the default (e.g. the user picks 10 min) now yields a client token floored at the default rather than a matching short token. This is intentional — a best-effort, unauthenticated, racy hint should never shorten the client's token, and II's hard ceiling still stops the token from acting past the real grant. If you'd rather keep short client tokens exactly matching short grants, we can instead floor at a small minimum (e.g. a few minutes) to kill only the born-invalid case; say the word.

Testing

cargo test — 79 pass (renamed/expanded token_ttl_… test now covers: long grant → full remaining, short grant → floored at default, and expiration at/behind now → default not zero). No new clippy warnings (the 3 that remain are pre-existing in main.rs/management.rs). Only the production-shared token path changes; Phase-2 and per-instance gating are untouched.

🤖 Generated with Claude Code

https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym


Generated by Claude Code

…ant hint

The OAuth access-token lifetime was changed to track the II grant
expiration (matching the session duration the user picks). But that
expiration arrives on the connect-completion POST — a best-effort,
unauthenticated message that races the token exchange — and it was used
verbatim for BOTH the client's `expires_in` AND the server's internal
token-validity gate (`TokenInfo.ttl`), with no floor:

    Some(exp_ns) => Duration::from_nanos(exp_ns.saturating_sub(now_ns))

When that value lands at/near/behind `now` (clock skew, or a stale/dropped
POST that won the race), `saturating_sub` yields ~0, so the token is born
invalid. The client's first authenticated MCP call then 401s and the OAuth
flow never leaves the callback page ("Redirecting back to the agent…").
When the completion POST instead lost the race, the TTL fell back to the
default 1h and the connect succeeded — the intermittent "worked on the 2nd
attempt" the production (id.ai) connector showed.

Make the grant expiration asymmetric: it may EXTEND the token beyond the
default (a week-long grant still mints a week-long token — no fixed 1h cap)
but never SHORTEN it below the default. An at/near/behind-`now` expiration
now falls back to the default instead of zero, so a token is never born
invalid. The grant remains the hard ceiling at II (a signed call 401s once
it truly lapses), which drives the clean re-auth the shortening was meant
to achieve.

Only the production-shared token path changes; the Phase-2 flow and per-
instance gating are untouched. Adds a regression test covering the
at/behind-`now` and short-grant cases.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
@aterga
aterga marked this pull request as ready for review July 9, 2026 20:47
Copilot AI review requested due to automatic review settings July 9, 2026 20:47

Copilot AI 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.

Pull request overview

Fixes an intermittent OAuth completion failure in the production connector by preventing “born-invalid” access tokens when the grant-expiration hint arrives at/behind server now (race/clock-skew). The change floors the computed token TTL at the default value, ensuring the first authenticated MCP call doesn’t immediately 401 and stall on the OAuth callback page.

Changes:

  • Update token_ttl to apply an asymmetric rule: grant expiration may extend TTL beyond the default, but may not shorten it below the default.
  • Expand/rename the unit test to cover long-grant extension, short-grant flooring, and at/behind-now fallback behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/auth.rs
Comment on lines +1402 to +1404
/// The access-token lifetime. The II grant is the hard ceiling at II (a signed
/// call 401s once it lapses), so this governs only the CLIENT's view, and it
/// does so asymmetrically: a grant that outlasts the default may **extend** the

aterga commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator Author

Closing this.

Investigation of the production connector incident showed the "born-invalid token" scenario this PR guards against is not what was happening: every access token actually issued during the incident window had a healthy lifetime (ttl_secs of 3600 or 604800), never ~0. The real cause was in-memory session/token/code state being wiped by repeated deploy-restarts — a separate fix (persistence / deploy hygiene) tracked elsewhere.

On top of not fixing the incident, this PR's .max(default) floor would have overridden legitimately-short grants (a 10-minute session would mint a 1-hour token), partially reverting the "token tracks the grant the user picked" behavior from #43 — to defend an edge case (grant expiration ≤ now) that the data shows isn't occurring.

If a guard against a zero-TTL token is ever wanted (e.g. against clock skew reporting exp ≤ now), the surgical version is a one-liner — exp ≤ now → default, leaving exp > now tracking the grant exactly — with no behavior regression. Not merging that now since it's unobserved and low-priority.


Generated by Claude Code


Generated by Claude Code

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.

3 participants