A regular expression takes exponential time on some inputs, confirmed with an input that shows it
What it finds
Pattern.compile("^(\\w+\\s?)*$"): on an input that almost matches, such as a long run of letters followed by !, the matcher of java.util.regex tries exponentially many ways to split the letters between the inner and the outer repetition before it gives up. A request of a few dozen characters can keep a thread busy for minutes (CWE-1333).
The pattern is turned into an automaton and searched for ambiguity: a state that reaches itself on one input along two paths makes the time exponential. Every candidate is then confirmed by running the input it suggests on a backtracking matcher that follows the pattern exactly, lookarounds, atomic groups and possessive quantifiers included, with the semantics of the call (matches needs the whole input, find, split and replaceAll search). The finding names that input. Patterns whose subject comes from a request are reported by regex-backtracking-on-input instead.
From the rule's source, regex_backtracking.rs.
import java.util.regex.Pattern; class Slow { boolean a(String s) { return s.matches("((a|a)*)?b"); }tx0 reports: exponential time on boolean b(String s) { return s.matches("(a|a)*b"); } boolean c(String s) { return s.matches("(\\w+,)*"); } boolean d(String s) { return s.matches("((a|a)*+)?b"); }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/java:regex-exponential-backtracking" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:regex-exponential-backtracking] reason="..."tx0 explain tx0/java:regex-exponential-backtracking shows the same in the terminal. Found a false positive? Report it.