Skip to content

Commit bbef32b

Browse files
committed
Don't use Regex when validating a queue name
1 parent f7e4a0d commit bbef32b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Diff for: src/Hangfire.Core/States/EnqueuedState.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ namespace Hangfire.States
6363
/// <threadsafety static="true" instance="false" />
6464
public class EnqueuedState : IState
6565
{
66-
private static readonly Regex ValidationRegex = new Regex(@"^[a-z0-9_-]+$", RegexOptions.Compiled | RegexOptions.CultureInvariant, TimeSpan.FromSeconds(5));
67-
6866
/// <summary>
6967
/// Represents the default queue name. This field is constant.
7068
/// </summary>
@@ -232,7 +230,7 @@ internal static bool TryValidateQueueName([NotNull] string value)
232230
throw new ArgumentNullException(nameof(value));
233231
}
234232

235-
return ValidationRegex.IsMatch(value);
233+
return ValidateQueueNameInner(value);
236234
}
237235

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

245-
if (!ValidationRegex.IsMatch(value))
243+
if (!ValidateQueueNameInner(value))
246244
{
247245
throw new ArgumentException(
248246
$"The queue name must consist of lowercase letters, digits, underscore, and dash characters only. Given: '{value}'.",
249247
parameterName);
250248
}
251249
}
252250

251+
private static bool ValidateQueueNameInner(string value)
252+
{
253+
foreach (var ch in value)
254+
{
255+
// ^[a-z0-9_-]+$
256+
if (!((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_'))
257+
{
258+
return false;
259+
}
260+
}
261+
262+
return true;
263+
}
264+
253265
internal sealed class Handler : IStateHandler
254266
{
255267
public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)

0 commit comments

Comments
 (0)