Skip to content

Treat null path as non-matching pattern in AntPathMatcher #23297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 @@ -169,6 +169,9 @@ private void deactivatePatternCache() {

@Override
public boolean isPattern(String path) {
if(path == null) {
return false;
}
boolean uriVar = false;
for (int i = 0; i < path.length(); i++) {
char c = path.charAt(i);
Expand Down Expand Up @@ -207,7 +210,7 @@ public boolean matchStart(String pattern, String path) {
protected boolean doMatch(String pattern, String path, boolean fullMatch,
@Nullable Map<String, String> uriTemplateVariables) {

if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public void match() {
assertThat(pathMatcher.match("", "")).isTrue();

assertThat(pathMatcher.match("/{bla}.*", "/testing.html")).isTrue();

assertThat(pathMatcher.match("/test", null)).isFalse();
assertThat(pathMatcher.match("/", null)).isFalse();
assertThat(pathMatcher.match("/", null)).isFalse();
assertThat(pathMatcher.match(null, null)).isFalse();
}

// SPR-14247
Expand Down Expand Up @@ -688,6 +693,7 @@ public void isPattern() {
assertThat(pathMatcher.isPattern("/test/{name}")).isTrue();
assertThat(pathMatcher.isPattern("/test/name")).isFalse();
assertThat(pathMatcher.isPattern("/test/foo{bar")).isFalse();
assertThat(pathMatcher.isPattern(null)).isFalse();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public void redirect() throws Exception {
redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1"));
}

@Test
public void redirectNonMatching() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
redirectedUrl("/resource/2").match(getRedirectedUrlStubMvcResult("/resource/1")));
}

@Test
public void redirectNonMatchingBecauseNotRedirect() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
redirectedUrl("/resource/1").match(getForwardedUrlStubMvcResult("/resource/1")));
}

@Test
public void redirectWithUrlTemplate() throws Exception {
redirectedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(getRedirectedUrlStubMvcResult("/orders/1/items/2"));
Expand All @@ -58,11 +70,29 @@ public void redirectWithNonMatchingPattern() throws Exception {
redirectedUrlPattern("/resource/").match(getRedirectedUrlStubMvcResult("/resource/1")));
}

@Test
public void redirectWithNonMatchingPatternBecauseNotRedirect() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
redirectedUrlPattern("/resource/*").match(getForwardedUrlStubMvcResult("/resource/1")));
}

@Test
public void forward() throws Exception {
forwardedUrl("/api/resource/1").match(getForwardedUrlStubMvcResult("/api/resource/1"));
}

@Test
public void forwardNonMatching() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
forwardedUrlPattern("api/resource/2").match(getForwardedUrlStubMvcResult("api/resource/1")));
}

@Test
public void forwardNonMatchingBecauseNotForward() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
forwardedUrlPattern("api/resource/1").match(getRedirectedUrlStubMvcResult("api/resource/1")));
}

@Test
public void forwardWithQueryString() throws Exception {
forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value"));
Expand All @@ -84,6 +114,12 @@ public void forwardWithNonMatchingPattern() throws Exception {
forwardedUrlPattern("/resource/").match(getForwardedUrlStubMvcResult("/resource/1")));
}

@Test
public void forwardWithNonMatchingPatternBecauseNotForward() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
forwardedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1")));
}

private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.sendRedirect(redirectUrl);
Expand Down