Skip to content

Commit 6b19642

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-30298
1 parent 19bb4e9 commit 6b19642

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,18 +482,24 @@ void matchesWithPatternAccessThreshold() {
482482

483483
@Test
484484
void matchesWithPatternLengthThreshold() {
485-
String pattern = "(0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
486-
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
487-
"01234567890123456789012345678901234567890123456789|abc)";
488-
assertThat(pattern).hasSize(256);
489-
Expression expr = parser.parseExpression("'abc' matches '" + pattern + "'");
485+
String pattern = String.format("(%s|X)", repeat("1234", 255));
486+
assertThat(pattern).hasSize(1024);
487+
Expression expr = parser.parseExpression("'X' matches '" + pattern + "'");
490488
assertThat(expr.getValue(context, Boolean.class)).isTrue();
491489

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

495+
private String repeat(String str, int count) {
496+
String result = "";
497+
for (int i = 0; i < count; i++) {
498+
result += str;
499+
}
500+
return result;
501+
}
502+
497503
}
498504

499505
@Nested

0 commit comments

Comments
 (0)