Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.web.servlet.util.matcher;

import java.util.Objects;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.http.HttpMethod;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
Expand All @@ -32,6 +29,8 @@
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;

import jakarta.servlet.http.HttpServletRequest;

/**
* A {@link RequestMatcher} that uses {@link PathPattern}s to match against each
* {@link HttpServletRequest}. The provided path should be relative to the context path
Expand Down Expand Up @@ -336,7 +335,8 @@ private static final class HttpMethodRequestMatcher implements RequestMatcher {

@Override
public boolean matches(HttpServletRequest request) {
return this.method.name().equals(request.getMethod());
String requestMethod = request.getMethod();
return requestMethod != null && this.method.name().equals(requestMethod);
Comment on lines +338 to +339
Copy link
Contributor

@ngocnhan-tran1996 ngocnhan-tran1996 Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don’t need to check for null because this.method.name() will return a String type and String#equals(null) will return false, so the test passes without any changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this fixes the NPE case - ig that is our expectation of getting false for invalid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.method.name().equals(request.getMethod()) will return false if request.getMethod() == null and won’t throw an NPE unless request is null or method is null. The test passes without any changes.

Image

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.web.servlet.util.matcher;

import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.jupiter.api.Test;

import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.web.servlet.MockServletContext;
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
import org.springframework.security.web.util.matcher.RequestMatcher;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import org.springframework.web.util.ServletRequestPathUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;

/**
* Tests for {@link PathPatternRequestMatcher}
Expand Down Expand Up @@ -146,6 +144,14 @@ void matcherWhenBasePathIsRootThenNoDoubleSlash() {
assertThat(matcher.matches(mock)).isTrue();
}

@Test
void matcherWhenRequestMethodIsNullThenNoNullPointerException() {
RequestMatcher matcher = pathPattern(HttpMethod.GET, "/");
MockHttpServletRequest mock = new MockHttpServletRequest(null, "/");
ServletRequestPathUtils.parseAndCache(mock);
assertThat(matcher.matches(mock)).isFalse();
}

MockHttpServletRequest request(String uri) {
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
ServletRequestPathUtils.parseAndCache(request);
Expand Down
Loading