Skip to content

Commit

Permalink
Add regex evaluation timeout (#1630)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokarchi authored Feb 26, 2024
1 parent 9ee7b8f commit 783c482
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/coverlet.core/Helpers/InstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ internal class InstrumentationHelper : IInstrumentationHelper
private readonly IFileSystem _fileSystem;
private readonly ISourceRootTranslator _sourceRootTranslator;
private ILogger _logger;
private static readonly RegexOptions s_regexOptions =
RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase;

public InstrumentationHelper(IProcessExitHandler processExitHandler, IRetryHelper retryHelper, IFileSystem fileSystem, ILogger logger, ISourceRootTranslator sourceRootTranslator)
{
Expand Down Expand Up @@ -331,7 +333,7 @@ public bool IsValidFilterExpression(string filter)
if (filter.EndsWith("]"))
return false;

if (new Regex(@"[^\w*]").IsMatch(filter.Replace(".", "").Replace("?", "").Replace("[", "").Replace("]", "")))
if (new Regex(@"[^\w*]", s_regexOptions, TimeSpan.FromSeconds(10)).IsMatch(filter.Replace(".", "").Replace("?", "").Replace("[", "").Replace("]", "")))
return false;

return true;
Expand All @@ -358,7 +360,7 @@ public bool IsModuleExcluded(string module, string[] excludeFilters)
#pragma warning restore IDE0057 // Use range operator
modulePattern = WildcardToRegex(modulePattern);

var regex = new Regex(modulePattern);
var regex = new Regex(modulePattern, s_regexOptions, TimeSpan.FromSeconds(10));

if (regex.IsMatch(module))
return true;
Expand Down Expand Up @@ -387,7 +389,7 @@ public bool IsModuleIncluded(string module, string[] includeFilters)

modulePattern = WildcardToRegex(modulePattern);

var regex = new Regex(modulePattern);
var regex = new Regex(modulePattern, s_regexOptions, TimeSpan.FromSeconds(10));

if (regex.IsMatch(module))
return true;
Expand Down Expand Up @@ -421,7 +423,7 @@ public bool IsTypeIncluded(string module, string type, string[] includeFilters)
}

public bool IsLocalMethod(string method)
=> new Regex(WildcardToRegex("<*>*__*|*")).IsMatch(method);
=> new Regex(WildcardToRegex("<*>*__*|*"), s_regexOptions, TimeSpan.FromSeconds(10)).IsMatch(method);

public void SetLogger(ILogger logger)
{
Expand All @@ -443,7 +445,7 @@ private static bool IsTypeFilterMatch(string module, string type, string[] filte
typePattern = WildcardToRegex(typePattern);
modulePattern = WildcardToRegex(modulePattern);

if (new Regex(typePattern).IsMatch(type) && new Regex(modulePattern).IsMatch(module))
if (new Regex(typePattern, s_regexOptions, TimeSpan.FromSeconds(10)).IsMatch(type) && new Regex(modulePattern, s_regexOptions, TimeSpan.FromSeconds(10)).IsMatch(module))
return true;
}

Expand Down

0 comments on commit 783c482

Please sign in to comment.