@@ -43,6 +43,12 @@ public class OperatorMatches extends Operator {
43
43
44
44
private static final int PATTERN_ACCESS_THRESHOLD = 1000000 ;
45
45
46
+ /**
47
+ * Maximum number of characters permitted in a regular expression.
48
+ * @since 5.3.26
49
+ */
50
+ private static final int MAX_REGEX_LENGTH = 256 ;
51
+
46
52
private final ConcurrentMap <String , Pattern > patternCache ;
47
53
48
54
@@ -78,25 +84,27 @@ public OperatorMatches(ConcurrentMap<String, Pattern> patternCache, int startPos
78
84
public BooleanTypedValue getValueInternal (ExpressionState state ) throws EvaluationException {
79
85
SpelNodeImpl leftOp = getLeftOperand ();
80
86
SpelNodeImpl rightOp = getRightOperand ();
81
- String left = leftOp .getValue (state , String .class );
82
- Object right = getRightOperand ().getValue (state );
83
87
84
- if (left == null ) {
88
+ String input = leftOp .getValue (state , String .class );
89
+ if (input == null ) {
85
90
throw new SpelEvaluationException (leftOp .getStartPosition (),
86
91
SpelMessage .INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR , (Object ) null );
87
92
}
88
- if (!(right instanceof String rightString )) {
93
+
94
+ Object right = rightOp .getValue (state );
95
+ if (!(right instanceof String regex )) {
89
96
throw new SpelEvaluationException (rightOp .getStartPosition (),
90
97
SpelMessage .INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR , right );
91
98
}
92
99
93
100
try {
94
- Pattern pattern = this .patternCache .get (rightString );
101
+ Pattern pattern = this .patternCache .get (regex );
95
102
if (pattern == null ) {
96
- pattern = Pattern .compile (rightString );
97
- this .patternCache .putIfAbsent (rightString , pattern );
103
+ checkRegexLength (regex );
104
+ pattern = Pattern .compile (regex );
105
+ this .patternCache .putIfAbsent (regex , pattern );
98
106
}
99
- Matcher matcher = pattern .matcher (new MatcherInput (left , new AccessCount ()));
107
+ Matcher matcher = pattern .matcher (new MatcherInput (input , new AccessCount ()));
100
108
return BooleanTypedValue .forValue (matcher .matches ());
101
109
}
102
110
catch (PatternSyntaxException ex ) {
@@ -109,6 +117,13 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati
109
117
}
110
118
}
111
119
120
+ private void checkRegexLength (String regex ) {
121
+ if (regex .length () > MAX_REGEX_LENGTH ) {
122
+ throw new SpelEvaluationException (getStartPosition (),
123
+ SpelMessage .MAX_REGEX_LENGTH_EXCEEDED , MAX_REGEX_LENGTH );
124
+ }
125
+ }
126
+
112
127
113
128
private static class AccessCount {
114
129
0 commit comments