A regular expression takes exponential time on some inputs, confirmed with an input that shows it
What it finds
Regex("^(\\w+\\s?)*$"): on an input that almost matches, such as a long run of letters followed by !, the backtracking 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 string of a few dozen characters can keep a thread busy for minutes (CWE-1333), which is a denial of service when the string comes from a user.
The pattern is searched for ambiguity, and every candidate is confirmed by running the input it suggests on a matcher that follows the pattern exactly, matching the whole input. Patterns that match the empty string, which succeed at once when a Regex is used to search, are left alone. The finding names the input.
From the rule's source, regex_patterns.rs.
val nested = "((a|a)*)?b".toRegex()tx0 reports: exponential time onval linear = Regex("^[a-z]+(,[a-z]+)*$")val possessive = Regex("((a|a)*+)?b")val empty = Regex("(a|a)*") Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:regex-exponential-backtracking" = "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/kotlin:regex-exponential-backtracking] reason="..."tx0 explain tx0/kotlin:regex-exponential-backtracking shows the same in the terminal. Found a false positive? Report it.