Regular expressions should always be created with timeouts
Developers on our team sometimes create new regular expressions without timeouts. These have caused several live site incidents where heavy cpu usage occurred.
Developers should not be able to use certain constructors of the Regex class unless they include a timeout parameter:
Bad: var r = new System.Text.RegularExpressions.Regex("."); var r = new System.Text.RegularExpressions.Regex(".", System.Text.RegularExpressions.RegexOptions.None); Good: var r = new System.Text.RegularExpressions.Regex(".*", System.Text.RegularExpressions.RegexOptions.None, TimeSpan.FromSeconds(1));
I think analyzers for .NET API usage should be reviewed in dotnet/runtime first.
@stephentoub @mavasani Can any of you move the issue?
Tagging subscribers to this area: @eerhardt, @pgovind See info in area-owners.md if you want to be subscribed.
Issue Details
Developers on our team sometimes create new regular expressions without timeouts. These have caused several live site incidents where heavy cpu usage occurred.
Developers should not be able to use certain constructors of the Regex class unless they include a timeout parameter:
Bad: var r = new System.Text.RegularExpressions.Regex("."); var r = new System.Text.RegularExpressions.Regex(".", System.Text.RegularExpressions.RegexOptions.None); Good: var r = new System.Text.RegularExpressions.Regex(".*", System.Text.RegularExpressions.RegexOptions.None, TimeSpan.FromSeconds(1));
| Author: | masisley |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Timeouts are important when dealing with certain forms of expressions, particularly with untrusted input. But there's also a large category of expressions where timeouts are irrelevant and also make execution much more expensive, especially as of .NET 5 where execution without timeouts are optimized further. While timeouts are important for untrusted scenarios, I would be hesitant to apply a blanket rule for this when timeouts are often not required (and can make perf a bit worse).
Do we have precedent for analyzers that may reduce perf in favor of possibly increased reliability/security - and do we believe that folks make informed decisions when they chose to use it?
That's not exactly the tradeoff. If you set too low a timeout for something that didn't need them in the first place, you're actually introducing reliability problems.
Timeouts are important for a class of scenarios. I think it's up to the developer to decide whether they're relevant. If we wanted an off-entirely-by- default rule that an app developer could then turn on for their app given what they know about their use of regex, I'd be ok with that. But, for example, examine all the regexes in source.dot.net. 194 call sites to instantiate one... how many of those should be using a timeout but aren't? I've not examined it, but I suspect the number is very low. Requiring it on all regexes would be like saying we must add a string.IndexOf(string, TimeSpan) overload and rewrite all uses of IndexOf to use it for reliability.
That is true. This seems like an analyzer for a select group that would understand the potential impact of adding timeouts. If there is a role for an analyzer here, perhaps it is one that the community creates and distributes just for them. There is always the possibility of adding to the SDK some time in the future if the concern turns out to be unfounded.
If we added a DFA based engine in future there may be some cases where the analyzer would recommend that instead.
I would be hesitant to apply a blanket rule for this when timeouts are often not required
This is exactly where I stand too. I don't think we should enforce timeouts in the API for all users. I'm going to close this issue.
FWIW, @masisley, I think it is feasible to implement a local analyzer that looks for Regex constructors that don't have a timeout specified and suggests a code fix.