Skip to content

fix: accept loopback Host and Origin without a port in OriginValidator.localhost() - #335

Open
Perennos wants to merge 1 commit into
modelcontextprotocol:mainfrom
Perennos:fix/localhost-validator-default-port
Open

Perennos wants to merge 1 commit into
modelcontextprotocol:mainfrom
Perennos:fix/localhost-validator-default-port

Conversation

@Perennos

Copy link
Copy Markdown

OriginValidator.localhost() builds wildcard patterns such as localhost:*, and matchesPattern requires a :<digits> suffix after the host. A request with Host: localhost (or Origin: http://localhost) is therefore rejected with 421/403. A missing port means the scheme's default port (RFC 9110, section 7.2), so a server bound to port 80 rejects its own loopback requests.

This change makes the wildcard also match the bare host. localhost(port:) is unchanged, and non-loopback hosts are still rejected.

Tests: adds OriginValidatorTests, covering accepted loopback forms with and without a port, rejected hosts and origins, and the port-specific validator. There were no tests for OriginValidator before.

  • swift build and swift test on Linux (Swift 6.1.2, swift:6.1.2-noble): 500 tests passed.
  • With only the source change reverted, the new accept test fails on bare localhost, 127.0.0.1, [::1] and http://localhost.
  • Not run on macOS; the change is platform-independent string matching.

AI assistance: the change and tests were drafted with an AI coding assistant and reviewed by a person before submitting.

…r.localhost()

OriginValidator.localhost() builds wildcard patterns such as "localhost:*",
and matchesPattern required a ":<digits>" suffix. A request whose Host is
"localhost" (or Origin "http://localhost") was rejected with 421/403, even
though a missing port means the scheme's default port (RFC 9110, section 7.2).
That breaks a server bound to port 80 on its own loopback requests.

The wildcard now also matches the bare host. Port-specific validators
(localhost(port:)) are unchanged, and non-loopback hosts are still rejected.

Adds OriginValidatorTests, covering accepted loopback forms, rejected hosts and
origins, and the port-specific validator.

AI assistance: the change and tests were drafted with an AI coding assistant.
@Perennos
Perennos requested a review from a team as a code owner November 24, 2026 05:50
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.

1 participant