Skip to content

Commit e7283a5

Browse files
authored
Typo-squatting: log the original uploaded package Id rather than normalized one (#6482)
* log the original uploaded package Id rather than normalized one * Add unit test
1 parent c4a6498 commit e7283a5

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/NuGetGallery/Services/TyposquattingService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ public bool IsUploadedPackageIdTyposquatting(string uploadedPackageId, User uplo
6767

6868
var algorithmProcessingStopwatch = Stopwatch.StartNew();
6969
var threshold = GetThreshold(uploadedPackageId);
70-
uploadedPackageId = TyposquattingStringNormalization.NormalizeString(uploadedPackageId);
70+
var normalizedUploadedPackageId = TyposquattingStringNormalization.NormalizeString(uploadedPackageId);
7171

7272
var collisionIds = new ConcurrentBag<string>();
7373
Parallel.ForEach(packagesCheckList, (packageId, loopState) =>
7474
{
7575
string normalizedPackageId = TyposquattingStringNormalization.NormalizeString(packageId);
76-
if (TyposquattingDistanceCalculation.IsDistanceLessThanThreshold(uploadedPackageId, normalizedPackageId, threshold))
76+
if (TyposquattingDistanceCalculation.IsDistanceLessThanThreshold(normalizedUploadedPackageId, normalizedPackageId, threshold))
7777
{
7878
collisionIds.Add(packageId);
7979
}

tests/NuGetGallery.Facts/Services/TyposquattingServiceFacts.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,38 @@ public void CheckIsTyposquattingBlockUserNotEnabled()
318318
Assert.Equal("microsoft_netframework_v1", typosquattingCheckCollisionIds[0]);
319319
}
320320

321+
[Fact]
322+
public void CheckTelemetryServiceLogOriginalUploadedPackageId()
323+
{
324+
// Arrange
325+
var uploadedPackageId = "microsoft_netframework.v1";
326+
var newService = new TyposquattingService(_contentObjectService.Object, _packageService.Object, _reservedNamespaceService.Object, _telemetryService.Object);
327+
328+
// Act
329+
var typosquattingCheckResult = newService.IsUploadedPackageIdTyposquatting(uploadedPackageId, _uploadedPackageOwner, out List<string> typosquattingCheckCollisionIds);
330+
331+
// Assert
332+
_telemetryService.Verify(
333+
x => x.TrackMetricForTyposquattingChecklistRetrievalTime(uploadedPackageId, It.IsAny<TimeSpan>()),
334+
Times.Once);
335+
336+
_telemetryService.Verify(
337+
x => x.TrackMetricForTyposquattingAlgorithmProcessingTime(uploadedPackageId, It.IsAny<TimeSpan>()),
338+
Times.Once);
339+
340+
_telemetryService.Verify(
341+
x => x.TrackMetricForTyposquattingCheckResultAndTotalTime(
342+
uploadedPackageId,
343+
It.IsAny<TimeSpan>(),
344+
It.IsAny<bool>(),
345+
It.IsAny<List<string>>(),
346+
It.IsAny<int>()),
347+
Times.Once);
348+
349+
_telemetryService.Verify(
350+
x => x.TrackMetricForTyposquattingOwnersCheckTime(uploadedPackageId, It.IsAny<TimeSpan>()),
351+
Times.Once);
352+
}
321353

322354
[Theory]
323355
[InlineData("Microsoft_NetFramework_v1", "Microsoft.NetFramework.v1", 0)]

0 commit comments

Comments
 (0)