AntRegexRequestMatcher Optimization

Closes gh-11234
This commit is contained in:
Rob Winch 2022-05-12 20:14:03 -05:00
parent 0df5ece758
commit 472c25b5e8
2 changed files with 20 additions and 2 deletions

View File

@ -42,7 +42,9 @@ import org.springframework.util.StringUtils;
*/ */
public final class RegexRequestMatcher implements RequestMatcher { public final class RegexRequestMatcher implements RequestMatcher {
private static final int DEFAULT = 0; private static final int DEFAULT = Pattern.DOTALL;
private static final int CASE_INSENSITIVE = DEFAULT | Pattern.CASE_INSENSITIVE;
private static final Log logger = LogFactory.getLog(RegexRequestMatcher.class); private static final Log logger = LogFactory.getLog(RegexRequestMatcher.class);
@ -67,7 +69,7 @@ public final class RegexRequestMatcher implements RequestMatcher {
* {@link Pattern#CASE_INSENSITIVE} flag set. * {@link Pattern#CASE_INSENSITIVE} flag set.
*/ */
public RegexRequestMatcher(String pattern, String httpMethod, boolean caseInsensitive) { public RegexRequestMatcher(String pattern, String httpMethod, boolean caseInsensitive) {
this.pattern = Pattern.compile(pattern, caseInsensitive ? Pattern.CASE_INSENSITIVE : DEFAULT); this.pattern = Pattern.compile(pattern, caseInsensitive ? CASE_INSENSITIVE : DEFAULT);
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null; this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null;
} }

View File

@ -100,6 +100,22 @@ public class RegexRequestMatcherTests {
assertThat(matcher.matches(request)).isFalse(); assertThat(matcher.matches(request)).isFalse();
} }
@Test
public void matchesWithCarriageReturn() {
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0a");
request.setServletPath("/blah\n");
assertThat(matcher.matches(request)).isTrue();
}
@Test
public void matchesWithLineFeed() {
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0d");
request.setServletPath("/blah\r");
assertThat(matcher.matches(request)).isTrue();
}
@Test @Test
public void toStringThenFormatted() { public void toStringThenFormatted() {
RegexRequestMatcher matcher = new RegexRequestMatcher("/blah", "GET"); RegexRequestMatcher matcher = new RegexRequestMatcher("/blah", "GET");