Skip to content

Commit

Permalink
Abstract typosquatting (#9767)
Browse files Browse the repository at this point in the history
  • Loading branch information
erdembayar authored Jan 8, 2024
1 parent 2a982dc commit c1115f8
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 559 deletions.
19 changes: 19 additions & 0 deletions src/NuGetGallery.Core/Services/ITyposquattingServiceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace NuGetGallery
{
/// <summary>
/// This interface for providing additional methods for ITyposquattingService.
/// </summary>
public interface ITyposquattingServiceHelper
{
/// <summary>
/// This method is used to check if the distance between the currently uploaded package ID and another package ID is less than or equal to the threshold.
/// </summary>
/// <param name="uploadedPackageId">Uploaded package Id</param>
/// <param name="packageId">Package Id compared to</param>
/// <returns>Return true if distance is less than the threshold</returns>
bool IsDistanceLessThanOrEqualToThreshold(string uploadedPackageId, string packageId);
}
}
30 changes: 29 additions & 1 deletion src/NuGetGallery/App_Start/DefaultDependenciesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
using NuGetGallery.Infrastructure.Mail;
using NuGetGallery.Infrastructure.Search;
using NuGetGallery.Infrastructure.Search.Correlation;
using NuGetGallery.Login;
using NuGetGallery.Security;
using NuGetGallery.Services;
using Role = NuGet.Services.Entities.Role;
Expand Down Expand Up @@ -407,6 +406,8 @@ protected override void Load(ContainerBuilder builder)
.AsSelf()
.As<ICertificateService>()
.InstancePerLifetimeScope();

RegisterTyposquattingServiceHelper(builder, loggerFactory);

builder.RegisterType<TyposquattingService>()
.AsSelf()
Expand Down Expand Up @@ -1587,5 +1588,32 @@ private static void RegisterCookieComplianceService(ConfigurationService configu

CookieComplianceService.Initialize(service ?? new NullCookieComplianceService(), logger);
}

private static void RegisterTyposquattingServiceHelper(ContainerBuilder builder, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger(nameof(ITyposquattingServiceHelper));

builder.Register(c =>
{
var typosquattingService = GetAddInServices<ITyposquattingServiceHelper>(sp =>
{
sp.ComposeExportedValue<ILogger>(logger);
}).FirstOrDefault();

if (typosquattingService == null)
{
typosquattingService = new ExactMatchTyposquattingServiceHelper();
logger.LogInformation("No typosquatting service helper was found, using ExactMatchTyposquattingServiceHelper instead.");
}
else
{
logger.LogInformation("ITyposquattingServiceHelper found.");
}

return typosquattingService;
})
.As<ITyposquattingServiceHelper>()
.SingleInstance();
}
}
}
3 changes: 1 addition & 2 deletions src/NuGetGallery/NuGetGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@
<Compile Include="RequestModels\DeletePackagesApiRequest.cs" />
<Compile Include="RequestModels\UpdateListedRequest.cs" />
<Compile Include="Services\MissingLicenseValidationMessageV2.cs" />
<Compile Include="Services\NullTyposquattingService.cs" />
<Compile Include="Services\UploadPackageMissingReadme.cs" />
<Compile Include="Services\MissingLicenseValidationMessage.cs" />
<Compile Include="Services\UploadPackageIdNamespaceConflict.cs" />
Expand Down Expand Up @@ -650,8 +651,6 @@
<Compile Include="Services\PackageValidationResultType.cs" />
<Compile Include="Services\ReadMeService.cs" />
<Compile Include="Services\TyposquattingService.cs" />
<Compile Include="Services\TyposquattingDistanceCalculation.cs" />
<Compile Include="Services\TyposquattingStringNormalization.cs" />
<Compile Include="Services\UpdateDeprecationError.cs" />
<Compile Include="Services\ValidationService.cs" />
<Compile Include="Strings.Designer.cs">
Expand Down
13 changes: 13 additions & 0 deletions src/NuGetGallery/Services/NullTyposquattingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace NuGetGallery.Services
{
public class ExactMatchTyposquattingServiceHelper : ITyposquattingServiceHelper
{
public bool IsDistanceLessThanOrEqualToThreshold(string uploadedPackageId, string packageId)
{
return uploadedPackageId.ToLowerInvariant() == packageId.ToLowerInvariant();
}
}
}
213 changes: 0 additions & 213 deletions src/NuGetGallery/Services/TyposquattingDistanceCalculation.cs

This file was deleted.

Loading

0 comments on commit c1115f8

Please sign in to comment.