Skip to content

Commit c259c43

Browse files
authored
fix: ignore low confidence packages during nuget scan (#177)
1 parent 9b9a7fc commit c259c43

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/Microsoft.ComponentDetection.Detectors/nuget/NuGetComponentDetector.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public class NuGetComponentDetector : FileComponentDetector
3131

3232
private readonly IList<string> repositoryPathKeyNames = new List<string> { "repositorypath", "globalpackagesfolder" };
3333

34+
private static readonly IEnumerable<string> LowConfidencePackages = new[] { "Newtonsoft.Json" };
35+
3436
protected override async Task OnFileFound(ProcessRequest processRequest, IDictionary<string, string> detectorArgs)
3537
{
3638
var stream = processRequest.ComponentStream;
3739
bool ignoreNugetConfig = detectorArgs.TryGetValue("NuGet.IncludeRepositoryPaths", out string includeRepositoryPathsValue) && includeRepositoryPathsValue.Equals(bool.FalseString, StringComparison.OrdinalIgnoreCase);
38-
40+
3941
if (NugetConfigFileName.Equals(stream.Pattern, StringComparison.OrdinalIgnoreCase))
4042
{
4143
await ProcessAdditionalDirectory(processRequest, ignoreNugetConfig);
@@ -50,7 +52,7 @@ private async Task ProcessAdditionalDirectory(ProcessRequest processRequest, boo
5052
{
5153
var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder;
5254
var stream = processRequest.ComponentStream;
53-
55+
5456
if (!ignoreNugetConfig)
5557
{
5658
var additionalPaths = GetRepositoryPathsFromNugetConfig(stream);
@@ -106,7 +108,7 @@ private async Task ProcessFile(ProcessRequest processRequest)
106108
string name = metadataNode["id"].InnerText;
107109
string version = metadataNode["version"].InnerText;
108110

109-
string[] authors = metadataNode["authors"]?.InnerText.Split(",").Select(author => author.Trim()).ToArray();
111+
string[] authors = metadataNode["authors"]?.InnerText.Split(",").Select(author => author.Trim()).ToArray();
110112

111113
if (!NuGetVersion.TryParse(version, out NuGetVersion parsedVer))
112114
{
@@ -116,7 +118,10 @@ private async Task ProcessFile(ProcessRequest processRequest)
116118
}
117119

118120
NuGetComponent component = new NuGetComponent(name, version, authors);
119-
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(component));
121+
if (!LowConfidencePackages.Contains(name, StringComparer.OrdinalIgnoreCase))
122+
{
123+
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(component));
124+
}
120125
}
121126
catch (Exception e)
122127
{

test/Microsoft.ComponentDetection.Detectors.Tests/NuGetComponentDetectorTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace Microsoft.ComponentDetection.Detectors.Tests
1717
{
18+
using FluentAssertions;
19+
1820
[TestClass]
1921
[TestCategory("Governance/All")]
2022
[TestCategory("Governance/ComponentDetection")]
@@ -207,6 +209,20 @@ public async Task TestNugetDetector_AdditionalDirectories()
207209
Assert.AreEqual(1, componentRecorder.GetDetectedComponents().Count());
208210
}
209211

212+
[TestMethod]
213+
public async Task TestNugetDetector_LowConfidencePackages()
214+
{
215+
var nupkg = await NugetTestUtilities.ZipNupkgComponent("Newtonsoft.Json.nupkg", NugetTestUtilities.GetValidNuspec("Newtonsoft.Json", "9.0.1", new []{ "JamesNK"}));
216+
217+
var (scanResult, componentRecorder) = await this.detectorTestUtility
218+
.WithFile("Newtonsoft.Json.nupkg", nupkg)
219+
.ExecuteDetector();
220+
221+
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
222+
componentRecorder.GetDetectedComponents().Should().BeEmpty()
223+
.And.HaveCount(0);
224+
}
225+
210226
private string CreateTemporaryDirectory()
211227
{
212228
string path;

0 commit comments

Comments
 (0)