Package org.jline.utils
Class SafeRegex
java.lang.Object
org.jline.utils.SafeRegex
Regex utilities that guard against catastrophic backtracking (ReDoS).
Java's java.util.regex engine uses backtracking, so patterns with
nested quantifiers (e.g. (a+)+b) can take exponential time on
non-matching input. This class wraps input in a CharSequence that
enforces a wall-clock deadline, throwing RegexTimeoutException if
matching takes too long.
Usage:
- For simple boolean checks, use
matches(java.util.regex.Pattern, java.lang.CharSequence)orfind(java.util.regex.Pattern, java.lang.CharSequence)— they catch timeouts and returnfalse. - When you need the
Matcher(e.g. for a find loop or to read match groups), usematcher(java.util.regex.Pattern, java.lang.CharSequence)and catchRegexTimeoutExceptionyourself. - For glob-style patterns (only
*is special), usecompileGlob(java.lang.String)to compile aPatternthat properly escapes literal characters. Note:compileGlobonly builds the pattern; to get timeout protection, pass the result throughmatcher(java.util.regex.Pattern, java.lang.CharSequence),matches(java.util.regex.Pattern, java.lang.CharSequence), orfind(java.util.regex.Pattern, java.lang.CharSequence).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classACharSequencewrapper that enforces a wall-clock deadline. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intHow often (incharAtcalls) the deadline is checked.private static final longDefault timeout for regex matching operations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidappendQuoted(StringBuilder sb, char ch) static PatterncompileGlob(String globPattern) Compile a glob-style pattern into aPattern.static PatterncompileGlob(String globPattern, int flags) Compile a glob-style pattern into aPatternwith the given regex flags.static booleanfind(Pattern pattern, CharSequence input) Test whether the pattern is found anywhere in the input, with timeout protection.static Matchermatcher(Pattern pattern, CharSequence input) Create aMatcherthat will throwRegexTimeoutExceptionif matching exceeds the default timeout.static Matchermatcher(Pattern pattern, CharSequence input, long timeoutMs) Create aMatcherthat will throwRegexTimeoutExceptionif matching exceeds the given timeout.static booleanmatches(Pattern pattern, CharSequence input) Test whether the pattern matches the entire input, with timeout protection.
-
Field Details
-
DEFAULT_TIMEOUT_MS
private static final long DEFAULT_TIMEOUT_MSDefault timeout for regex matching operations.- See Also:
-
CHECK_INTERVAL
private static final int CHECK_INTERVALHow often (incharAtcalls) the deadline is checked. Checking every call would add measurable overhead; checking every 1024 calls keeps the cost negligible while still catching runaway backtracking within milliseconds on typical input.- See Also:
-
-
Constructor Details
-
SafeRegex
private SafeRegex()
-
-
Method Details
-
matcher
Create aMatcherthat will throwRegexTimeoutExceptionif matching exceeds the default timeout. -
matcher
Create aMatcherthat will throwRegexTimeoutExceptionif matching exceeds the given timeout. The timeout starts lazily on the first deadline check during matching (not when theMatcheris created), so it measures actual matching time. -
matches
Test whether the pattern matches the entire input, with timeout protection. Returnsfalseon timeout. -
find
Test whether the pattern is found anywhere in the input, with timeout protection. Returnsfalseon timeout. -
compileGlob
Compile a glob-style pattern into aPattern.Only
*(match any string) and\(escape) are special; every other character is regex-quoted so it matches literally. This is suitable for user-facing wildcard syntax where full regex power is not intended.- Parameters:
globPattern- the glob pattern (e.g."foo*bar")- Returns:
- a compiled regex pattern
-
compileGlob
Compile a glob-style pattern into aPatternwith the given regex flags.- Parameters:
globPattern- the glob patternflags- regex flags (e.g.Pattern.DOTALL)- Returns:
- a compiled regex pattern
-
appendQuoted
-