An unbounded repetition in a regular expression contains another one, which can backtrack exponentially

tx0/ts:regex-nested-repetitionrecommendedwarningsecurity, cwe-1333

What it finds

/(a+)+$/, /(\w*)*x/, /(?:.*,)*end/: a repetition with no upper bound around a group that itself repeats without bound can match the same text in exponentially many ways. On an input that almost matches, the backtracking engine of JavaScript tries all of them, and a short string can block the event loop for minutes. Where the input comes from users, this is a denial of service.

Reported are *, + and {n,} applied to a group that contains, at any depth, another unbounded repetition. The pattern can usually be rewritten so that each part of the input can be matched in one way only.

From the rule's source, regex_nested_repetition.rs.

Example from the rule's tests
const a = /(a+)+$/;tx0 reports: a repeated group in this pattern contains another unbounded repetitionconst b = /^(?:\w*\s?)*$/;tx0 reports: a repeated group in this pattern contains another unbounded repetitionconst c = /(ab)+/;const d = /(a{1,3})+/;const e = /\d+(?:\.\d+)?/;
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/ts:regex-nested-repetition" = "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/ts:regex-nested-repetition] reason="..."

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