Skip to content

Commit 487d6d2

Browse files
authored
Use default timeout for Regex (#1160)
1 parent d2b5389 commit 487d6d2

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

src/WireMock.Net/Authentication/AzureADAuthenticationMatcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
1111
using Microsoft.IdentityModel.Tokens;
1212
using Stef.Validation;
13+
using WireMock.Constants;
1314
using WireMock.Matchers;
1415
using WireMock.Models;
1516

@@ -50,7 +51,7 @@ public MatchResult IsMatch(string? input)
5051
return MatchScores.Mismatch;
5152
}
5253

53-
var token = Regex.Replace(input, BearerPrefix, string.Empty, RegexOptions.IgnoreCase);
54+
var token = Regex.Replace(input, BearerPrefix, string.Empty, RegexOptions.IgnoreCase, WireMockConstants.DefaultRegexTimeout);
5455

5556
try
5657
{

src/WireMock.Net/Authentication/BasicAuthenticationMatcher.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66

77
namespace WireMock.Authentication;
88

9-
internal class BasicAuthenticationMatcher : RegexMatcher
9+
internal class BasicAuthenticationMatcher(string username, string password)
10+
: RegexMatcher(BuildPattern(username, password))
1011
{
11-
public BasicAuthenticationMatcher(string username, string password) : base(BuildPattern(username, password))
12-
{
13-
}
14-
1512
public override string Name => nameof(BasicAuthenticationMatcher);
1613

1714
private static string BuildPattern(string username, string password)

src/WireMock.Net/Compatibility/StringExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#if NET451 || NET452 || NET46 || NET451 || NET461 || NETSTANDARD1_3 || NETSTANDARD2_0
44
using System.Text.RegularExpressions;
5+
using WireMock.Constants;
56

67
// ReSharper disable once CheckNamespace
78
namespace System;
@@ -11,7 +12,7 @@ internal static class StringExtensions
1112
public static string Replace(this string text, string oldValue, string newValue, StringComparison stringComparison)
1213
{
1314
var options = stringComparison == StringComparison.OrdinalIgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
14-
return Regex.Replace(text, oldValue, newValue, options);
15+
return Regex.Replace(text, oldValue, newValue, options, WireMockConstants.DefaultRegexTimeout);
1516
}
1617
}
1718
#endif
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
// Copyright © WireMock.Net
22

3+
using System;
4+
35
namespace WireMock.Constants;
46

57
internal static class WireMockConstants
68
{
7-
public const int AdminPriority = int.MinValue;
8-
public const int MinPriority = -1_000_000;
9-
public const int ProxyPriority = -2_000_000;
9+
internal static readonly TimeSpan DefaultRegexTimeout = TimeSpan.FromSeconds(10);
10+
11+
internal const int AdminPriority = int.MinValue;
12+
internal const int MinPriority = -1_000_000;
13+
internal const int ProxyPriority = -2_000_000;
1014

11-
public const string ContentTypeJson = "application/json";
12-
public const string ContentTypeTextPlain = "text/plain";
15+
internal const string ContentTypeJson = "application/json";
16+
internal const string ContentTypeTextPlain = "text/plain";
1317

14-
public const string NoMatchingFound = "No matching mapping found";
15-
}
18+
internal const string NoMatchingFound = "No matching mapping found";
19+
}

src/WireMock.Net/Matchers/RegexMatcher.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
using System.Text.RegularExpressions;
66
using AnyOfTypes;
77
using JetBrains.Annotations;
8+
using Stef.Validation;
9+
using WireMock.Constants;
810
using WireMock.Extensions;
911
using WireMock.Models;
1012
using WireMock.RegularExpressions;
11-
using Stef.Validation;
1213

1314
namespace WireMock.Matchers;
1415

@@ -37,7 +38,7 @@ public RegexMatcher(
3738
bool ignoreCase = false,
3839
bool useRegexExtended = true,
3940
MatchOperator matchOperator = MatchOperator.Or) :
40-
this(MatchBehaviour.AcceptOnMatch, new[] { pattern }, ignoreCase, useRegexExtended, matchOperator)
41+
this(MatchBehaviour.AcceptOnMatch, [pattern], ignoreCase, useRegexExtended, matchOperator)
4142
{
4243
}
4344

@@ -55,7 +56,7 @@ public RegexMatcher(
5556
bool ignoreCase = false,
5657
bool useRegexExtended = true,
5758
MatchOperator matchOperator = MatchOperator.Or) :
58-
this(matchBehaviour, new[] { pattern }, ignoreCase, useRegexExtended, matchOperator)
59+
this(matchBehaviour, [pattern], ignoreCase, useRegexExtended, matchOperator)
5960
{
6061
}
6162

@@ -86,7 +87,7 @@ public RegexMatcher(
8687
options |= RegexOptions.IgnoreCase;
8788
}
8889

89-
_expressions = patterns.Select(p => useRegexExtended ? new RegexExtended(p.GetPattern(), options) : new Regex(p.GetPattern(), options)).ToArray();
90+
_expressions = patterns.Select(p => useRegexExtended ? new RegexExtended(p.GetPattern(), options) : new Regex(p.GetPattern(), options, WireMockConstants.DefaultRegexTimeout)).ToArray();
9091
}
9192

9293
/// <inheritdoc />

src/WireMock.Net/Util/HttpVersionParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright © WireMock.Net
22

3-
using System;
43
using System.Text.RegularExpressions;
54
using Stef.Validation;
5+
using WireMock.Constants;
66

77
namespace WireMock.Util;
88

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

1616
/// <summary>
1717
/// Try to extract the version (as a string) from the protocol.

src/WireMock.Net/Util/PortUtils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Net;
66
using System.Net.Sockets;
77
using System.Text.RegularExpressions;
8+
using WireMock.Constants;
89

910
namespace WireMock.Util;
1011

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

1819
/// <summary>
1920
/// Finds a free TCP port.

src/WireMock.Net/Util/RegexUtils.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// Copyright © WireMock.Net
22

3-
using System;
43
using System.Collections.Generic;
54
using System.Text.RegularExpressions;
5+
using WireMock.Constants;
66
using WireMock.RegularExpressions;
77

88
namespace WireMock.Util;
99

1010
internal static class RegexUtils
1111
{
12-
private static readonly TimeSpan RegexTimeOut = new(0, 0, 10);
13-
1412
public static Dictionary<string, string> GetNamedGroups(Regex regex, string input)
1513
{
1614
var namedGroupsDictionary = new Dictionary<string, string>();
@@ -38,11 +36,11 @@ public static (bool IsValid, bool Result) MatchRegex(string? pattern, string inp
3836
{
3937
if (useRegexExtended)
4038
{
41-
var regexExtended = new RegexExtended(pattern!, RegexOptions.None, RegexTimeOut);
39+
var regexExtended = new RegexExtended(pattern!, RegexOptions.None, WireMockConstants.DefaultRegexTimeout);
4240
return (true, regexExtended.IsMatch(input));
4341
}
4442

45-
var regex = new Regex(pattern, RegexOptions.None, RegexTimeOut);
43+
var regex = new Regex(pattern, RegexOptions.None, WireMockConstants.DefaultRegexTimeout);
4644
return (true, regex.IsMatch(input));
4745
}
4846
catch

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy