Repository navigation
Commit 545e4d5
[Feat] Support custom MCP OAuth callback paths and WWW-Authenticate discovery (#2219)
* [Feat] Support custom MCP OAuth callback paths and WWW-Authenticate discovery
Some authorization servers register static clients with a callback path
other than /callback. The redirectUri option now accepts any path except
the root path. The local callback server listens on that path.
Some MCP servers host the protected resource metadata outside the RFC 9728
well-known URLs. When the well-known lookup fails, discovery now reads the
resource_metadata URL from the 401 WWW-Authenticate header.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Give MCP OAuth discovery fallback its own timeout and parse resource_metadata case-insensitively
Each protected resource metadata lookup now races its own timeout, so a
slow well-known lookup no longer consumes the budget of the
WWW-Authenticate fallback.
The resource_metadata parameter of the Bearer challenge is matched
case-insensitively, as RFC 9110 requires for authentication parameters.
The probe test now asserts the initialize payload and MCP headers.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Validate MCP protected resource metadata and abort timed-out lookups
Protected resource metadata whose resource does not match the server URL
is now rejected on both the well-known and the WWW-Authenticate fallback
paths (RFC 9728 §3.3), as the SDK does in its own auth flow. The resource
is sent as the RFC 8707 resource indicator, so a mismatched value must not
be used.
Each lookup timeout now aborts the SDK's requests through its fetch hook
instead of leaving them running.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Keep MCP OAuth when the published resource does not match the server URL
A protected resource whose URL did not pass checkResourceAllowed made
discovery return null, so the server looked like it had no OAuth. A
trailing slash alone was enough: https://x/mcp/ for https://x/mcp.
A trailing slash on the published resource is now ignored. Any other
mismatch drops only the RFC 8707 resource indicator, and the server
still uses OAuth.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Find the Bearer resource_metadata after other WWW-Authenticate challenges
A 401 header such as `Basic realm="mcp", Bearer resource_metadata="..."`
lists the Bearer challenge second. The parser only matched a header that
starts with Bearer, so the fallback was skipped. It now also matches a
Bearer challenge that follows a comma.
Adds a test for a resource with multiple trailing slashes.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Read resource_metadata only from the Bearer challenge's own parameters
The parser could skip past the Bearer challenge into a later challenge.
For `Bearer realm="mcp", Basic resource_metadata="..."` it returned the
Basic URL. After Bearer it now skips only auth-params, so a later
challenge stops the match.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Parse WWW-Authenticate without a backtracking regular expression
CodeQL flagged the resource_metadata regular expression: a crafted 401
header could make it backtrack exponentially. The parser now splits the
header on commas outside quoted strings and matches each item against a
single auth-param pattern, tracking the challenge it belongs to.
Behavior is unchanged. Adds the CodeQL example header as a test case.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Handle escaped quotes and invalid URLs in WWW-Authenticate parsing
A quoted auth-param can contain an escaped quote or a comma (RFC 9110
§5.6.4), which broke the split before a later resource_metadata. The
header is now split by a loop that tracks quoted strings and escapes.
An invalid resource_metadata URL no longer stops parsing, so a later
Bearer challenge with a valid URL is still used.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Restrict header-supplied MCP OAuth metadata to the server origin
The WWW-Authenticate fallback fetched any resource_metadata URL, so a
server could direct the client to a loopback, private, or other host.
It now fetches only metadata on the MCP server's own origin. Servers
with cross-origin metadata keep the behavior they had before the
fallback existed.
A well-known lookup that timed out may still be running, so it no
longer starts the fallback.
Adds tests for a failed probe, a failed or timed-out fallback lookup,
no fallback after a timed-out first lookup, and rejected destinations.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [Fix] Do not follow redirects in the MCP OAuth WWW-Authenticate fallback
The same-origin check covered only the resource_metadata URL. A 3xx
response could still lead the initialize probe or the metadata fetch to
a loopback, private, or other host. Both requests now use
redirect: "manual", so a redirect fails the fallback. Well-known lookups
are unchanged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>1 parent c4ed96c commit 545e4d5
6 files changed
Lines changed: 447 additions & 22 deletions
File tree
- src/services/mcp
- __tests__
- utils
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
| 246 | + | |
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
| 251 | + | |
251 | 252 | | |
252 | 253 | | |
253 | 254 | | |
| |||
670 | 671 | | |
671 | 672 | | |
672 | 673 | | |
673 | | - | |
674 | | - | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
675 | 680 | | |
676 | 681 | | |
677 | 682 | | |
678 | 683 | | |
679 | 684 | | |
680 | 685 | | |
681 | 686 | | |
682 | | - | |
683 | | - | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
684 | 690 | | |
685 | 691 | | |
686 | 692 | | |
| |||
689 | 695 | | |
690 | 696 | | |
691 | 697 | | |
692 | | - | |
| 698 | + | |
693 | 699 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
234 | | - | |
| 234 | + | |
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
239 | 262 | | |
240 | 263 | | |
241 | 264 | | |
242 | 265 | | |
243 | | - | |
| 266 | + | |
244 | 267 | | |
245 | 268 | | |
246 | 269 | | |
| |||
563 | 586 | | |
564 | 587 | | |
565 | 588 | | |
566 | | - | |
| 589 | + | |
567 | 590 | | |
568 | 591 | | |
569 | 592 | | |
| |||
614 | 637 | | |
615 | 638 | | |
616 | 639 | | |
617 | | - | |
| 640 | + | |
618 | 641 | | |
619 | 642 | | |
620 | 643 | | |
| |||
1398 | 1421 | | |
1399 | 1422 | | |
1400 | 1423 | | |
1401 | | - | |
| 1424 | + | |
1402 | 1425 | | |
1403 | 1426 | | |
1404 | 1427 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
87 | 104 | | |
88 | 105 | | |
89 | 106 | | |
| |||
0 commit comments