1818
1919/**
2020 * Common utilities for regex operations. Provides pattern caching and consistent matching behavior.
21+ *
22+ * <p>Matches against user-supplied text go through {@link #boundedMatcher} to cap backtracking
23+ * work; see {@link LimitedCharSequence}.
2124 */
2225public class RegexCommonUtils {
2326
@@ -27,6 +30,35 @@ public class RegexCommonUtils {
2730 // Pattern to extract ANY named group (valid or invalid) for validation
2831 private static final Pattern ANY_NAMED_GROUP_PATTERN = Pattern .compile ("\\ (\\ ?<([^>]+)>" );
2932
33+ /**
34+ * Default absolute cap on the number of characters the regex engine may read in a single match
35+ * before it is aborted. Mirrors the default of PCRE2's {@code match_limit} as adopted by Splunk
36+ * {@code rex} (100000): high enough never to trip legitimate matches, low enough that a
37+ * catastrophic match aborts in well under a millisecond. Overridable via the {@code
38+ * plugins.ppl.regex.match.limit} setting through {@link #setMatchLimit(long)}.
39+ */
40+ public static final long DEFAULT_MATCH_LIMIT = 100_000L ;
41+
42+ // Node-scoped value, pushed in by the OpenSearch settings layer on register and on dynamic
43+ // change. volatile so updates are visible to matching threads.
44+ private static volatile long matchLimit = DEFAULT_MATCH_LIMIT ;
45+
46+ /**
47+ * Set the absolute per-match character-read cap. Called by the OpenSearch settings layer when
48+ * {@code plugins.ppl.regex.match.limit} is registered or changed. Non-positive values are ignored
49+ * (the previous value is kept) so a misconfiguration cannot disable the guard.
50+ */
51+ public static void setMatchLimit (long limit ) {
52+ if (limit > 0 ) {
53+ matchLimit = limit ;
54+ }
55+ }
56+
57+ /** Current absolute per-match character-read cap. */
58+ public static long getMatchLimit () {
59+ return matchLimit ;
60+ }
61+
3062 private static final int MAX_CACHE_SIZE = 1000 ;
3163
3264 private static final Map <String , Pattern > patternCache =
@@ -105,20 +137,33 @@ private static boolean isValidJavaRegexGroupName(String groupName) {
105137 return groupName .matches ("^[A-Za-z][A-Za-z0-9]*$" );
106138 }
107139
140+ /**
141+ * Create a {@link Matcher} whose backtracking work is bounded by wrapping the input in a {@link
142+ * LimitedCharSequence}. All user-controlled matching (parse and rex) should use this.
143+ *
144+ * @param pattern the compiled pattern
145+ * @param text the input text to match against
146+ * @return a Matcher over a length-bounded view of the text
147+ */
148+ public static Matcher boundedMatcher (Pattern pattern , String text ) {
149+ return pattern .matcher (new LimitedCharSequence (text , pattern , matchLimit ));
150+ }
151+
108152 /**
109153 * Match using find() for partial match semantics with string pattern.
110154 *
111155 * @param text The text to match against
112156 * @param patternStr The pattern string
113157 * @return true if pattern is found anywhere in the text
114158 * @throws PatternSyntaxException if the regex is invalid
159+ * @throws IllegalArgumentException if the match exceeds the backtracking bound
115160 */
116161 public static boolean matchesPartial (String text , String patternStr ) {
117162 if (text == null || patternStr == null ) {
118163 return false ;
119164 }
120165 Pattern pattern = getCompiledPattern (patternStr );
121- return pattern . matcher ( text ).find ();
166+ return boundedMatcher ( pattern , text ).find ();
122167 }
123168
124169 /**
@@ -128,13 +173,14 @@ public static boolean matchesPartial(String text, String patternStr) {
128173 * @param pattern The compiled pattern with named groups
129174 * @param groupName The name of the group to extract
130175 * @return The extracted value or null if not found
176+ * @throws IllegalArgumentException if the match exceeds the backtracking bound
131177 */
132178 public static String extractNamedGroup (String text , Pattern pattern , String groupName ) {
133179 if (text == null || pattern == null || groupName == null ) {
134180 return null ;
135181 }
136182
137- Matcher matcher = pattern . matcher ( text );
183+ Matcher matcher = boundedMatcher ( pattern , text );
138184
139185 if (matcher .matches ()) {
140186 try {
0 commit comments