refactor: Add timeouts to all regular expressions

pull/201/head
Robert Dailey 2 years ago
parent 286f4b95b1
commit 134477ab46

@ -52,6 +52,6 @@ public partial class MigrateTrashYmlTest
fs.AllFiles.Should().ContainSingle(x => RecyclarrYmlRegex().IsMatch(x));
}
[GeneratedRegex("[/\\\\]recyclarr\\.yml$")]
[GeneratedRegex("[/\\\\]recyclarr\\.yml$", RegexOptions.None, 1000)]
private static partial Regex RecyclarrYmlRegex();
}

@ -4,11 +4,8 @@ using Recyclarr.Common.Extensions;
namespace Recyclarr.Cli.Pipelines.CustomFormat.Guide;
public class CustomFormatCategoryParser : ICustomFormatCategoryParser
public partial class CustomFormatCategoryParser : ICustomFormatCategoryParser
{
private static readonly Regex TableRegex = new(@"^\s*\|(.*)\|\s*$");
private static readonly Regex LinkRegex = new(@"^\[(.+?)\]\(#(.+?)\)$");
public ICollection<CustomFormatCategoryItem> Parse(IFileInfo collectionOfCustomFormatsMdFile)
{
var columns = new List<List<string>>();
@ -35,7 +32,7 @@ public class CustomFormatCategoryParser : ICustomFormatCategoryParser
private static CustomFormatCategoryItem? ParseLink(string categoryName, string markdownLink)
{
var match = LinkRegex.Match(markdownLink);
var match = LinkRegex().Match(markdownLink);
return match.Success
? new CustomFormatCategoryItem(categoryName, match.Groups[1].Value, match.Groups[2].Value)
: null;
@ -71,14 +68,14 @@ public class CustomFormatCategoryParser : ICustomFormatCategoryParser
return tableRows
// Filter out the `|---|---|---|` part of the table between the heading & data rows.
.Where(x => !Regex.IsMatch(x[0], @"^-+$"));
.Where(x => !Regex.IsMatch(x[0], @"^-+$", RegexOptions.None, TimeSpan.FromMilliseconds(1000)));
}
private static List<string> GetTableRow(string line)
{
var fields = new List<string>();
var match = TableRegex.Match(line);
var match = TableRegex().Match(line);
if (match.Success)
{
var tableRow = match.Groups[1].Value;
@ -87,4 +84,10 @@ public class CustomFormatCategoryParser : ICustomFormatCategoryParser
return fields;
}
[GeneratedRegex("^\\s*\\|(.*)\\|\\s*$", RegexOptions.None, 1000)]
private static partial Regex TableRegex();
[GeneratedRegex("^\\[(.+?)\\]\\(#(.+?)\\)$", RegexOptions.None, 1000)]
private static partial Regex LinkRegex();
}

@ -93,6 +93,6 @@ internal static partial class Program
_tasks.Reverse().ForEach(x => x.OnFinish());
}
[GeneratedRegex("'.*?'")]
[GeneratedRegex("'.*?'", RegexOptions.None, 1000)]
private static partial Regex CommandMessageRegex();
}

@ -16,9 +16,9 @@ public static partial class FileUtils
: LinuxRootRegex().Replace(path, @"C:\").Replace("/", "\\");
}
[GeneratedRegex(@"^C:\\")]
[GeneratedRegex(@"^C:\\", RegexOptions.None, 1000)]
private static partial Regex WindowsRootRegex();
[GeneratedRegex("^/")]
[GeneratedRegex("^/", RegexOptions.None, 1000)]
private static partial Regex LinuxRootRegex();
}

@ -9,9 +9,8 @@ using Recyclarr.TrashLib.Interfaces;
namespace Recyclarr.TrashLib.Cache;
public class ServiceCache : IServiceCache
public partial class ServiceCache : IServiceCache
{
private static readonly Regex AllowedObjectNameCharacters = new(@"^[\w-]+$", RegexOptions.Compiled);
private readonly ICacheStoragePath _storagePath;
private readonly JsonSerializerSettings _jsonSettings;
@ -79,11 +78,14 @@ public class ServiceCache : IServiceCache
private IFileInfo PathFromAttribute<T>(IServiceConfiguration config)
{
var objectName = GetCacheObjectNameAttribute<T>();
if (!AllowedObjectNameCharacters.IsMatch(objectName))
if (!AllowedObjectNameCharactersRegex().IsMatch(objectName))
{
throw new ArgumentException($"Object name '{objectName}' has unacceptable characters");
}
return _storagePath.CalculatePath(config, objectName);
}
[GeneratedRegex("^[\\w-]+$", RegexOptions.None, 1000)]
private static partial Regex AllowedObjectNameCharactersRegex();
}

Loading…
Cancel
Save