Skip to content

Commit e363891

Browse files
authored
[HotFix] Temporary limit to ascii only (#10409)
Temporary allow ascii only
1 parent 8c990b2 commit e363891

12 files changed

Lines changed: 161 additions & 24 deletions

File tree

src/AccountDeleter/EmptyFeatureFlagService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,5 +328,10 @@ public bool CanUseFederatedCredentials(User user)
328328
{
329329
throw new NotImplementedException();
330330
}
331+
332+
public bool IsAsciiOnlyPackageIdEnabled()
333+
{
334+
throw new NotImplementedException();
335+
}
331336
}
332337
}

src/GitHubVulnerabilities2Db/Fakes/FakeFeatureFlagService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,10 @@ public bool CanUseFederatedCredentials(User user)
329329
{
330330
throw new NotImplementedException();
331331
}
332+
333+
public bool IsAsciiOnlyPackageIdEnabled()
334+
{
335+
throw new NotImplementedException();
336+
}
332337
}
333338
}

src/NuGetGallery.Core/Packaging/ManifestValidator.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -7,14 +7,15 @@
77
using System.Globalization;
88
using System.IO;
99
using System.Linq;
10+
using System.Net;
1011
using NuGet.Packaging;
1112
using NuGet.Versioning;
1213

1314
namespace NuGetGallery.Packaging
1415
{
1516
public class ManifestValidator
1617
{
17-
public static IEnumerable<ValidationResult> Validate(Stream nuspecStream, out NuspecReader nuspecReader, out PackageMetadata packageMetadata)
18+
public static IEnumerable<ValidationResult> Validate(Stream nuspecStream, bool asciiOnlyPackageIds, out NuspecReader nuspecReader, out PackageMetadata packageMetadata)
1819
{
1920
packageMetadata = null;
2021

@@ -25,7 +26,7 @@ public static IEnumerable<ValidationResult> Validate(Stream nuspecStream, out Nu
2526
if (rawMetadata != null && rawMetadata.Any())
2627
{
2728
packageMetadata = PackageMetadata.FromNuspecReader(nuspecReader, strict: true);
28-
return ValidateCore(packageMetadata);
29+
return ValidateCore(packageMetadata, asciiOnlyPackageIds);
2930
}
3031
}
3132
catch (Exception ex)
@@ -38,7 +39,7 @@ public static IEnumerable<ValidationResult> Validate(Stream nuspecStream, out Nu
3839
return Enumerable.Empty<ValidationResult>();
3940
}
4041

41-
private static IEnumerable<ValidationResult> ValidateCore(PackageMetadata packageMetadata)
42+
private static IEnumerable<ValidationResult> ValidateCore(PackageMetadata packageMetadata, bool asciiOnlyPackageIds)
4243
{
4344
// Validate the ID
4445
if (string.IsNullOrEmpty(packageMetadata.Id))
@@ -51,12 +52,21 @@ private static IEnumerable<ValidationResult> ValidateCore(PackageMetadata packag
5152
{
5253
yield return new ValidationResult(CoreStrings.Manifest_IdTooLong);
5354
}
54-
else if (!PackageIdValidator.IsValidPackageId(packageMetadata.Id))
55+
else
5556
{
56-
yield return new ValidationResult(String.Format(
57-
CultureInfo.CurrentCulture,
58-
CoreStrings.Manifest_InvalidId,
59-
packageMetadata.Id));
57+
if (!PackageIdValidator.IsValidPackageId(packageMetadata.Id))
58+
{
59+
yield return new ValidationResult(string.Format(
60+
CultureInfo.CurrentCulture,
61+
CoreStrings.Manifest_InvalidId,
62+
packageMetadata.Id));
63+
} else if (asciiOnlyPackageIds && !PackageIdValidator.IsAsciiOnlyPackageId(packageMetadata.Id))
64+
{
65+
yield return new ValidationResult(string.Format(
66+
CultureInfo.CurrentCulture,
67+
"Non-ASCII characters in package Id are temporary blocked, please check https://aka.ms/nuget/non-ascii-ids for updates.",
68+
packageMetadata.Id));
69+
}
6070
}
6171
}
6272

src/NuGetGallery.Core/Packaging/PackageIdValidator.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,40 @@ public static class PackageIdValidator
1414
@"^\w+([.-]\w+)*$",
1515
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
1616

17+
private static readonly Regex AllAsciiRegex = RegexEx.CreateWithTimeout(
18+
@"^[A-Za-z0-9\-_\.]+$",
19+
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
20+
1721
public static bool IsValidPackageId(string packageId)
1822
{
1923
if (packageId == null)
2024
{
2125
throw new ArgumentNullException(nameof(packageId));
2226
}
2327

24-
if (String.Equals(packageId, "$id$", StringComparison.OrdinalIgnoreCase))
28+
if (string.Equals(packageId, "$id$", StringComparison.OrdinalIgnoreCase))
2529
{
2630
return false;
2731
}
2832

2933
return IdRegex.IsMatch(packageId);
3034
}
3135

36+
public static bool IsAsciiOnlyPackageId(string packageId)
37+
{
38+
if (packageId == null)
39+
{
40+
throw new ArgumentNullException(nameof(packageId));
41+
}
42+
43+
if (string.Equals(packageId, "$id$", StringComparison.OrdinalIgnoreCase))
44+
{
45+
return false;
46+
}
47+
48+
return AllAsciiRegex.IsMatch(packageId);
49+
}
50+
3251
public static void ValidatePackageId(string packageId)
3352
{
3453
if (packageId == null)
@@ -45,9 +64,9 @@ public static void ValidatePackageId(string packageId)
4564
{
4665
throw new ArgumentException(string.Format(
4766
CultureInfo.CurrentCulture,
48-
"The package ID '{0}' contains invalid characters. Examples of valid package IDs include 'MyPackage' and 'MyPackage.Sample'.",
67+
"The package ID '{0}' contains invalid characters. Package ID can only contain alphanumeric characters, hyphens, underscores, and periods.",
4968
packageId));
5069
}
5170
}
5271
}
53-
}
72+
}

src/NuGetGallery.Services/Configuration/FeatureFlagService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class FeatureFlagService : IFeatureFlagService
6464
private const string DisplayTfmBadgesFeatureName = GalleryPrefix + "DisplayTfmBadges";
6565
private const string AdvancedFrameworkFilteringFeatureName = GalleryPrefix + "AdvancedFrameworkFiltering";
6666
private const string FederatedCredentialsFeatureName = GalleryPrefix + "FederatedCredentials";
67+
private const string AsciiOnlyPackageIdFeatureName = GalleryPrefix + "AsciiOnlyPackageId";
6768

6869
private const string ODataV1GetAllNonHijackedFeatureName = GalleryPrefix + "ODataV1GetAllNonHijacked";
6970
private const string ODataV1GetAllCountNonHijackedFeatureName = GalleryPrefix + "ODataV1GetAllCountNonHijacked";
@@ -427,5 +428,10 @@ public bool CanUseFederatedCredentials(User user)
427428
{
428429
return _client.IsEnabled(FederatedCredentialsFeatureName, user, defaultValue: false);
429430
}
431+
432+
public bool IsAsciiOnlyPackageIdEnabled()
433+
{
434+
return _client.IsEnabled(AsciiOnlyPackageIdFeatureName, defaultValue: false);
435+
}
430436
}
431437
}

src/NuGetGallery.Services/Configuration/IFeatureFlagService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,5 +347,10 @@ public interface IFeatureFlagService
347347
/// Whether or not the user specified in a package owner scope can use federated credentials.
348348
/// </summary>
349349
bool CanUseFederatedCredentials(User user);
350+
351+
/// <summary>
352+
/// Whether or not only ASCII characters are allowed in PackageId, used for temporary block unicode.
353+
/// </summary>
354+
bool IsAsciiOnlyPackageIdEnabled();
350355
}
351356
}

src/NuGetGallery/Controllers/ApiController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ private async Task<ActionResult> CreatePackageInternal()
617617

618618
NuspecReader nuspec;
619619
PackageMetadata packageMetadata;
620-
var errors = ManifestValidator.Validate(packageToPush.GetNuspec(), out nuspec, out packageMetadata).ToArray();
620+
var errors = ManifestValidator.Validate(packageToPush.GetNuspec(), FeatureFlagService.IsAsciiOnlyPackageIdEnabled(), out nuspec, out packageMetadata).ToArray();
621621
if (errors.Length > 0)
622622
{
623623
var errorsString = string.Join("', '", errors.Select(error => error.ErrorMessage));

src/NuGetGallery/Controllers/PackagesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public virtual async Task<JsonResult> UploadPackage(HttpPostedFileBase uploadFil
420420
PackageArchiveReader packageArchiveReader = CreatePackage(uploadStream);
421421
NuspecReader nuspec;
422422
PackageMetadata packageMetadata;
423-
var errors = ManifestValidator.Validate(packageArchiveReader.GetNuspec(), out nuspec, out packageMetadata).ToArray();
423+
var errors = ManifestValidator.Validate(packageArchiveReader.GetNuspec(), _featureFlagService.IsAsciiOnlyPackageIdEnabled(), out nuspec, out packageMetadata).ToArray();
424424
if (errors.Length > 0)
425425
{
426426
var errorStrings = new List<JsonValidationMessage>();

src/NuGetGallery/Services/SymbolPackageService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -120,7 +120,7 @@ private static void ValidateSymbolPackage(PackageArchiveReader symbolPackage, Pa
120120
PackageHelper.ValidateNuGetPackageMetadata(metadata);
121121

122122
// Validate nuspec manifest.
123-
var errors = ManifestValidator.Validate(symbolPackage.GetNuspec(), out var nuspec, out var packageMetadata).ToArray();
123+
var errors = ManifestValidator.Validate(symbolPackage.GetNuspec(), false, out var nuspec, out var packageMetadata).ToArray();
124124
if (errors.Length > 0)
125125
{
126126
var errorsString = string.Join("', '", errors.Select(error => error.ErrorMessage));
@@ -201,4 +201,4 @@ private static bool IsPortable(string pdbFile)
201201
return currentPDBStamp.SequenceEqual(portableStamp);
202202
}
203203
}
204-
}
204+
}

src/VerifyMicrosoftPackage/Fakes/FakeFeatureFlagService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,7 @@ public class FakeFeatureFlagService : IFeatureFlagService
137137
public bool IsAdvancedFrameworkFilteringEnabled(User user) => throw new NotImplementedException();
138138

139139
public bool CanUseFederatedCredentials(User user) => throw new NotImplementedException();
140+
141+
public bool IsAsciiOnlyPackageIdEnabled() => throw new NotImplementedException();
140142
}
141143
}

0 commit comments

Comments
 (0)