Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract typosquatting #9767

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
{
/// <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);
}
}
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.LogInformation("No typosquatting service helper was found, using NullTyposquattingServiceHelper instead.");
}
else
{
logger.LogInformation("ITyposquattingServiceHelper found.");
}

return typosquattingService;
})
.AsSelf()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to have that type resolved as self? Wouldn't .As<ITyposquattingServiceHelper>() be enough?

Copy link
Contributor Author

@erdembayar erdembayar Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh this method is new to me, I just copied from line 1570 above assuming how it's done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd guess, there is no need for it to be there as well :)

Copy link
Contributor Author

@erdembayar erdembayar Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed here, I think removing other instances are out of scope for this PR. If you wish I can create a tracking issue for those.

.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
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 NullTyposquattingServiceHelper : ITyposquattingServiceHelper
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
{
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