Skip to content

Commit 310344c

Browse files
committed
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-30265
1 parent 2bac371 commit 310344c

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class OperatorMatches extends Operator {
4747
* Maximum number of characters permitted in a regular expression.
4848
* @since 5.2.23
4949
*/
50-
private static final int MAX_REGEX_LENGTH = 256;
50+
private static final int MAX_REGEX_LENGTH = 1024;
5151

5252
private final ConcurrentMap<String, Pattern> patternCache;
5353

spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,13 @@ void matchesWithPatternAccessThreshold() {
484484

485485
@Test
486486
void matchesWithPatternLengthThreshold() {
487-
String pattern = "(0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
488-
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
489-
"01234567890123456789012345678901234567890123456789|abc)";
490-
assertThat(pattern).hasSize(256);
491-
Expression expr = parser.parseExpression("'abc' matches '" + pattern + "'");
487+
String pattern = "(%s|X)".formatted("1234".repeat(255));
488+
assertThat(pattern).hasSize(1024);
489+
Expression expr = parser.parseExpression("'X' matches '" + pattern + "'");
492490
assertThat(expr.getValue(context, Boolean.class)).isTrue();
493491

494492
pattern += "?";
495-
assertThat(pattern).hasSize(257);
493+
assertThat(pattern).hasSize(1025);
496494
evaluateAndCheckError("'abc' matches '" + pattern + "'", Boolean.class, SpelMessage.MAX_REGEX_LENGTH_EXCEEDED);
497495
}
498496

0 commit comments

Comments
 (0)