Skip to content
This repository has been archived by the owner on Sep 11, 2019. It is now read-only.

Regex operations need timeout #49

Open
VahidN opened this issue Nov 30, 2016 · 4 comments
Open

Regex operations need timeout #49

VahidN opened this issue Nov 30, 2016 · 4 comments

Comments

@VahidN
Copy link

VahidN commented Nov 30, 2016

Consider these samples:

var emailAddressRegex = new Regex(@"^[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*\@[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*\.[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*$|^$");
if (emailAddressRegex.IsMatch("an.infinite.loop.sample.just_for.test"))
{
    Console.WriteLine("Matched!");
}

// Or

var input = "The quick brown fox jumps";
var pattern = @"([a-z ]+)*!";
if (Regex.IsMatch(input, pattern))
{
    Console.WriteLine("Matched!");
}

Both of them will create an infinite loop and they will cause 100% CPU usage.
More info:
Runaway Regular Expressions: Catastrophic Backtracking
Best Practices for Regular Expressions in the .NET Framework

Solution:
Suggest adding timeouts to Regular Expressions.

Regex regexpr = new Regex("[A-Z ]{10}", RegexOptions.Singleline, TimeSpan.FromMilliseconds(1));  
@h3xstream
Copy link
Member

h3xstream commented Nov 30, 2016

I think it would be a good trade off to only enforce the timeout if the RegEx use is at risk.

RegEx at risk typically follow the pattern (a+)+ or (b*)*.

@h3xstream h3xstream added this to the version-2.3.0 milestone Nov 30, 2016
@Siderale
Copy link
Contributor

Siderale commented Dec 6, 2016

I am investigating this one @h3xstream

http://www.cs.bham.ac.uk/~hxt/research/reg-exp-sec.pdf

@h3xstream
Copy link
Member

@Siderale Make sure you use TaintAnalysis extension.

@VahidN
Copy link
Author

VahidN commented Jun 4, 2017

I think Regex DOS (ReDOS) and its tests can be used as an inspiration.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants