Skip to content

Commit 3c92ddc

Browse files
committed
Fix path matching for paths containing spaces
Prior to this commit, the latest optimizations introduced in SPR-13913 would prevent matching when patterns contained spaces. Indeed, the optimized path would not fully tokenize the paths nor trim the tokens, as the "longer" code path does. This commit disables this optimized path when the `trimTokens` option is set to `true`. Also, the `trimTokens` setting is now set to `false` by default. Issue: SPR-14247
1 parent 3f85efe commit 3c92ddc

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class AntPathMatcher implements PathMatcher {
8383

8484
private boolean caseSensitive = true;
8585

86-
private boolean trimTokens = true;
86+
private boolean trimTokens = false;
8787

8888
private volatile Boolean cachePatterns;
8989

@@ -314,19 +314,21 @@ else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) {
314314
}
315315

316316
private boolean isPotentialMatch(String path, String[] pattDirs) {
317-
char[] pathChars = path.toCharArray();
318-
int pos = 0;
319-
for (String pattDir : pattDirs) {
320-
int skipped = skipSeparator(path, pos, this.pathSeparator);
321-
pos += skipped;
322-
skipped = skipSegment(pathChars, pos, pattDir);
323-
if (skipped < pattDir.length()) {
324-
if (skipped > 0) {
325-
return true;
317+
if (!this.trimTokens) {
318+
char[] pathChars = path.toCharArray();
319+
int pos = 0;
320+
for (String pattDir : pattDirs) {
321+
int skipped = skipSeparator(path, pos, this.pathSeparator);
322+
pos += skipped;
323+
skipped = skipSegment(pathChars, pos, pattDir);
324+
if (skipped < pattDir.length()) {
325+
if (skipped > 0) {
326+
return true;
327+
}
328+
return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0));
326329
}
327-
return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0));
330+
pos += skipped;
328331
}
329-
pos += skipped;
330332
}
331333
return true;
332334
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ public void match() {
136136
assertTrue(pathMatcher.match("/{bla}.*", "/testing.html"));
137137
}
138138

139+
// SPR-14247
140+
@Test
141+
public void matchWithTrimTokensEnabled() throws Exception {
142+
pathMatcher.setTrimTokens(true);
143+
144+
assertTrue(pathMatcher.match("/foo/bar", "/foo /bar"));
145+
}
146+
139147
@Test
140148
public void withMatchStart() {
141149
// test exact matching

0 commit comments

Comments
 (0)