Skip to content

Commit c6461d6

Browse files
committed
AntRegexRequestMatcher Optimization
Closes gh-11234
1 parent 4405cf1 commit c6461d6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

web/src/main/java/org/springframework/security/web/util/matcher/RegexRequestMatcher.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
*/
4444
public final class RegexRequestMatcher implements RequestMatcher {
4545

46-
private static final int DEFAULT = 0;
46+
private static final int DEFAULT = Pattern.DOTALL;
47+
48+
private static final int CASE_INSENSITIVE = DEFAULT | Pattern.CASE_INSENSITIVE;
4749

4850
private static final Log logger = LogFactory.getLog(RegexRequestMatcher.class);
4951

@@ -68,7 +70,7 @@ public RegexRequestMatcher(String pattern, String httpMethod) {
6870
* {@link Pattern#CASE_INSENSITIVE} flag set.
6971
*/
7072
public RegexRequestMatcher(String pattern, String httpMethod, boolean caseInsensitive) {
71-
this.pattern = Pattern.compile(pattern, caseInsensitive ? Pattern.CASE_INSENSITIVE : DEFAULT);
73+
this.pattern = Pattern.compile(pattern, caseInsensitive ? CASE_INSENSITIVE : DEFAULT);
7274
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null;
7375
}
7476

web/src/test/java/org/springframework/security/web/util/matcher/RegexRequestMatcherTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ public void matchesWithInvalidMethod() {
101101
assertThat(matcher.matches(request)).isFalse();
102102
}
103103

104+
@Test
105+
public void matchesWithCarriageReturn() {
106+
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
107+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0a");
108+
request.setServletPath("/blah\n");
109+
assertThat(matcher.matches(request)).isTrue();
110+
}
111+
112+
@Test
113+
public void matchesWithLineFeed() {
114+
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
115+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0d");
116+
request.setServletPath("/blah\r");
117+
assertThat(matcher.matches(request)).isTrue();
118+
}
119+
104120
@Test
105121
public void toStringThenFormatted() {
106122
RegexRequestMatcher matcher = new RegexRequestMatcher("/blah", "GET");

0 commit comments

Comments
 (0)