A regular expression takes exponential time on some inputs, confirmed with an input that shows it
What it finds
/^(\w+\s?)*$/.test(name): on an input that almost matches, such as a long run of letters followed by !, the backtracking engine of JavaScript tries exponentially many ways to split the letters between the inner and the outer repetition. A string of a few dozen characters blocks the event loop 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, as test, exec, match, replace and split search for the pattern, on a backtracking matcher that follows the pattern exactly, lookarounds included. The finding names that input. Patterns matched against request input are reported by regex-backtracking-on-input instead. Patterns of a language definition of the Monaco editor (an object with a tokenizer) that refer to other patterns as @name are left out: the editor puts the pattern of name in place of @name before it compiles them, so the pattern as written is not the one that runs.
From the rule's source, regex_backtracking.rs.
const a = /^(\w+\s?)*$/;tx0 reports: exponential timeconst b = new RegExp("(a|a)*b");tx0 reports: exponential timeconst c = /(a+)+/;const d = /^(\S+\s+)*x$/;const e = /(a+)+b/y;tx0 reports: exponential timeconst markdown = { escapes: /\\(?:[\\*_`])/, tokenizer: { root: [[/\*([^\\*]|@escapes)+\*/, "strong"]] },};const plain = { root: [[/^([^\\*]|@escapes)+!$/, "strong"]] };tx0 reports: exponential time 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/ts: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/ts:regex-exponential-backtracking] reason="..."tx0 explain tx0/ts:regex-exponential-backtracking shows the same in the terminal. Found a false positive? Report it.