Skip to content

Commit c8bd4d4

Browse files
Fixed NPE for unknown request in FilterChainProxy
Closes #18157 Signed-off-by: Soumik Sarker <ronodhirsoumik@gmail.com>
1 parent b130e72 commit c8bd4d4

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

web/src/main/java/org/springframework/security/web/servlet/util/matcher/PathPatternRequestMatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.security.web.servlet.util.matcher;
1817

1918
import java.util.Objects;
2019

21-
import jakarta.servlet.http.HttpServletRequest;
22-
2320
import org.springframework.http.HttpMethod;
2421
import org.springframework.http.server.PathContainer;
2522
import org.springframework.http.server.RequestPath;
@@ -32,6 +29,8 @@
3229
import org.springframework.web.util.pattern.PathPattern;
3330
import org.springframework.web.util.pattern.PathPatternParser;
3431

32+
import jakarta.servlet.http.HttpServletRequest;
33+
3534
/**
3635
* A {@link RequestMatcher} that uses {@link PathPattern}s to match against each
3736
* {@link HttpServletRequest}. The provided path should be relative to the context path
@@ -336,7 +335,8 @@ private static final class HttpMethodRequestMatcher implements RequestMatcher {
336335

337336
@Override
338337
public boolean matches(HttpServletRequest request) {
339-
return this.method.name().equals(request.getMethod());
338+
String requestMethod = request.getMethod();
339+
return requestMethod != null && this.method.name().equals(requestMethod);
340340
}
341341

342342
@Override

web/src/test/java/org/springframework/security/web/servlet/util/matcher/PathPatternRequestMatcherTests.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.security.web.servlet.util.matcher;
1817

19-
import jakarta.servlet.Servlet;
20-
import jakarta.servlet.ServletContext;
21-
import jakarta.servlet.ServletRegistration;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2220
import org.junit.jupiter.api.Test;
23-
2421
import org.springframework.http.HttpMethod;
2522
import org.springframework.mock.web.MockHttpServletRequest;
2623
import org.springframework.security.web.servlet.MockServletContext;
24+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
2725
import org.springframework.security.web.util.matcher.RequestMatcher;
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2827
import org.springframework.web.util.ServletRequestPathUtils;
2928

30-
import static org.assertj.core.api.Assertions.assertThat;
31-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
32-
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
33-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import jakarta.servlet.Servlet;
30+
import jakarta.servlet.ServletContext;
31+
import jakarta.servlet.ServletRegistration;
3432

3533
/**
3634
* Tests for {@link PathPatternRequestMatcher}
@@ -146,6 +144,14 @@ void matcherWhenBasePathIsRootThenNoDoubleSlash() {
146144
assertThat(matcher.matches(mock)).isTrue();
147145
}
148146

147+
@Test
148+
void matcherWhenRequestMethodIsNullThenNoNullPointerException() {
149+
RequestMatcher matcher = pathPattern(HttpMethod.GET, "/");
150+
MockHttpServletRequest mock = new MockHttpServletRequest(null, "/");
151+
ServletRequestPathUtils.parseAndCache(mock);
152+
assertThat(matcher.matches(mock)).isFalse();
153+
}
154+
149155
MockHttpServletRequest request(String uri) {
150156
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
151157
ServletRequestPathUtils.parseAndCache(request);

0 commit comments

Comments
 (0)