Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,18 @@ 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 {
return value == pattern
}

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)
Expand Down
46 changes: 46 additions & 0 deletions Tests/MCPTests/OriginValidatorTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}