A regular expression built from a variable pattern runs without a timeout

tx0/csharp:regex-without-timeoutstrictwarningsecurity/denial-of-servicedepth fastsecurity, cwe-1333

What it finds

The .NET regular expression engine backtracks, and some patterns take exponential time on crafted input. When the pattern itself comes from a variable, nobody has checked it for such cases; a match timeout, or RegexOptions.NonBacktracking, bounds the cost. Literal patterns are left alone.

From the rule's source, regex_without_timeout.rs.

Example from the rule's tests
using System;using System.Text.RegularExpressions; class Search{    bool Find(string input, string pattern)    {        var fixedOne = new Regex("^a+$");        var risky = new Regex(pattern);tx0 reports: a regular expression with a variable pattern has no timeout        var bounded = new Regex(pattern, RegexOptions.None, TimeSpan.FromSeconds(1));        return Regex.IsMatch(input, pattern);tx0 reports: a regular expression with a variable pattern has no timeout    }}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:regex-without-timeout" = "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/csharp:regex-without-timeout] reason="..."

tx0 explain tx0/csharp:regex-without-timeout shows the same in the terminal. Found a false positive? Report it.