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) + } +}