Skip to content

Commit 7fc1c32

Browse files
anzal1Kehrlann
andauthored
Require a numeric port in wildcard Host/Origin matching (modelcontextprotocol#1193)
Fixes modelcontextprotocol#1192 Signed-off-by: Daniel Garnier-Moiroux <[email protected]> Co-authored-by: Daniel Garnier-Moiroux <[email protected]>
1 parent 7b9b000 commit 7fc1c32

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/DefaultServerTransportSecurityValidator.java‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected void validateOrigin(String origin) throws ServerTransportSecurityExcep
8989
else if (allowed.endsWith(":*")) {
9090
// Wildcard port pattern: "http://example.com:*"
9191
String baseOrigin = allowed.substring(0, allowed.length() - 2);
92-
if (origin.equals(baseOrigin) || origin.startsWith(baseOrigin + ":")) {
92+
if (origin.equals(baseOrigin) || matchesWithPort(origin, baseOrigin)) {
9393
return;
9494
}
9595
}
@@ -121,7 +121,7 @@ private void validateHost(String host) throws ServerTransportSecurityException {
121121
else if (allowed.endsWith(":*")) {
122122
// Wildcard port pattern: "example.com:*"
123123
String baseHost = allowed.substring(0, allowed.length() - 2);
124-
if (host.equals(baseHost) || host.startsWith(baseHost + ":")) {
124+
if (host.equals(baseHost) || matchesWithPort(host, baseHost)) {
125125
return;
126126
}
127127
}
@@ -130,6 +130,28 @@ else if (allowed.endsWith(":*")) {
130130
throw new ServerTransportSecurityException(421, "Invalid Host header");
131131
}
132132

133+
/**
134+
* Checks that {@code actual} is {@code allowed} followed by {@code ":"} and a valid
135+
* port: 1 to 5 ASCII digits, in the range 1-65535.
136+
*/
137+
private static boolean matchesWithPort(String actual, String allowed) {
138+
if (!actual.startsWith(allowed + ":")) {
139+
return false;
140+
}
141+
String port = actual.substring(allowed.length() + 1);
142+
if (port.isEmpty() || port.length() > 5) {
143+
return false;
144+
}
145+
for (int i = 0; i < port.length(); i++) {
146+
char c = port.charAt(i);
147+
if (c < '0' || c > '9') {
148+
return false;
149+
}
150+
}
151+
int portNumber = Integer.parseInt(port);
152+
return portNumber >= 1 && portNumber <= 65535;
153+
}
154+
133155
/**
134156
* Creates a new builder for constructing a DefaultServerTransportSecurityValidator.
135157
* @return A new builder instance

‎mcp-core/src/test/java/io/modelcontextprotocol/server/transport/DefaultServerTransportSecurityValidatorTests.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import org.junit.jupiter.api.Nested;
1212
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.params.ParameterizedTest;
14+
import org.junit.jupiter.params.provider.ValueSource;
1315

1416
import static org.assertj.core.api.Assertions.assertThatCode;
1517
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -118,6 +120,25 @@ void differentPortWithWildcard() {
118120
assertThatCode(() -> wildcardValidator.validate(accessor)).doesNotThrowAnyException();
119121
}
120122

123+
@ParameterizedTest
124+
@ValueSource(strings = { "http://localhost:1", "http://localhost:65535" })
125+
void boundaryPortsWithWildcard(String origin) {
126+
var accessor = originAccessor(origin);
127+
128+
assertThatCode(() -> wildcardValidator.validate(accessor)).doesNotThrowAnyException();
129+
}
130+
131+
@ParameterizedTest
132+
@ValueSource(strings = { "http://localhost:", "http://localhost:evil.com", "http://localhost:3000.evil.com",
133+
"http://localhost:@evil.com", "http://localhost:80a", "http://localhost: 80",
134+
"http://localhost:3000/path", "http://localhost:0", "http://localhost:65536",
135+
"http://localhost:123456", "http://localhost:-1", "http://localhost:+80" })
136+
void invalidPortWithWildcard(String origin) {
137+
var accessor = originAccessor(origin);
138+
139+
assertThatThrownBy(() -> wildcardValidator.validate(accessor)).isEqualTo(INVALID_ORIGIN);
140+
}
141+
121142
@Test
122143
void differentHostWithWildcard() {
123144
var accessor = originAccessor("http://example.com:3000");
@@ -273,6 +294,33 @@ void noPort() {
273294
assertThatCode(() -> wildcardHostValidator.validate(accessor)).doesNotThrowAnyException();
274295
}
275296

297+
@ParameterizedTest
298+
@ValueSource(strings = { "localhost:1", "localhost:65535" })
299+
void boundaryPorts(String host) {
300+
var accessor = hostAccessor(host);
301+
302+
assertThatCode(() -> wildcardHostValidator.validate(accessor)).doesNotThrowAnyException();
303+
}
304+
305+
@ParameterizedTest
306+
@ValueSource(strings = { "localhost:", "localhost:evil.com", "localhost:3000.evil.com",
307+
"localhost:@evil.com", "localhost:80a", "localhost: 80", "localhost:3000/path", "localhost:0",
308+
"localhost:65536", "localhost:123456", "localhost:-1", "localhost:+80" })
309+
void invalidPort(String host) {
310+
var accessor = hostAccessor(host);
311+
312+
assertThatThrownBy(() -> wildcardHostValidator.validate(accessor)).isEqualTo(INVALID_HOST);
313+
}
314+
315+
@ParameterizedTest
316+
@ValueSource(strings = { "127.0.0.1:evil.com", "127.0.0.1:80.evil.com", "127.0.0.1:@evil.com" })
317+
void nonNumericPortIpv4(String host) {
318+
var ipValidator = DefaultServerTransportSecurityValidator.builder().allowedHost("127.0.0.1:*").build();
319+
320+
assertThatThrownBy(() -> ipValidator.validate(hostAccessor(host))).isEqualTo(INVALID_HOST);
321+
assertThatCode(() -> ipValidator.validate(hostAccessor("127.0.0.1:8080"))).doesNotThrowAnyException();
322+
}
323+
276324
@Test
277325
void differentHost() {
278326
var accessor = hostAccessor("example.com:3000");

0 commit comments

Comments
 (0)