Skip to content

Commit 783c482

Browse files
authored
Add regex evaluation timeout (#1630)
1 parent 9ee7b8f commit 783c482

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/coverlet.core/Helpers/InstrumentationHelper.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ internal class InstrumentationHelper : IInstrumentationHelper
2424
private readonly IFileSystem _fileSystem;
2525
private readonly ISourceRootTranslator _sourceRootTranslator;
2626
private ILogger _logger;
27+
private static readonly RegexOptions s_regexOptions =
28+
RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase;
2729

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

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

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

361-
var regex = new Regex(modulePattern);
363+
var regex = new Regex(modulePattern, s_regexOptions, TimeSpan.FromSeconds(10));
362364

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

388390
modulePattern = WildcardToRegex(modulePattern);
389391

390-
var regex = new Regex(modulePattern);
392+
var regex = new Regex(modulePattern, s_regexOptions, TimeSpan.FromSeconds(10));
391393

392394
if (regex.IsMatch(module))
393395
return true;
@@ -421,7 +423,7 @@ public bool IsTypeIncluded(string module, string type, string[] includeFilters)
421423
}
422424

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

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

446-
if (new Regex(typePattern).IsMatch(type) && new Regex(modulePattern).IsMatch(module))
448+
if (new Regex(typePattern, s_regexOptions, TimeSpan.FromSeconds(10)).IsMatch(type) && new Regex(modulePattern, s_regexOptions, TimeSpan.FromSeconds(10)).IsMatch(module))
447449
return true;
448450
}
449451

0 commit comments

Comments
 (0)