From c74f99a993e3d6691d0b8eebf171b5fb454f751c Mon Sep 17 00:00:00 2001 From: Perennos <333385283+Perennos@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:30:04 -0500 Subject: [PATCH] fix: accept loopback Host and Origin without a port in OriginValidator.localhost() OriginValidator.localhost() builds wildcard patterns such as "localhost:*", and matchesPattern required a ":" 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. --- .../HTTPServer/HTTPRequestValidation.swift | 7 ++- Tests/MCPTests/OriginValidatorTests.swift | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 Tests/MCPTests/OriginValidatorTests.swift diff --git a/Sources/MCP/Base/Transports/HTTPServer/HTTPRequestValidation.swift b/Sources/MCP/Base/Transports/HTTPServer/HTTPRequestValidation.swift index 51c7aaed..6f1e91e2 100644 --- a/Sources/MCP/Base/Transports/HTTPServer/HTTPRequestValidation.swift +++ b/Sources/MCP/Base/Transports/HTTPServer/HTTPRequestValidation.swift @@ -324,8 +324,8 @@ public struct OriginValidator: HTTPRequestValidator { /// Matches a value against a pattern that may contain a port wildcard `:*`. /// /// Examples: - /// - `"localhost:*"` matches `"localhost:8080"`, `"localhost:3000"` - /// - `"http://localhost:*"` matches `"http://localhost:8080"` + /// - `"localhost:*"` matches `"localhost:8080"`, `"localhost:3000"`, and `"localhost"` + /// - `"http://localhost:*"` matches `"http://localhost:8080"` and `"http://localhost"` /// - `"localhost:8080"` matches only `"localhost:8080"` exactly private func matchesPattern(_ value: String, pattern: String) -> Bool { guard pattern.hasSuffix(":*") else { @@ -333,6 +333,9 @@ public struct OriginValidator: HTTPRequestValidator { } let prefix = String(pattern.dropLast(2)) + // A value without a port uses the scheme's default port (RFC 9110, section 7.2), + // which the wildcard covers. + if value == prefix { return true } guard value.hasPrefix(prefix + ":") else { return false } let portPart = value.dropFirst(prefix.count + 1) diff --git a/Tests/MCPTests/OriginValidatorTests.swift b/Tests/MCPTests/OriginValidatorTests.swift new file mode 100644 index 00000000..e0576367 --- /dev/null +++ b/Tests/MCPTests/OriginValidatorTests.swift @@ -0,0 +1,46 @@ +import Foundation +import Testing + +@testable import MCP + +@Suite("OriginValidator Tests") +struct OriginValidatorTests { + private let context = HTTPValidationContext(httpMethod: "POST") + + private func request(host: String, origin: String? = nil) -> HTTPRequest { + var headers = ["Host": host] + if let origin { headers["Origin"] = origin } + return HTTPRequest(method: "POST", headers: headers) + } + + @Test("Localhost validator accepts loopback hosts with and without a port") + func testAcceptsLoopbackHosts() { + let validator = OriginValidator.localhost() + for host in ["localhost:8080", "127.0.0.1:3000", "[::1]:9000", "localhost", "127.0.0.1", "[::1]"] { + #expect(validator.validate(request(host: host), context: context) == nil, "\(host)") + } + #expect( + validator.validate( + request(host: "localhost", origin: "http://localhost"), context: context) == nil) + } + + @Test("Localhost validator rejects other hosts and origins") + func testRejectsOtherHosts() { + let validator = OriginValidator.localhost() + for host in ["evil.example", "evil.example:8080", "localhost.evil.example", "localhost:", "localhost:80x"] { + #expect(validator.validate(request(host: host), context: context)?.statusCode == 421, "\(host)") + } + #expect( + validator.validate( + request(host: "localhost:8080", origin: "http://evil.example"), context: context)? + .statusCode == 403) + } + + @Test("Port-specific localhost validator still requires that port") + func testPortSpecificValidator() { + let validator = OriginValidator.localhost(port: 8080) + #expect(validator.validate(request(host: "localhost:8080"), context: context) == nil) + #expect(validator.validate(request(host: "localhost"), context: context)?.statusCode == 421) + #expect(validator.validate(request(host: "localhost:9090"), context: context)?.statusCode == 421) + } +}