Skip to content

Commit a97138f

Browse files
Merge pull request #1525 from mus65/regexmatchtimeout
fix possible RegexMatchTimeoutException
2 parents 9b67690 + a99baf2 commit a97138f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/Microsoft.OpenApi/Validations/Rules/OpenApiPathsRules.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Text.RegularExpressions;
76
using Microsoft.OpenApi.Models;
87
using Microsoft.OpenApi.Properties;
98

@@ -36,7 +35,6 @@ public static class OpenApiPathsRules
3635
}
3736
});
3837

39-
private static readonly Regex regexPath = new Regex("\\{([^/}]+)\\}", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100));
4038
/// <summary>
4139
/// A relative path to an individual endpoint. The field name MUST begin with a slash.
4240
/// </summary>
@@ -50,7 +48,7 @@ public static class OpenApiPathsRules
5048
{
5149
context.Enter(path);
5250

53-
var pathSignature = regexPath.Replace(path, "{}");
51+
var pathSignature = GetPathSignature(path);
5452

5553
if (!hashSet.Add(pathSignature))
5654
context.CreateError(nameof(PathMustBeUnique),
@@ -60,6 +58,28 @@ public static class OpenApiPathsRules
6058
}
6159
});
6260

61+
/// <summary>
62+
/// Replaces placeholders in the path with {}, e.g. /pets/{petId} becomes /pets/{} .
63+
/// </summary>
64+
/// <param name="path">The input path</param>
65+
/// <returns>The path signature</returns>
66+
private static string GetPathSignature(string path)
67+
{
68+
for (int openBrace = path.IndexOf('{'); openBrace > -1; openBrace = path.IndexOf('{', openBrace + 2))
69+
{
70+
int closeBrace = path.IndexOf('}', openBrace);
71+
72+
if (closeBrace < 0)
73+
{
74+
return path;
75+
}
76+
77+
path = path.Substring(0, openBrace + 1) + path.Substring(closeBrace);
78+
}
79+
80+
return path;
81+
}
82+
6383
// add more rules
6484
}
6585
}

0 commit comments

Comments
 (0)