From 705bf7810406358aa42e091f029e7bfa60cbf3d5 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 4 Apr 2022 18:51:03 +0200 Subject: [PATCH] Consistently handle match with '*' and trailing slash Prior to this commit, the `AntPathMatcher` would inconsistently match pattern with trailing slashes if they contain `"**"` within their pattern. For example `"/en/test/"` would match `"/**/test"`, but it would not match `"/*/test"` (as it should). This commit fixes this behavior for better consistency. Fixes gh-27506 --- .../main/java/org/springframework/util/AntPathMatcher.java | 4 ++++ .../java/org/springframework/util/AntPathMatcherTests.java | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java index 63eea1e9ea..bf4eb349e0 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -275,6 +275,10 @@ public class AntPathMatcher implements PathMatcher { if (!matchStrings(pattDir, pathDirs[pathIdxEnd], uriTemplateVariables)) { return false; } + if (pattIdxEnd == (pattDirs.length - 1) + && pattern.endsWith(this.pathSeparator) != path.endsWith(this.pathSeparator)) { + return false; + } pattIdxEnd--; pathIdxEnd--; } diff --git a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java index 8bebf5920f..b018e99caf 100644 --- a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java +++ b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java @@ -704,4 +704,11 @@ class AntPathMatcherTests { assertThat(pathMatcher.isPattern(null)).isFalse(); } + @Test // gh-27506 + void consistentMatchWithWildcardsAndTrailingSlash() { + assertThat(pathMatcher.match("/*/foo", "/en/foo")).isTrue(); + assertThat(pathMatcher.match("/*/foo", "/en/foo/")).isFalse(); + assertThat(pathMatcher.match("/**/foo", "/en/foo")).isTrue(); + assertThat(pathMatcher.match("/**/foo", "/en/foo/")).isFalse(); + } }