Increase max regex length in SpEL expressions

This commit increases the max regex length in SpEL expressions from 256
to 1024 in order to support use cases where a regex may be rather long
without necessarily increasing the complexity of the regex.

Closes gh-30298
This commit is contained in:
Sam Brannen
2023-04-06 17:53:03 +02:00
parent 19bb4e96f2
commit 6b19642256
2 changed files with 13 additions and 7 deletions

View File

@@ -47,7 +47,7 @@ public class OperatorMatches extends Operator {
* Maximum number of characters permitted in a regular expression.
* @since 5.2.23
*/
private static final int MAX_REGEX_LENGTH = 256;
private static final int MAX_REGEX_LENGTH = 1024;
private final ConcurrentMap<String, Pattern> patternCache;

View File

@@ -482,18 +482,24 @@ class EvaluationTests extends AbstractExpressionTests {
@Test
void matchesWithPatternLengthThreshold() {
String pattern = "(0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
"01234567890123456789012345678901234567890123456789|abc)";
assertThat(pattern).hasSize(256);
Expression expr = parser.parseExpression("'abc' matches '" + pattern + "'");
String pattern = String.format("(%s|X)", repeat("1234", 255));
assertThat(pattern).hasSize(1024);
Expression expr = parser.parseExpression("'X' matches '" + pattern + "'");
assertThat(expr.getValue(context, Boolean.class)).isTrue();
pattern += "?";
assertThat(pattern).hasSize(257);
assertThat(pattern).hasSize(1025);
evaluateAndCheckError("'abc' matches '" + pattern + "'", Boolean.class, SpelMessage.MAX_REGEX_LENGTH_EXCEEDED);
}
private String repeat(String str, int count) {
String result = "";
for (int i = 0; i < count; i++) {
result += str;
}
return result;
}
}
@Nested