Use default timeout for Regex (#1160)

This commit is contained in:
Stef Heyenrath
2024-08-22 20:31:03 +02:00
committed by GitHub
parent d2b53893db
commit 487d6d2db1
8 changed files with 29 additions and 26 deletions

View File

@@ -10,6 +10,7 @@ using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Stef.Validation; using Stef.Validation;
using WireMock.Constants;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Models; using WireMock.Models;
@@ -50,7 +51,7 @@ internal class AzureADAuthenticationMatcher : IStringMatcher
return MatchScores.Mismatch; 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 try
{ {

View File

@@ -6,12 +6,9 @@ using WireMock.Matchers;
namespace WireMock.Authentication; 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); public override string Name => nameof(BasicAuthenticationMatcher);
private static string BuildPattern(string username, string password) private static string BuildPattern(string username, string password)

View File

@@ -2,6 +2,7 @@
#if NET451 || NET452 || NET46 || NET451 || NET461 || NETSTANDARD1_3 || NETSTANDARD2_0 #if NET451 || NET452 || NET46 || NET451 || NET461 || NETSTANDARD1_3 || NETSTANDARD2_0
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using WireMock.Constants;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
namespace System; namespace System;
@@ -11,7 +12,7 @@ internal static class StringExtensions
public static string Replace(this string text, string oldValue, string newValue, StringComparison stringComparison) public static string Replace(this string text, string oldValue, string newValue, StringComparison stringComparison)
{ {
var options = stringComparison == StringComparison.OrdinalIgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None; 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 #endif

View File

@@ -1,15 +1,19 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using System;
namespace WireMock.Constants; namespace WireMock.Constants;
internal static class WireMockConstants internal static class WireMockConstants
{ {
public const int AdminPriority = int.MinValue; internal static readonly TimeSpan DefaultRegexTimeout = TimeSpan.FromSeconds(10);
public const int MinPriority = -1_000_000;
public const int ProxyPriority = -2_000_000; 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"; internal const string ContentTypeJson = "application/json";
public const string ContentTypeTextPlain = "text/plain"; internal const string ContentTypeTextPlain = "text/plain";
public const string NoMatchingFound = "No matching mapping found"; internal const string NoMatchingFound = "No matching mapping found";
} }

View File

@@ -5,10 +5,11 @@ using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using AnyOfTypes; using AnyOfTypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using Stef.Validation;
using WireMock.Constants;
using WireMock.Extensions; using WireMock.Extensions;
using WireMock.Models; using WireMock.Models;
using WireMock.RegularExpressions; using WireMock.RegularExpressions;
using Stef.Validation;
namespace WireMock.Matchers; namespace WireMock.Matchers;
@@ -37,7 +38,7 @@ public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
bool ignoreCase = false, bool ignoreCase = false,
bool useRegexExtended = true, bool useRegexExtended = true,
MatchOperator matchOperator = MatchOperator.Or) : MatchOperator matchOperator = MatchOperator.Or) :
this(MatchBehaviour.AcceptOnMatch, new[] { pattern }, ignoreCase, useRegexExtended, matchOperator) this(MatchBehaviour.AcceptOnMatch, [pattern], ignoreCase, useRegexExtended, matchOperator)
{ {
} }
@@ -55,7 +56,7 @@ public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
bool ignoreCase = false, bool ignoreCase = false,
bool useRegexExtended = true, bool useRegexExtended = true,
MatchOperator matchOperator = MatchOperator.Or) : MatchOperator matchOperator = MatchOperator.Or) :
this(matchBehaviour, new[] { pattern }, ignoreCase, useRegexExtended, matchOperator) this(matchBehaviour, [pattern], ignoreCase, useRegexExtended, matchOperator)
{ {
} }
@@ -86,7 +87,7 @@ public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
options |= RegexOptions.IgnoreCase; 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 /> /// <inheritdoc />

View File

@@ -1,8 +1,8 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using System;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Stef.Validation; using Stef.Validation;
using WireMock.Constants;
namespace WireMock.Util; namespace WireMock.Util;
@@ -11,7 +11,7 @@ namespace WireMock.Util;
/// </summary> /// </summary>
internal static class HttpVersionParser 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> /// <summary>
/// Try to extract the version (as a string) from the protocol. /// Try to extract the version (as a string) from the protocol.

View File

@@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using WireMock.Constants;
namespace WireMock.Util; namespace WireMock.Util;
@@ -13,7 +14,7 @@ namespace WireMock.Util;
/// </summary> /// </summary>
internal static class PortUtils 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> /// <summary>
/// Finds a free TCP port. /// Finds a free TCP port.

View File

@@ -1,16 +1,14 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using WireMock.Constants;
using WireMock.RegularExpressions; using WireMock.RegularExpressions;
namespace WireMock.Util; namespace WireMock.Util;
internal static class RegexUtils internal static class RegexUtils
{ {
private static readonly TimeSpan RegexTimeOut = new(0, 0, 10);
public static Dictionary<string, string> GetNamedGroups(Regex regex, string input) public static Dictionary<string, string> GetNamedGroups(Regex regex, string input)
{ {
var namedGroupsDictionary = new Dictionary<string, string>(); var namedGroupsDictionary = new Dictionary<string, string>();
@@ -38,11 +36,11 @@ internal static class RegexUtils
{ {
if (useRegexExtended) if (useRegexExtended)
{ {
var regexExtended = new RegexExtended(pattern!, RegexOptions.None, RegexTimeOut); var regexExtended = new RegexExtended(pattern!, RegexOptions.None, WireMockConstants.DefaultRegexTimeout);
return (true, regexExtended.IsMatch(input)); 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)); return (true, regex.IsMatch(input));
} }
catch catch