Skip to content

OAuth discovery drops explicit default ports from advertised issuer identifiers #1420

Open

Description

@cdasilvacosta

Summary

OAuth discovery rejects internally consistent metadata when the advertised authorization-server issuer contains an explicit default port, such as https://auth.example.com:443/tenant1.

The original identifier is parsed into a Url, which removes :443. The serialized URL is then used as the expected issuer, while AuthorizationMetadata.issuer retains the original JSON string.

Reproduced against main at c742b5536724ec376b14b7ddcbbd024b7d6ae163 (rmcp 3.8.0). The same code is present at 98a54789b80110ad210495c4ad845c89e1980471, the rmcp revision pinned by Codex 0.166.0.

Reproduction with simulated metadata

Using the existing RecordingOAuthHttpClient test helper, return these responses during AuthorizationManager::resolve_metadata():

  1. An initial 401 for the MCP resource https://mcp.example.com/.
  2. Protected resource metadata:
{
  "resource": "https://mcp.example.com/",
  "authorization_servers": ["https://auth.example.com:443/tenant1"]
}
  1. Authorization-server metadata:
{
  "issuer": "https://auth.example.com:443/tenant1",
  "authorization_endpoint": "https://auth.example.com/tenant1/authorize",
  "token_endpoint": "https://auth.example.com/tenant1/token"
}

No real OAuth credentials or external service are needed.

Expected: discovery accepts the metadata because its issuer exactly matches the identifier advertised by protected resource metadata.

Actual:

Authorization server issuer mismatch: expected https://auth.example.com/tenant1, received https://auth.example.com:443/tenant1

Root cause

In crates/rmcp/src/transport/auth.rs, authorization_metadata_from_resource_metadata() parses the candidate with Url::parse(candidate), then calls:

.try_discover_oauth_server(&candidate_url, Some(candidate_url.as_str()))

candidate_url.as_str() has already lost the explicit default port. Parsing is appropriate for transport, but its normalized output no longer represents the original OAuth identifier.

This also creates an asymmetric check: an advertised issuer containing :443 can incorrectly match metadata that omits it, because the expected value has been normalized first.

Relation to #1144 / #1145

#1145 preserves the issuer through discovery to address the trailing-slash problem in #1144, but retains the parsed URL representation. That preserves a tenant's trailing slash while still losing an explicit default port and host casing.

I did not find an existing issue specifically covering this default-port case. #1268 concerns HTTP Origin validation and is a separate problem.

Proposed approach

Preserve the advertised absolute issuer string for identity validation, separately from the parsed URL used to construct discovery requests. Keep the existing resolved-URL behavior for legacy relative references.

The fix should not normalize both issuers or treat identifiers with and without :443 as interchangeable. RFC 8414 sections 3.3 and 4 require validation against the original issuer identifier.

Local validation and question

A local candidate fix has mocked regression coverage for matching and mismatched default ports, nonstandard ports, host casing, tenant paths and trailing slashes, including the complete metadata discovery path. A before/after comparison of OAuth and HTTP tests found no new failures; full upstream CI has not been run.

Would preserving the original advertised identifier be an appropriate follow-up to #1145 before submitting a PR?

AI assistance was used for the investigation and local regression tests. All examples above use fictional domains and tenants.

Activity

  1. added
    bug
    P1
    T-security
    T-transport
    ready for work
    on Oct 31, 2026
  2. github-actions commented on Oct 31, 2026

    @github-actions
    Contributor

    @gocamille is on triage rotation this week and is the reviewer for this one.

    Codex labeled this bug / P1 (T-security, T-transport). Please confirm or correct those labels.

    Automated triage. Labels are a first pass and the assigned maintainer has the final say.

  3. cdasilvacosta commented on Oct 31, 2026

    @cdasilvacosta
    Author

    TypeScript SDK comparison: v2 accepts the matching :443 case

    The TypeScript v2 SDK accepts the case reported in this issue: when protected-resource metadata and authorization-server metadata both publish exactly https://auth.example.com:443/tenant1, discovery succeeds with issuer validation enabled. It also rejects all issuer mismatches tested below.

    The difference from the affected Rust code is how the expected issuer is retained:

    • TypeScript v2: keeps the original advertised string, including :443, for issuer validation. A separately parsed URL is used to construct HTTP requests; normalization of that URL does not change the expected issuer.
    • Affected rmcp code: parses the advertised identifier, then uses candidate_url.as_str() as the expected issuer. Parsing has already removed :443, so metadata containing the original, matching identifier is incorrectly rejected.

    This supports preserving the original advertised issuer string in rmcp, separately from the parsed transport URL, while keeping issuer validation enabled.

    Test setup and results

    I tested the distributed npm packages @modelcontextprotocol/sdk@1.32.2 (v1) and @modelcontextprotocol/client@2.4.0 (v2), using Node.js 24.19.0 and pinned dependencies.

    The tests call the SDK implementations directly through two public entry points:

    • discoverOAuthServerInfo(): protected-resource metadata followed by authorization-server metadata discovery.
    • auth(): the initial OAuth flow through the redirect result, intercepted by a mock provider. No token exchange was performed.

    Only HTTP responses were mocked, via an injected fetch function. No issuer-validation bypass was enabled, and no real network requests or credentials were used. Results were identical for both entry points:

    Advertised identifier versus metadata issuer v1 1.32.2 v2 2.4.0
    Identical, without an explicit port Accepts Accepts
    Both exactly https://auth.example.com:443/tenant1 Accepts Accepts
    Identical, with :8443 Accepts Accepts
    :443 added or removed only in metadata Accepts Rejects with IssuerMismatchError
    :8443 versus :9443 Accepts Rejects
    Different domain Accepts Rejects
    Different tenant path Accepts Rejects

    The v1 result needs a qualification: these v1 discovery paths do not check the metadata issuer against the discovery identifier. Acceptance of the matching :443 case alone therefore does not demonstrate correct strict validation. V2 accepts that case while also rejecting the tested mismatches.

    One additional v2 test deliberately passed an already-normalized URL object to discoverAuthorizationServerMetadata(). Because new URL('https://auth.example.com:443/tenant1').href omits :443, that input no longer contains the original identifier, and metadata retaining :443 is rejected. This does not occur in the tested full protected-resource discovery flow, which preserves the original string and succeeds. The additional test only illustrates the information lost when a caller supplies an already-normalized identifier.

    The runner executed 33 tests: 23 passed and 10 failed. All 17 v2 tests passed. The 10 v1 failures are assertions expecting rejection of the five mismatches above, exercised through both entry points; v1 accepted them instead.

    This comparison covers simulated discovery and initial authorization only, not a complete live OAuth login.

  4. thelastbackspace commented on Nov 5, 2026

    @thelastbackspace

    I took a look at this. In authorization_metadata_from_resource_metadata, the expected issuer comes from the parsed Url, so the :443 gets dropped before the comparison. I've got a small fix that compares against the advertised string instead, plus regression tests for the matching and mismatching :443 cases. OK if I open a PR?

  5. jokemanfire commented on Nov 7, 2026

    @jokemanfire
    Member

    @/tmp/issue-1420.md

  6. tsarlandie-oai commented on Nov 24, 2026

    @tsarlandie-oai
    Contributor

    @thelastbackspace thanks for reporting - can you send your PR for review?

  7. thelastbackspace commented on Nov 24, 2026

    @thelastbackspace

    Thanks, opened #1498.

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

Metadata

Metadata

Assignees

Labels

P1T-securityT-transportbugready for work

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions