Skip to content

Bound what one /oauth/authorize request stores in its pending connect - #221

Merged
aterga merged 1 commit into
mainfrom
claude/oauth-unbounded-state-challenge-9mb6o4
Nov 3, 2026
Merged

aterga merged 1 commit into
mainfrom
claude/oauth-unbounded-state-challenge-9mb6o4

Conversation

@aterga

@aterga aterga commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

GET /oauth/authorize is unauthenticated, and its pending-connect map is limited only by count (MAX_PENDING_CONNECTS = 1 024, 10-minute TTL). Each entry stored the client's state, code_challenge and requested redirect_uri exactly as sent, so the only limit on an entry's size was hyper's 65 534-byte request-URI limit. That allows about 64 MiB per instance, and twice that in the staging process that serves both /mcp and /mcp-beta (CWE-770).

The registration store already caps redirect URIs and CIMD client_ids at 2 048 bytes for the same reason; these three fields had no cap. This PR adds caps on what one request may store, so a full map holds about 6 MiB per instance.

Severity is low hardening: the growth was already limited to tens of MiB and released at TTL, against MemoryMax=2G. In a local axum::serve harness, 1 024 maximum-size requests raised RSS by about 66 MiB before this change and by about 0.26 MiB after it.

Related issues

None filed; this came from an automated security scan (CWE-770). The scan's per-request ceiling (about 408 KiB, from hyper's max_buf_size) was too high. The URI limit (MAX_URI_LEN, hyper-1.10.1/src/proto/h1/role.rs) applies first, so this PR uses the corrected figures.

Changes

authorize now refuses with 400 invalid_request when any of the three checks below fails. The checks run after the PKCE method check and before the session is minted or the connect is recorded, so a refusal leaves neither behind.

  • code_challenge must be exactly 43 characters of [A-Za-z0-9_-] (new is_s256_challenge).
    • Every value pkce_s256 can produce has this shape, and /oauth/token compares pkce_s256(verifier) with the stored challenge byte for byte.
    • Any other challenge could never be exchanged, so no working client is turned away. The failure just happens before the II sign-in instead of after it.
  • state is limited to MAX_CLIENT_STATE_LEN = 2 048 bytes, measured after decoding.
    • The surveyed MCP clients and OAuth libraries send 22–64 bytes; ChatGPT's base64 JSON is about 170.
    • RFC 6749 sets no maximum.
  • redirect_uri is limited to MAX_REDIRECT_URI_LEN + ":65535".len() = 2 054 bytes.
    • The scan didn't name this field, but without the cap the fix would not lower the ceiling.
    • A loopback redirect is matched on its parsed form (loopback_match, any port per RFC 8252 §7.3) but stored raw. Padding the URL parser discards (tabs, /. segments, a zero-padded port) passed the match and was stored at about 60 KB.
    • A legitimate request is a registered URI (at most 2 048 bytes), or a registered loopback URI with a port added. So 2 054 refuses nothing real.
  • The new consts are documented in the style of MAX_REDIRECT_URIS / MAX_REDIRECT_URI_LEN.
  • The README "Bounded state" section now gives the per-entry byte bound.
  • tests/routers.rs: the DCR round-trip sent code_challenge=abc and expected a 302. It now sends the RFC 7636 Appendix B challenge.

Testing

  • cargo build --locked --all-targets (--workspace, 0 warnings)
  • cargo test --locked --all-targets (--workspace: 75 + 8 + 10 + 203 + 21 passed)
  • cargo fmt --all -- --check
  • cargo clippy --all-targets: nothing in src/ or tests/. The only warnings are 9 existing ones in crates/imcp2-core, which this PR doesn't touch, and CI runs no clippy job.
  • npm test --prefix monitoring/mcp-status: n/a, the dashboard is unchanged.
  • .github/scripts/scan-internal-identifiers.sh origin/main...HEAD: clean.

New tests:

  • is_s256_challenge accepts pkce_s256 output and refuses near misses: 42 or 44 characters, = padding, + and /, non-ASCII, and 300 KiB.
  • For each cap: over-long values (including 300 KiB state and code_challenge, and loopback redirects padded about 60 KB with a zero-padded port, tabs or dot segments) are refused. The test checks that nothing reaches store.authz and that no session is minted. A probe fills the session map to its cap, so a mint would evict a probe entry.
  • Boundaries: a state of exactly 2 048 bytes and a 2 048-byte registered loopback URI on port 65535 are accepted.
  • Real query path (router test): state is measured after percent-decoding, and a %09-padded loopback redirect is refused.

Also checked outside the repo tests:

  • Mutations: disabling each check, or moving the session mint ahead of the checks, makes the matching test fail.
  • Real hyper server: an axum::serve harness, as in src/main.rs. Before this change, every oversized case returned 302 and was stored. After it they return 400, and a 300 KiB URI still gets 414 from hyper.

Checklist

  • I have read the Contributing guidelines.
  • Docs (README / comments) updated for any user-visible change.
  • No secrets, credentials, or internal-only information are included.

Out of scope, possible follow-ups:

  • Registration store: its worst case (10 000 × 16 × 2 KiB ≈ 312 MiB live, cloned and pretty-serialized on each save) is larger than this one.
  • Sign-in eviction: a flood of small authorize requests can evict a real user's pending connect regardless of size. That needs an edge rate limit.
  • Check order: the new length checks could move ahead of validate_client, to skip URL parsing under the registrations lock for oversized requests.

Generated by Claude Code

GET /oauth/authorize is unauthenticated, and the pending-connect map was
bounded only by count (MAX_PENDING_CONNECTS = 1024, 10-minute TTL). Each
entry copied the client's `state`, its `code_challenge` and the requested
`redirect_uri` as sent, so the only per-entry bound was hyper's 65534-byte
request URI: about 64 MiB per instance, and twice that in the staging
process that serves /mcp and /mcp-beta (CWE-770). DCR redirect URIs and
CIMD client ids were already capped at 2048 bytes; these fields were not.

authorize now refuses with 400 invalid_request, after the PKCE method check
and before the session is minted or the connect recorded, so a refusal
leaves neither behind, when:

- code_challenge is not exactly 43 characters of [A-Za-z0-9_-]. That is
  the shape of every value pkce_s256 can produce, and /oauth/token compares
  pkce_s256(verifier) with the stored challenge byte for byte. Any other
  challenge could never be exchanged, so the rule turns away no working
  client. It only moves the failure ahead of the II sign-in.
- state is over MAX_CLIENT_STATE_LEN = 2048 bytes (decoded). Real clients
  send 22 to about 170 bytes.
- redirect_uri is over MAX_REDIRECT_URI_LEN + ":65535".len() = 2054 bytes.
  The finding named only state and code_challenge, but a loopback redirect
  is matched on its parsed form (loopback_match) and stored raw. Tabs, "/."
  segments or a zero-padded port that the URL parser discards would
  otherwise still let one request store about 64 KiB, leaving the ceiling
  where it was. A legitimate request is a registered URI (at most 2048
  bytes) or a registered loopback URI with a port added, so 2054 refuses
  nothing real.

A connect's strings now total at most about 6 KiB (a CIMD client_id of
2048 bytes plus the three caps), so a full map holds about 6 MiB per
instance. The README's "Bounded state" section says so.

Tests: the shape check against pkce_s256 output and its near misses (42 or
44 characters, `=` padding, `+` and `/`, non-ASCII, 300 KiB). For each
refused case (a malformed challenge, an oversized state, a padded loopback
redirect), a check that no pending connect is recorded and no session is
minted. The session check works by filling the session map to its cap, so
that any mint evicts a probe entry; the accepted cases show the probe does
see a mint. Also covered: a state exactly at the cap, the RFC 7636
Appendix B challenge, and the longest legitimate redirect (a 2048-byte
registration requested on port 65535), all accepted. The router test now
sends a real S256 challenge in place of `abc`, and checks over the real
query path that state is measured after percent-decoding and that a
%09-padded loopback redirect is refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Qk1kkyoa76QU6pgSGTv81Y

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.

🟢 Approval recommended

No active review issues block approval of the OAuth authorization endpoint hardening change.

No open issues 🎉

What changed in this PR

This PR hardens the unauthenticated OAuth authorization endpoint by bounding request-controlled data stored in pending connects, reducing their documented maximum storage from about 64 MiB to 6 MiB per instance.

Changes:

  • Validates S256 challenge shape and caps decoded state and redirect URI lengths before creating a session or pending connect.
  • Adds rejection, boundary, and router-level decoding tests, including checks that rejected requests leave no session or pending authorization.
  • Documents the per-entry limits and memory bound.
File Description
tests/​routers.rs Uses a valid PKCE challenge and tests decoded state limits and padded redirects.
src/​auth.rs Adds authorization field limits, S256 validation, and regression tests.
README.md Documents pending-connect size limits and the resulting memory bound.

Review effort: Balanced

@aterga
aterga marked this pull request as ready for review November 3, 2026 13:44
@aterga
aterga requested a review from a team November 3, 2026 13:44
@aterga
aterga merged commit d3e2dcd into main Nov 3, 2026
17 checks passed
aterga added a commit that referenced this pull request Nov 3, 2026
All three crates share one version. A patch bump: nothing since 0.6.0
changes the crates' public API or refuses anything 0.6.0 allowed. It
carries the mcp_get_delegation retry on a lagging replica (#220), the
bound on what one /oauth/authorize request stores (#221), the reworded
open_app and call tool descriptions and server instructions (#226), and
clippy fixes (#224).

Co-authored-by: Claude Opus 5.5 <[email protected]>
aterga added a commit that referenced this pull request Nov 10, 2026
Brings the branch up to date with 27 commits on main. Two of them needed
changes on this side:

- #221 validates the PKCE code_challenge at /oauth/authorize, so the
  router test's branding round trip now authorizes with a well-formed S256
  challenge (the conflict in tests/routers.rs).
- #234 reads pending connects on tokio's clock and keeps expired ones
  around so the redeem gate can say "expired". branding_is_bound_to_the_session
  now runs on a paused clock: it walks a connect past CONNECT_TTL and checks
  that an expired but still-retained connect gets no branding, replacing
  the old checked_sub check that some hosts skipped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

4 participants