Skip to content

Commit a0671e9

Browse files
kartheekp-mszivkan
authored andcommitted
Use a timeout when validating NuGet package id
1 parent 606dcb6 commit a0671e9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/NuGet.Core/NuGet.Packaging/PackageCreation/Utility/PackageIdValidator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace NuGet.Packaging
1111
public static class PackageIdValidator
1212
{
1313
public const int MaxPackageIdLength = 100;
14-
private static readonly Regex IdRegex = new Regex(@"^\w+([_.-]\w+)*$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
14+
private static readonly Regex IdRegex = new Regex(pattern: @"^\w+([.-]\w+)*$",
15+
options: RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant,
16+
matchTimeout: TimeSpan.FromSeconds(10));
1517

1618
public static bool IsValidPackageId(string packageId)
1719
{

test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageIdValidatorTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,32 @@ public void DotToolsIsNotAllowed()
159159
Assert.False(isValid);
160160
}
161161

162+
[Fact]
163+
public void IsValidPackageId_PackageIdWithTwoUnderscores_Success()
164+
{
165+
// Arrange
166+
string packageId = "Hello__World";
167+
168+
// Act
169+
bool isValid = PackageIdValidator.IsValidPackageId(packageId);
170+
171+
// Assert
172+
Assert.True(isValid);
173+
}
174+
175+
[Fact]
176+
public void IsValidPackageId_LongPackageIdWithUnMatchedCharAtTheEnd_Fails()
177+
{
178+
// Arrange
179+
string packageId = string.Concat(new string('_', 100), "!");
180+
181+
// Act
182+
bool isValid = PackageIdValidator.IsValidPackageId(packageId);
183+
184+
// Assert
185+
Assert.False(isValid);
186+
}
187+
162188
[Theory]
163189
[InlineData(101)]
164190
[InlineData(102)]

0 commit comments

Comments
 (0)