Skip to content

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

Open
anzal1 wants to merge 1 commit into
modelcontextprotocol:mainfrom
anzal1:fix/wildcard-port-digits-only
Open

anzal1 wants to merge 1 commit into
modelcontextprotocol:mainfrom
anzal1:fix/wildcard-port-digits-only

Conversation

@anzal1

@anzal1 anzal1 commented Oct 16, 2026

Copy link
Copy Markdown

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

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.

This branch has not been deployed

No deployments
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

1 participant