Skip to content

Commit

Permalink
Add new interface ITyposquattingServiceHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
erdembayar committed Dec 27, 2023
1 parent 7736941 commit 3c28fba
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 561 deletions.
15 changes: 15 additions & 0 deletions src/NuGetGallery.Core/Services/ITyposquattingServiceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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
{
string NormalizeString(string packageId);
bool IsDistanceLessThanOrEqualToThreshold(string normalizedUploadedPackageId, string normalizedPackageId, int threshold);
int GetThreshold(string packageId);
}
}
31 changes: 30 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,33 @@ 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 NullTyposquattingServiceHelper();
logger.LogWarning("No typosquatting service was found, using NullTyposquattingServiceHelper instead.");
}
else
{
logger.LogWarning("MEF injected ITyposquattingServiceHelper found.");
}

return typosquattingService;
})
.AsSelf()
.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 @@ -350,6 +350,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 @@ -649,8 +650,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
23 changes: 23 additions & 0 deletions src/NuGetGallery/Services/NullTyposquattingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 NullTyposquattingServiceHelper : ITyposquattingServiceHelper
{
public int GetThreshold(string packageId)
{
return 0;
}

public bool IsDistanceLessThanOrEqualToThreshold(string normalizedUploadedPackageId, string normalizedPackageId, int threshold)
{
return normalizedUploadedPackageId == normalizedPackageId;
}

public string NormalizeString(string packageId)
{
return packageId.ToLowerInvariant();
}
}
}
213 changes: 0 additions & 213 deletions src/NuGetGallery/Services/TyposquattingDistanceCalculation.cs

This file was deleted.

Loading

0 comments on commit 3c28fba

Please sign in to comment.