Skip to content

Commit

Permalink
Don't use Regex when validating a queue name
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Nov 13, 2024
1 parent f7e4a0d commit bbef32b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Hangfire.Core/States/EnqueuedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ namespace Hangfire.States
/// <threadsafety static="true" instance="false" />
public class EnqueuedState : IState
{
private static readonly Regex ValidationRegex = new Regex(@"^[a-z0-9_-]+$", RegexOptions.Compiled | RegexOptions.CultureInvariant, TimeSpan.FromSeconds(5));

/// <summary>
/// Represents the default queue name. This field is constant.
/// </summary>
Expand Down Expand Up @@ -232,7 +230,7 @@ internal static bool TryValidateQueueName([NotNull] string value)
throw new ArgumentNullException(nameof(value));
}

return ValidationRegex.IsMatch(value);
return ValidateQueueNameInner(value);
}

internal static void ValidateQueueName([InvokerParameterName] string parameterName, [NotNull] string value)
Expand All @@ -242,14 +240,28 @@ internal static void ValidateQueueName([InvokerParameterName] string parameterNa
throw new ArgumentNullException(parameterName);
}

if (!ValidationRegex.IsMatch(value))
if (!ValidateQueueNameInner(value))
{
throw new ArgumentException(
$"The queue name must consist of lowercase letters, digits, underscore, and dash characters only. Given: '{value}'.",
parameterName);
}
}

private static bool ValidateQueueNameInner(string value)
{
foreach (var ch in value)
{
// ^[a-z0-9_-]+$
if (!((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_'))
{
return false;
}
}

return true;
}

internal sealed class Handler : IStateHandler
{
public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
Expand Down

0 comments on commit bbef32b

Please sign in to comment.