Improve diagnostics in SpEL for matches operator

Supplying a large regular expression to the `matches` operator in a
SpEL expression can result in errors that are not very helpful to the
user.

This commit improves the diagnostics in SpEL for the `matches` operator
by throwing a SpelEvaluationException with a meaningful error message
to better assist the user.

Closes gh-30150
This commit is contained in:
Sam Brannen
2023-03-17 12:56:53 +01:00
parent 4542b53103
commit b9b31afcc9
3 changed files with 43 additions and 10 deletions

View File

@@ -199,6 +199,20 @@ public class EvaluationTests extends AbstractExpressionTests {
evaluateAndCheckError(expression, SpelMessage.FLAWED_PATTERN);
}
@Test
void matchesWithPatternLengthThreshold() {
String pattern = "(0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
"01234567890123456789012345678901234567890123456789|abc)";
assertThat(pattern).hasSize(256);
Expression expr = parser.parseExpression("'abc' matches '" + pattern + "'");
assertThat(expr.getValue(context, Boolean.class)).isTrue();
pattern += "?";
assertThat(pattern).hasSize(257);
evaluateAndCheckError("'abc' matches '" + pattern + "'", Boolean.class, SpelMessage.MAX_REGEX_LENGTH_EXCEEDED);
}
// mixing operators
@Test
public void testMixingOperators01() {