Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regex evaluation timeout #1630

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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