Skip to content

Commit

Permalink
Use default timeout for Regex (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Aug 22, 2024
1 parent d2b5389 commit 487d6d2
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
using Stef.Validation;
using WireMock.Constants;
using WireMock.Matchers;
using WireMock.Models;

Expand Down Expand Up @@ -50,7 +51,7 @@ public MatchResult IsMatch(string? input)
return MatchScores.Mismatch;
}

var token = Regex.Replace(input, BearerPrefix, string.Empty, RegexOptions.IgnoreCase);
var token = Regex.Replace(input, BearerPrefix, string.Empty, RegexOptions.IgnoreCase, WireMockConstants.DefaultRegexTimeout);

try
{
Expand Down
7 changes: 2 additions & 5 deletions src/WireMock.Net/Authentication/BasicAuthenticationMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

namespace WireMock.Authentication;

internal class BasicAuthenticationMatcher : RegexMatcher
internal class BasicAuthenticationMatcher(string username, string password)
: RegexMatcher(BuildPattern(username, password))
{
public BasicAuthenticationMatcher(string username, string password) : base(BuildPattern(username, password))
{
}

public override string Name => nameof(BasicAuthenticationMatcher);

private static string BuildPattern(string username, string password)
Expand Down
3 changes: 2 additions & 1 deletion src/WireMock.Net/Compatibility/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#if NET451 || NET452 || NET46 || NET451 || NET461 || NETSTANDARD1_3 || NETSTANDARD2_0
using System.Text.RegularExpressions;
using WireMock.Constants;

// ReSharper disable once CheckNamespace
namespace System;
Expand All @@ -11,7 +12,7 @@ internal static class StringExtensions
public static string Replace(this string text, string oldValue, string newValue, StringComparison stringComparison)
{
var options = stringComparison == StringComparison.OrdinalIgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
return Regex.Replace(text, oldValue, newValue, options);
return Regex.Replace(text, oldValue, newValue, options, WireMockConstants.DefaultRegexTimeout);
}
}
#endif
18 changes: 11 additions & 7 deletions src/WireMock.Net/Constants/WireMockConstants.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Copyright © WireMock.Net

using System;

namespace WireMock.Constants;

internal static class WireMockConstants
{
public const int AdminPriority = int.MinValue;
public const int MinPriority = -1_000_000;
public const int ProxyPriority = -2_000_000;
internal static readonly TimeSpan DefaultRegexTimeout = TimeSpan.FromSeconds(10);

internal const int AdminPriority = int.MinValue;
internal const int MinPriority = -1_000_000;
internal const int ProxyPriority = -2_000_000;

public const string ContentTypeJson = "application/json";
public const string ContentTypeTextPlain = "text/plain";
internal const string ContentTypeJson = "application/json";
internal const string ContentTypeTextPlain = "text/plain";

public const string NoMatchingFound = "No matching mapping found";
}
internal const string NoMatchingFound = "No matching mapping found";
}
9 changes: 5 additions & 4 deletions src/WireMock.Net/Matchers/RegexMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using System.Text.RegularExpressions;
using AnyOfTypes;
using JetBrains.Annotations;
using Stef.Validation;
using WireMock.Constants;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.RegularExpressions;
using Stef.Validation;

namespace WireMock.Matchers;

Expand Down Expand Up @@ -37,7 +38,7 @@ public RegexMatcher(
bool ignoreCase = false,
bool useRegexExtended = true,
MatchOperator matchOperator = MatchOperator.Or) :
this(MatchBehaviour.AcceptOnMatch, new[] { pattern }, ignoreCase, useRegexExtended, matchOperator)
this(MatchBehaviour.AcceptOnMatch, [pattern], ignoreCase, useRegexExtended, matchOperator)
{
}

Expand All @@ -55,7 +56,7 @@ public RegexMatcher(
bool ignoreCase = false,
bool useRegexExtended = true,
MatchOperator matchOperator = MatchOperator.Or) :
this(matchBehaviour, new[] { pattern }, ignoreCase, useRegexExtended, matchOperator)
this(matchBehaviour, [pattern], ignoreCase, useRegexExtended, matchOperator)
{
}

Expand Down Expand Up @@ -86,7 +87,7 @@ public RegexMatcher(
options |= RegexOptions.IgnoreCase;
}

_expressions = patterns.Select(p => useRegexExtended ? new RegexExtended(p.GetPattern(), options) : new Regex(p.GetPattern(), options)).ToArray();
_expressions = patterns.Select(p => useRegexExtended ? new RegexExtended(p.GetPattern(), options) : new Regex(p.GetPattern(), options, WireMockConstants.DefaultRegexTimeout)).ToArray();
}

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions src/WireMock.Net/Util/HttpVersionParser.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright © WireMock.Net

using System;
using System.Text.RegularExpressions;
using Stef.Validation;
using WireMock.Constants;

namespace WireMock.Util;

Expand All @@ -11,7 +11,7 @@ namespace WireMock.Util;
/// </summary>
internal static class HttpVersionParser
{
private static readonly Regex HttpVersionRegex = new(@"HTTP/(\d+(\.\d+)?(?!\.))", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled, TimeSpan.FromMilliseconds(100));
private static readonly Regex HttpVersionRegex = new(@"HTTP/(\d+(\.\d+)?(?!\.))", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled, WireMockConstants.DefaultRegexTimeout);

/// <summary>
/// Try to extract the version (as a string) from the protocol.
Expand Down
3 changes: 2 additions & 1 deletion src/WireMock.Net/Util/PortUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using WireMock.Constants;

namespace WireMock.Util;

Expand All @@ -13,7 +14,7 @@ namespace WireMock.Util;
/// </summary>
internal static class PortUtils
{
private static readonly Regex UrlDetailsRegex = new(@"^((?<proto>\w+)://)(?<host>[^/]+?):(?<port>\d+)\/?$", RegexOptions.Compiled);
private static readonly Regex UrlDetailsRegex = new(@"^((?<proto>\w+)://)(?<host>[^/]+?):(?<port>\d+)\/?$", RegexOptions.Compiled, WireMockConstants.DefaultRegexTimeout);

/// <summary>
/// Finds a free TCP port.
Expand Down
8 changes: 3 additions & 5 deletions src/WireMock.Net/Util/RegexUtils.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Copyright © WireMock.Net

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using WireMock.Constants;
using WireMock.RegularExpressions;

namespace WireMock.Util;

internal static class RegexUtils
{
private static readonly TimeSpan RegexTimeOut = new(0, 0, 10);

public static Dictionary<string, string> GetNamedGroups(Regex regex, string input)
{
var namedGroupsDictionary = new Dictionary<string, string>();
Expand Down Expand Up @@ -38,11 +36,11 @@ public static (bool IsValid, bool Result) MatchRegex(string? pattern, string inp
{
if (useRegexExtended)
{
var regexExtended = new RegexExtended(pattern!, RegexOptions.None, RegexTimeOut);
var regexExtended = new RegexExtended(pattern!, RegexOptions.None, WireMockConstants.DefaultRegexTimeout);
return (true, regexExtended.IsMatch(input));
}

var regex = new Regex(pattern, RegexOptions.None, RegexTimeOut);
var regex = new Regex(pattern, RegexOptions.None, WireMockConstants.DefaultRegexTimeout);
return (true, regex.IsMatch(input));
}
catch
Expand Down

0 comments on commit 487d6d2

Please sign in to comment.