Skip to content

Commit 7f121e8

Browse files
committed
AntRegexRequestMatcher Optimization
Closes gh-11234
1 parent 9059fb3 commit 7f121e8

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@
4040
* @since 3.1
4141
*/
4242
public final class RegexRequestMatcher implements RequestMatcher {
43+
4344
private final static Log logger = LogFactory.getLog(RegexRequestMatcher.class);
4445

46+
private static final int DEFAULT = Pattern.DOTALL;
47+
48+
private static final int CASE_INSENSITIVE = DEFAULT | Pattern.CASE_INSENSITIVE;
49+
4550
private final Pattern pattern;
4651
private final HttpMethod httpMethod;
4752

@@ -64,14 +69,8 @@ public RegexRequestMatcher(String pattern, String httpMethod) {
6469
* {@link Pattern#CASE_INSENSITIVE} flag set.
6570
*/
6671
public RegexRequestMatcher(String pattern, String httpMethod, boolean caseInsensitive) {
67-
if (caseInsensitive) {
68-
this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
69-
}
70-
else {
71-
this.pattern = Pattern.compile(pattern);
72-
}
73-
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod
74-
.valueOf(httpMethod) : null;
72+
this.pattern = Pattern.compile(pattern, caseInsensitive ? CASE_INSENSITIVE : DEFAULT);
73+
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null;
7574
}
7675

7776
/**

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
@@ -108,6 +108,22 @@ public void matchesWithInvalidMethod() {
108108
assertThat(matcher.matches(request)).isFalse();
109109
}
110110

111+
@Test
112+
public void matchesWithCarriageReturn() {
113+
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
114+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0a");
115+
request.setServletPath("/blah\n");
116+
assertThat(matcher.matches(request)).isTrue();
117+
}
118+
119+
@Test
120+
public void matchesWithLineFeed() {
121+
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
122+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0d");
123+
request.setServletPath("/blah\r");
124+
assertThat(matcher.matches(request)).isTrue();
125+
}
126+
111127
@Test
112128
public void toStringThenFormatted() {
113129
RegexRequestMatcher matcher = new RegexRequestMatcher("/blah", "GET");

0 commit comments

Comments
 (0)