Skip to content

Commit

Permalink
Guard for null pointer if no host header.
Browse files Browse the repository at this point in the history
Fixes gh-3699
  • Loading branch information
spencergibb committed Feb 21, 2025
1 parent 159ce34 commit 4564a13
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ private static class HostPatternPredicate implements RequestPredicate, ChangePat
@Override
public boolean test(ServerRequest request) {
String host = request.headers().firstHeader(HttpHeaders.HOST);
if (host == null) {
host = "";
}
PathContainer pathContainer = PathContainer.parsePath(host, PathContainer.Options.MESSAGE_ROUTE);
PathPattern.PathMatchInfo info = this.pattern.matchAndExtract(pathContainer);
traceMatch("Pattern", this.pattern.getPatternString(), host, info != null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2013-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.server.mvc.predicate;

import java.util.Collections;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.servlet.function.ServerRequest;

public class GatewayRequestPredicatesTests {

@Test
void nullHostPassedToHostPredicate() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
ServerRequest serverRequest = ServerRequest.create(servletRequest, Collections.emptyList());
boolean result = GatewayRequestPredicates.host("*.myhost.org").test(serverRequest);
Assertions.assertThat(result).isFalse();
}

}

0 comments on commit 4564a13

Please sign in to comment.