A regular expression built from a variable pattern runs without a timeout
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.
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 }} 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/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.