Skip to content

Commit

Permalink
Increase max regex length in SpEL expressions
Browse files Browse the repository at this point in the history
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-30265
  • Loading branch information
sbrannen committed Apr 6, 2023
1 parent 2bac371 commit 310344c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,13 @@ void matchesWithPatternAccessThreshold() {

@Test
void matchesWithPatternLengthThreshold() {
String pattern = "(0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
"01234567890123456789012345678901234567890123456789|abc)";
assertThat(pattern).hasSize(256);
Expression expr = parser.parseExpression("'abc' matches '" + pattern + "'");
String pattern = "(%s|X)".formatted("1234".repeat(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);
}

Expand Down

0 comments on commit 310344c

Please sign in to comment.