Skip to content

Commit 528a11f

Browse files
authored
Merge pull request #335 from Perennos/fix/localhost-validator-default-port
fix: accept loopback Host and Origin without a port in OriginValidator.localhost()
2 parents 510c100 + 95b9b82 commit 528a11f

2 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎Sources/MCP/Base/Transports/HTTPServer/HTTPRequestValidation.swift‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ public struct OriginValidator: HTTPRequestValidator {
302302
}
303303

304304
/// Protection for localhost-bound servers.
305-
/// Allows requests from `localhost`, `127.0.0.1`, and `[::1]` with the specified port.
305+
/// Allows requests from `localhost`, `127.0.0.1`, and `[::1]` on the given port.
306+
/// Pass `nil` to allow any port, or none.
306307
public static func localhost(port: Int? = nil) -> OriginValidator {
307308
let portPattern = port.map { String($0) } ?? "*"
308309
return OriginValidator(
@@ -365,15 +366,18 @@ public struct OriginValidator: HTTPRequestValidator {
365366
/// Matches a value against a pattern that may contain a port wildcard `:*`.
366367
///
367368
/// Examples:
368-
/// - `"localhost:*"` matches `"localhost:8080"`, `"localhost:3000"`
369-
/// - `"http://localhost:*"` matches `"http://localhost:8080"`
369+
/// - `"localhost:*"` matches `"localhost:8080"`, `"localhost:3000"`, and `"localhost"`
370+
/// - `"http://localhost:*"` matches `"http://localhost:8080"` and `"http://localhost"`
370371
/// - `"localhost:8080"` matches only `"localhost:8080"` exactly
371372
private func matchesPattern(_ value: String, pattern: String) -> Bool {
372373
guard pattern.hasSuffix(":*") else {
373374
return value == pattern
374375
}
375376

376377
let prefix = String(pattern.dropLast(2))
378+
// A value without a port uses the scheme's default port (RFC 9110, section 7.2),
379+
// which the wildcard covers.
380+
if value == prefix { return true }
377381
guard value.hasPrefix(prefix + ":") else { return false }
378382

379383
let portPart = value.dropFirst(prefix.count + 1)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Foundation
2+
import Testing
3+
4+
@testable import MCP
5+
6+
@Suite("OriginValidator Tests")
7+
struct OriginValidatorTests {
8+
private let context = HTTPValidationContext(httpMethod: "POST")
9+
10+
private func request(host: String, origin: String? = nil) -> HTTPRequest {
11+
var headers = ["Host": host]
12+
if let origin { headers["Origin"] = origin }
13+
return HTTPRequest(method: "POST", headers: headers)
14+
}
15+
16+
@Test("Localhost validator accepts loopback hosts with and without a port")
17+
func testAcceptsLoopbackHosts() {
18+
let validator = OriginValidator.localhost()
19+
for host in ["localhost:8080", "127.0.0.1:3000", "[::1]:9000", "localhost", "127.0.0.1", "[::1]"] {
20+
#expect(validator.validate(request(host: host), context: context) == nil, "\(host)")
21+
}
22+
for origin in [
23+
"http://localhost:8080", "http://127.0.0.1:3000", "http://[::1]:9000",
24+
"http://localhost", "http://127.0.0.1", "http://[::1]",
25+
] {
26+
#expect(
27+
validator.validate(request(host: "localhost:8080", origin: origin), context: context) == nil,
28+
"\(origin)"
29+
)
30+
}
31+
}
32+
33+
@Test("Localhost validator rejects other hosts and origins")
34+
func testRejectsOtherHosts() {
35+
let validator = OriginValidator.localhost()
36+
for host in ["evil.example", "evil.example:8080", "localhost.evil.example", "localhost:", "localhost:80x"] {
37+
#expect(validator.validate(request(host: host), context: context)?.statusCode == 421, "\(host)")
38+
}
39+
for origin in [
40+
"http://evil.example", "http://localhost.evil.example",
41+
"http://localhost:", "http://localhost:80x", "https://localhost", "null",
42+
] {
43+
#expect(
44+
validator.validate(request(host: "localhost:8080", origin: origin), context: context)?
45+
.statusCode == 403,
46+
"\(origin)"
47+
)
48+
}
49+
}
50+
51+
@Test("Port-specific localhost validator still requires that port")
52+
func testPortSpecificValidator() {
53+
let validator = OriginValidator.localhost(port: 8080)
54+
#expect(validator.validate(request(host: "localhost:8080"), context: context) == nil)
55+
#expect(validator.validate(request(host: "localhost"), context: context)?.statusCode == 421)
56+
#expect(validator.validate(request(host: "localhost:9090"), context: context)?.statusCode == 421)
57+
#expect(
58+
validator.validate(request(host: "localhost:8080", origin: "http://localhost:8080"), context: context)
59+
== nil)
60+
#expect(
61+
validator.validate(request(host: "localhost:8080", origin: "http://localhost"), context: context)?
62+
.statusCode == 403)
63+
#expect(
64+
validator.validate(request(host: "localhost:8080", origin: "http://localhost:9090"), context: context)?
65+
.statusCode == 403)
66+
}
67+
}

0 commit comments

Comments
 (0)