From 6b19642256c33f0ba55c9275b883609933953318 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 6 Apr 2023 17:53:03 +0200 Subject: [PATCH] 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 --- .../expression/spel/ast/OperatorMatches.java | 2 +- .../expression/spel/EvaluationTests.java | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java index 0863716c182d..1775309b153e 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java @@ -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 patternCache; diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java index a4601ff3e8d3..6b246560e0eb 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java @@ -482,18 +482,24 @@ void matchesWithPatternAccessThreshold() { @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