Skip to content

Commit 84200f3

Browse files
rs017991sbrannen
authored andcommitted
Treat null path as non-matching pattern in AntPathMatcher
Prior to this commit, a null path supplied to the isPattern(), match(), and matchStart() methods in AntPathMatcher resulted in a NullPointerException. This commit addresses this by treating a `null` path as a non-matching pattern. Closes gh-23297
1 parent 639a254 commit 84200f3

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

spring-core/src/main/java/org/springframework/util/AntPathMatcher.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ private void deactivatePatternCache() {
169169

170170
@Override
171171
public boolean isPattern(String path) {
172+
if(path == null) {
173+
return false;
174+
}
172175
boolean uriVar = false;
173176
for (int i = 0; i < path.length(); i++) {
174177
char c = path.charAt(i);
@@ -207,7 +210,7 @@ public boolean matchStart(String pattern, String path) {
207210
protected boolean doMatch(String pattern, String path, boolean fullMatch,
208211
@Nullable Map<String, String> uriTemplateVariables) {
209212

210-
if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
213+
if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
211214
return false;
212215
}
213216

spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public void match() {
130130
assertThat(pathMatcher.match("", "")).isTrue();
131131

132132
assertThat(pathMatcher.match("/{bla}.*", "/testing.html")).isTrue();
133+
134+
assertThat(pathMatcher.match("/test", null)).isFalse();
135+
assertThat(pathMatcher.match("/", null)).isFalse();
136+
assertThat(pathMatcher.match("/", null)).isFalse();
137+
assertThat(pathMatcher.match(null, null)).isFalse();
133138
}
134139

135140
// SPR-14247
@@ -688,6 +693,7 @@ public void isPattern() {
688693
assertThat(pathMatcher.isPattern("/test/{name}")).isTrue();
689694
assertThat(pathMatcher.isPattern("/test/name")).isFalse();
690695
assertThat(pathMatcher.isPattern("/test/foo{bar")).isFalse();
696+
assertThat(pathMatcher.isPattern(null)).isFalse();
691697
}
692698

693699
}

spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public void redirect() throws Exception {
4242
redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1"));
4343
}
4444

45+
@Test
46+
public void redirectNonMatching() {
47+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
48+
redirectedUrl("/resource/2").match(getRedirectedUrlStubMvcResult("/resource/1")));
49+
}
50+
51+
@Test
52+
public void redirectNonMatchingBecauseNotRedirect() {
53+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
54+
redirectedUrl("/resource/1").match(getForwardedUrlStubMvcResult("/resource/1")));
55+
}
56+
4557
@Test
4658
public void redirectWithUrlTemplate() throws Exception {
4759
redirectedUrlTemplate("/orders/{orderId}/items/{itemId}", 1, 2).match(getRedirectedUrlStubMvcResult("/orders/1/items/2"));
@@ -58,11 +70,29 @@ public void redirectWithNonMatchingPattern() throws Exception {
5870
redirectedUrlPattern("/resource/").match(getRedirectedUrlStubMvcResult("/resource/1")));
5971
}
6072

73+
@Test
74+
public void redirectWithNonMatchingPatternBecauseNotRedirect() {
75+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
76+
redirectedUrlPattern("/resource/*").match(getForwardedUrlStubMvcResult("/resource/1")));
77+
}
78+
6179
@Test
6280
public void forward() throws Exception {
6381
forwardedUrl("/api/resource/1").match(getForwardedUrlStubMvcResult("/api/resource/1"));
6482
}
6583

84+
@Test
85+
public void forwardNonMatching() {
86+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
87+
forwardedUrlPattern("api/resource/2").match(getForwardedUrlStubMvcResult("api/resource/1")));
88+
}
89+
90+
@Test
91+
public void forwardNonMatchingBecauseNotForward() {
92+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
93+
forwardedUrlPattern("api/resource/1").match(getRedirectedUrlStubMvcResult("api/resource/1")));
94+
}
95+
6696
@Test
6797
public void forwardWithQueryString() throws Exception {
6898
forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value"));
@@ -84,6 +114,12 @@ public void forwardWithNonMatchingPattern() throws Exception {
84114
forwardedUrlPattern("/resource/").match(getForwardedUrlStubMvcResult("/resource/1")));
85115
}
86116

117+
@Test
118+
public void forwardWithNonMatchingPatternBecauseNotForward() {
119+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
120+
forwardedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1")));
121+
}
122+
87123
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
88124
MockHttpServletResponse response = new MockHttpServletResponse();
89125
response.sendRedirect(redirectUrl);

0 commit comments

Comments
 (0)