A repeated part of a regular expression contains another unbounded repetition, which can take exponential time

tx0/java:regex-nested-quantifierrecommendedwarningbug, security, cwe-1333

What it finds

(\w+\s?)*$ takes time exponential in the length of an input that almost matches: the inner \w+ and the outer * can split the same characters in very many ways, and the matcher of java.util.regex tries them all before it gives up. On input from outside this is a denial of service (CWE-1333).

Reported is an unbounded greedy or reluctant repetition whose body is another unbounded repetition with nothing else in it that must match, as in (a+)+ or (\w+\s?)*, but not (\S+\s+)*, whose two required parts split the input one way only. Possessive quantifiers and atomic groups, which never retry, are left alone.

From the rule's source, regex_nested_quantifier.rs.

Example from the rule's tests
import java.util.regex.Pattern; class Slow {  Pattern a = Pattern.compile("(a+)+$");tx0 reports: exponential time  Pattern b = Pattern.compile("^(\\w+\\s?)*$");tx0 reports: exponential time  Pattern c = Pattern.compile("(\\w+,)*");  Pattern d = Pattern.compile("(?>a+)+");  Pattern e = Pattern.compile("(a++)+");}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:regex-nested-quantifier" = "warning"

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-nested-quantifier] reason="..."

tx0 explain tx0/java:regex-nested-quantifier shows the same in the terminal. Found a false positive? Report it.