Skip to content

Commit 4564a13

Browse files
committed
Guard for null pointer if no host header.
Fixes gh-3699
1 parent 159ce34 commit 4564a13

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/predicate/GatewayRequestPredicates.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ private static class HostPatternPredicate implements RequestPredicate, ChangePat
374374
@Override
375375
public boolean test(ServerRequest request) {
376376
String host = request.headers().firstHeader(HttpHeaders.HOST);
377+
if (host == null) {
378+
host = "";
379+
}
377380
PathContainer pathContainer = PathContainer.parsePath(host, PathContainer.Options.MESSAGE_ROUTE);
378381
PathPattern.PathMatchInfo info = this.pattern.matchAndExtract(pathContainer);
379382
traceMatch("Pattern", this.pattern.getPatternString(), host, info != null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.gateway.server.mvc.predicate;
18+
19+
import java.util.Collections;
20+
21+
import org.assertj.core.api.Assertions;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.mock.web.MockHttpServletRequest;
25+
import org.springframework.web.servlet.function.ServerRequest;
26+
27+
public class GatewayRequestPredicatesTests {
28+
29+
@Test
30+
void nullHostPassedToHostPredicate() {
31+
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
32+
ServerRequest serverRequest = ServerRequest.create(servletRequest, Collections.emptyList());
33+
boolean result = GatewayRequestPredicates.host("*.myhost.org").test(serverRequest);
34+
Assertions.assertThat(result).isFalse();
35+
}
36+
37+
}

0 commit comments

Comments
 (0)