Skip to content

Require a numeric port in wildcard Host/Origin matching - #1193

Merged
Kehrlann merged 3 commits into
modelcontextprotocol:mainfrom
anzal1:fix/wildcard-port-digits-only
Oct 20, 2026
Merged

Kehrlann merged 3 commits into
modelcontextprotocol:mainfrom
anzal1:fix/wildcard-port-digits-only

Conversation

@anzal1

@anzal1 anzal1 commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #1192

Motivation and Context

Wildcard-port patterns (localhost:*, http://localhost:*) matched with a bare startsWith(base + ":"), so values like 127.0.0.1:evil.com, localhost:@evil.com and http://localhost:3000.evil.com were accepted. Browsers can't send these headers, so this is hardening and correctness rather than a standalone bypass. Related: modelcontextprotocol/python-sdk#3364. The Swift SDK already requires a digits-only port.

What changed

After the prefix, the remainder must be a non-empty string of ASCII digits. The no-port form (localhost) and exact matches are unchanged. No 1-65535 range check, matching Swift. One side effect: an Origin like http://localhost:8080/path is now rejected; real Origin headers never include a path.

How Has This Been Tested?

New parameterized tests for the Origin and Host wildcard cases (bad suffixes rejected, 127.0.0.1:8080 and http://localhost:3000 still accepted). Reverting the fix makes 16 of them fail. The whole mcp-core module passes (585 tests) with spring-javaformat validation. The Docker-based mcp-test integration tests weren't run locally.

Types of changes

  • Bug fix (hardening, non-breaking)
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • I have read the CONTRIBUTING document
  • My code follows the repository's formatting (spring-javaformat validate passes)
  • I have added tests that prove my fix is effective
  • New and existing mcp-core tests pass locally
  • AI-assisted; reviewed by a human who is accountable for it

anzal1 and others added 3 commits October 16, 2026 23:10
The wildcard-port patterns (e.g. "localhost:*") used a bare prefix match,
so values like "localhost:evil.com" or "http://localhost:3000.evil.com"
were accepted. Require a non-empty ASCII-digits-only port after the prefix.
Reject ports that are out of range (0, 65536) or longer than five
digits, and use the same invalid-port cases for both Origin and Host.

See modelcontextprotocol#1192

Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
@Kehrlann
Kehrlann self-requested a review October 20, 2026 11:27
@Kehrlann
Kehrlann merged commit 7fc1c32 into modelcontextprotocol:main Oct 20, 2026
22 checks passed
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.

Wildcard-port patterns in DefaultServerTransportSecurityValidator accept non-numeric ports

2 participants