Skip to content

Commit 95e5a89

Browse files
authored
Typo-squatting Integration (#6414)
* Finish typosquatting integration * push Strings.Designer.cs * Update * Refactor codes, delete user service for typosquatting, optimize linq-queries, and fix some issues. * Add namespace check logic * Add comment for user double check * Update
1 parent 9821300 commit 95e5a89

21 files changed

Lines changed: 599 additions & 426 deletions

src/NuGetGallery/App_Start/DefaultDependenciesModule.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ protected override void Load(ContainerBuilder builder)
323323
.As<ICertificateService>()
324324
.InstancePerLifetimeScope();
325325

326+
builder.RegisterType<TyposquattingService>()
327+
.AsSelf()
328+
.As<ITyposquattingService>()
329+
.InstancePerLifetimeScope();
330+
326331
Func<MailSender> mailSenderFactory = () =>
327332
{
328333
var settings = configuration;

src/NuGetGallery/Controllers/ApiController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,9 @@ await PackageDeleteService.HardDeletePackagesAsync(
723723
package,
724724
packageToPush,
725725
owner,
726-
currentUser);
726+
currentUser,
727+
isNewPackageRegistration: packageRegistration == null);
728+
727729
var afterValidationActionResult = GetActionResultOrNull(afterValidationResult);
728730
if (afterValidationActionResult != null)
729731
{

src/NuGetGallery/Controllers/PackagesController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,6 @@ public virtual async Task<JsonResult> VerifyPackage(VerifyPackageRequest formDat
16011601
return beforeValidationJsonResult;
16021602
}
16031603

1604-
// update relevant database tables
16051604
try
16061605
{
16071606
package = await _packageUploadService.GeneratePackageAsync(
@@ -1637,7 +1636,9 @@ public virtual async Task<JsonResult> VerifyPackage(VerifyPackageRequest formDat
16371636
package,
16381637
nugetPackage,
16391638
owner,
1640-
currentUser);
1639+
currentUser,
1640+
isNewPackageRegistration: existingPackageRegistration == null);
1641+
16411642
var afterValidationJsonResult = GetJsonResultOrNull(afterValidationResult);
16421643
if (afterValidationJsonResult != null)
16431644
{

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@
398398
<Compile Include="Services\ISymbolPackageFileService.cs" />
399399
<Compile Include="Services\ITyposquattingConfiguration.cs" />
400400
<Compile Include="Services\ISymbolsConfiguration.cs" />
401-
<Compile Include="Services\ITyposquattingCheckService.cs" />
402-
<Compile Include="Services\ITyposquattingUserService.cs" />
401+
<Compile Include="Services\ITyposquattingService.cs" />
403402
<Compile Include="Services\SymbolPackageUploadService.cs" />
404403
<Compile Include="Services\SymbolPackageFileService.cs" />
405404
<Compile Include="Services\SymbolPackageService.cs" />
@@ -509,10 +508,9 @@
509508
<Compile Include="Services\ReservedNamespaceService.cs" />
510509
<Compile Include="Services\ReadMeService.cs" />
511510
<Compile Include="Services\TelemetryClientWrapper.cs" />
512-
<Compile Include="Services\TyposquattingCheckService.cs" />
511+
<Compile Include="Services\TyposquattingService.cs" />
513512
<Compile Include="Services\TyposquattingDistanceCalculation.cs" />
514513
<Compile Include="Services\TyposquattingStringNormalization.cs" />
515-
<Compile Include="Services\TyposquattingUserService.cs" />
516514
<Compile Include="Services\ValidationService.cs" />
517515
<Compile Include="Telemetry\ClientInformationTelemetryEnricher.cs" />
518516
<Compile Include="Telemetry\ClientTelemetryPIIProcessor.cs" />

src/NuGetGallery/Services/IPackageService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public interface IPackageService : ICorePackageService
3535

3636
IQueryable<PackageRegistration> FindPackageRegistrationsByOwner(User user);
3737

38+
IQueryable<PackageRegistration> GetAllPackageRegistrations();
39+
3840
IEnumerable<Package> FindDependentPackages(Package package);
3941

4042
/// <summary>

src/NuGetGallery/Services/IPackageUploadService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ Task<Package> GeneratePackageAsync(
3737
/// <param name="nuGetPackage">The package archive reader.</param>
3838
/// <param name="owner">The owner of the package.</param>
3939
/// <param name="currentUser">The current user.</param>
40+
/// <param name="isNewPackageRegistration">Determine whether the uploaded package is a new package without existing package registration info.</param>
4041
/// <returns>The package validation result.</returns>
4142
Task<PackageValidationResult> ValidateAfterGeneratePackageAsync(
4243
Package package,
4344
PackageArchiveReader nuGetPackage,
4445
User owner,
45-
User currentUser);
46+
User currentUser,
47+
bool isNewPackageRegistration);
4648

4749
/// <summary>
4850
/// Commit the provided package metadata and stream to the package file storage and to the database. This
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// 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

4+
using System.Collections.Generic;
5+
46
namespace NuGetGallery
57
{
68
/// <summary>
79
/// This interface is used to check typo-squatting of uploaded package ID with the owner.
810
/// </summary>
9-
public interface ITyposquattingCheckService
11+
public interface ITyposquattingService
1012
{
1113
/// <summary>
1214
/// The function is used to check whether the uploaded package is a typo-squatting package.
1315
/// </summary>
1416
/// <param name="uploadedPackageId"> The package ID of the uploaded package. We check the pacakge ID with the packages in the gallery for typo-squatting issue</param>
1517
/// <param name="uploadedPackageOwner"> The package owner of the uploaded package.</param>
16-
bool IsUploadedPackageIdTyposquatting(string uploadedPackageId, User uploadedPackageOwner);
18+
/// <param name="typosquattingCheckCollisionIds"> The return collision package Id list if it exists</param>
19+
bool IsUploadedPackageIdTyposquatting(string uploadedPackageId, User uploadedPackageOwner, out List<string> typosquattingCheckCollisionIds);
1720
}
1821
}

src/NuGetGallery/Services/ITyposquattingUserService.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/NuGetGallery/Services/PackageService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public async Task<Package> CreatePackageAsync(PackageArchiveReader nugetPackage,
111111
return package;
112112
}
113113

114+
public IQueryable<PackageRegistration> GetAllPackageRegistrations()
115+
{
116+
return _packageRegistrationRepository.GetAll();
117+
}
118+
114119
public override PackageRegistration FindPackageRegistrationById(string packageId)
115120
{
116121
if (packageId == null)

src/NuGetGallery/Services/PackageUploadService.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,24 @@ public class PackageUploadService : IPackageUploadService
2323
private readonly IReservedNamespaceService _reservedNamespaceService;
2424
private readonly IValidationService _validationService;
2525
private readonly IAppConfiguration _config;
26+
private readonly ITyposquattingService _typosquattingService;
2627

2728
public PackageUploadService(
2829
IPackageService packageService,
2930
IPackageFileService packageFileService,
3031
IEntitiesContext entitiesContext,
3132
IReservedNamespaceService reservedNamespaceService,
3233
IValidationService validationService,
33-
IAppConfiguration config)
34+
IAppConfiguration config,
35+
ITyposquattingService typosquattingService)
3436
{
3537
_packageService = packageService ?? throw new ArgumentNullException(nameof(packageService));
3638
_packageFileService = packageFileService ?? throw new ArgumentNullException(nameof(packageFileService));
3739
_entitiesContext = entitiesContext ?? throw new ArgumentNullException(nameof(entitiesContext));
3840
_reservedNamespaceService = reservedNamespaceService ?? throw new ArgumentNullException(nameof(reservedNamespaceService));
3941
_validationService = validationService ?? throw new ArgumentNullException(nameof(validationService));
4042
_config = config ?? throw new ArgumentNullException(nameof(config));
43+
_typosquattingService = typosquattingService ?? throw new ArgumentNullException(nameof(typosquattingService));
4144
}
4245

4346
public async Task<PackageValidationResult> ValidateBeforeGeneratePackageAsync(PackageArchiveReader nuGetPackage, PackageMetadata packageMetadata)
@@ -183,7 +186,8 @@ public async Task<PackageValidationResult> ValidateAfterGeneratePackageAsync(
183186
Package package,
184187
PackageArchiveReader nuGetPackage,
185188
User owner,
186-
User currentUser)
189+
User currentUser,
190+
bool isNewPackageRegistration)
187191
{
188192
var result = await ValidateSignatureFilePresenceAsync(
189193
package.PackageRegistration,
@@ -195,6 +199,11 @@ public async Task<PackageValidationResult> ValidateAfterGeneratePackageAsync(
195199
return result;
196200
}
197201

202+
if (isNewPackageRegistration && _typosquattingService.IsUploadedPackageIdTyposquatting(package.Id, owner, out List<string> typosquattingCheckCollisionIds))
203+
{
204+
return PackageValidationResult.Invalid(string.Format(Strings.TyposquattingCheckFails, string.Join(",", typosquattingCheckCollisionIds)));
205+
}
206+
198207
return PackageValidationResult.Accepted();
199208
}
200209

0 commit comments

Comments
 (0)