Skip to content

Commit

Permalink
#1537 add regex timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Apr 24, 2024
1 parent e16a4e5 commit 8fd2b92
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions starsky/starsky.foundation.platform/Helpers/ExtensionRolesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace starsky.foundation.platform.Helpers
{
public static class ExtensionRolesHelper
public static partial class ExtensionRolesHelper
{
/// <summary>
/// Xmp sidecar file
Expand Down Expand Up @@ -88,12 +88,8 @@ private static readonly Dictionary<ImageFormat, List<string>>
public static ImageFormat MapFileTypesToExtension(string filename)
{
if ( string.IsNullOrEmpty(filename) ) return ImageFormat.unknown;

// without escaped values:
// \.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$
var matchCollection = new Regex("\\.([0-9a-z]+)(?=[?#])|(\\.)(?:[\\w]+)$",
RegexOptions.None, TimeSpan.FromMilliseconds(100)
).Matches(filename);

var matchCollection = FileExtensionRegex().Matches(filename);
if ( matchCollection.Count == 0 )
{
return ImageFormat.unknown;
Expand Down Expand Up @@ -280,11 +276,8 @@ public static bool IsExtensionSidecar(string? filename)
private static bool IsExtensionForce(string? filename, List<string> checkThisList)
{
if ( string.IsNullOrEmpty(filename) ) return false;

// without escaped values:
// \.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$
var matchCollection = new Regex("\\.([0-9a-z]+)(?=[?#])|(\\.)(?:[\\w]+)$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(200)).Matches(filename);

var matchCollection = FileExtensionRegex().Matches(filename);

if ( matchCollection.Count == 0 ) return false;
foreach ( var matchValue in matchCollection.Select(p => p.Value) )
Expand Down Expand Up @@ -316,10 +309,7 @@ public static string ReplaceExtensionWithXmp(string? filename)
return string.Empty;
}

// without escaped values:
// \.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$
var matchCollection = new Regex("\\.([0-9a-z]+)(?=[?#])|(\\.)(?:[\\w]+)$",
RegexOptions.None, TimeSpan.FromMilliseconds(100)).Matches(filename);
var matchCollection = FileExtensionRegex().Matches(filename);

if ( matchCollection.Count == 0 ) return string.Empty;
foreach ( Match match in matchCollection )
Expand All @@ -339,6 +329,18 @@ public static string ReplaceExtensionWithXmp(string? filename)

return string.Empty;
}

/// <summary>
/// Check for file extensions
/// without escaped values:
/// \.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$
/// </summary>
/// <returns>Regex object</returns>
[GeneratedRegex(
@"\.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase,
matchTimeoutMilliseconds: 500)]
private static partial Regex FileExtensionRegex();

/// <summary>
/// ImageFormat based on first bytes
Expand Down

0 comments on commit 8fd2b92

Please sign in to comment.