Package org.apache.wiki.util
Class TimeLimitedRegex
java.lang.Object
org.apache.wiki.util.TimeLimitedRegex
Evaluates
java.util.regex matches with an upper bound on matching
time. The JDK regex engine backtracks, so a pathological pattern such as
(a+)+$ matched against a long non-matching subject takes exponential
time and pins a CPU ("catastrophic backtracking" / ReDoS). When either the
pattern or the subject is user-controlled — as with wiki plugin parameters
matched against page content — every match must be time-boxed.
The subject is wrapped in a deadline-checking CharSequence: the
engine reads the subject on every matching step, so a runaway match is
aborted shortly after the deadline passes and reported as a non-match.
- Since:
- 3.0.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault time budget for a single match, in milliseconds. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanmatches(CharSequence subject, Pattern pattern) Attempts to match the whole subject against the given pattern, giving up afterDEFAULT_TIMEOUT_MILLISmilliseconds.static booleanmatches(CharSequence subject, Pattern pattern, long timeoutMillis) Attempts to match the whole subject against the given pattern, giving up aftertimeoutMillismilliseconds.
-
Field Details
-
DEFAULT_TIMEOUT_MILLIS
Default time budget for a single match, in milliseconds. Value is 1000L.- See Also:
-
-
Method Details
-
matches
Attempts to match the whole subject against the given pattern, giving up afterDEFAULT_TIMEOUT_MILLISmilliseconds.- Parameters:
subject- the character sequence to match.pattern- the compiled pattern.- Returns:
trueif the whole subject matches;falseif it doesn't, or if the time budget was exceeded.
-
matches
Attempts to match the whole subject against the given pattern, giving up aftertimeoutMillismilliseconds. A match that exceeds its budget is logged as a warning and treated as a non-match.- Parameters:
subject- the character sequence to match.pattern- the compiled pattern.timeoutMillis- time budget for this match, in milliseconds.- Returns:
trueif the whole subject matches;falseif it doesn't, or if the time budget was exceeded.
-