Skip to content

Commit 37b5444

Browse files
committed
Added RunTwice Test and Fixed #153
1 parent 605361d commit 37b5444

12 files changed

+81
-29
lines changed

Src/NuGetDefense.Lib/Scanner.cs

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using System.Text;
@@ -55,16 +56,19 @@ public class Scanner
5556
public int Scan(ScanOptions options)
5657
{
5758
int exitCode;
59+
NumberOfVulnerabilities = 0;
60+
_projects = [];
5861
try
5962
{
6063
LoadSettings(options);
6164
ConfigureLogging();
6265
ScanVulnerabilities(options);
6366
exitCode = _settings.WarnOnly ? 0 : NumberOfVulnerabilities;
6467
}
65-
catch (Exception)
68+
catch (Exception e)
6669
{
6770
exitCode = -1;
71+
Debug.WriteLine(e);
6872
}
6973

7074
return exitCode;
@@ -187,11 +191,20 @@ private void ScanVulnerabilities(ScanOptions options)
187191
var nonSensitivePackageIDs = nonSensitivePackages.SelectMany(p => p.Value).ToArray();
188192
if (_settings.OssIndex.Enabled)
189193
{
190-
const string OssIndexSourceID = "OSSIndex";
191-
var uncachedPkgs = options.Cache.GetUncachedPackages(nonSensitivePackageIDs, TimeSpan.FromDays(1), OssIndexSourceID, out var cachedPackages);
192-
194+
const string ossIndexSourceId = "OSSIndex";
195+
var uncachedPkgs = options.Cache.GetUncachedPackages(nonSensitivePackageIDs, TimeSpan.FromDays(1), ossIndexSourceId, out var cachedPackages);
196+
var modUncached = uncachedPkgs.Count % 128;
197+
if (modUncached > 0)
198+
{
199+
for (var i = cachedPackages.Length - 1; i >= cachedPackages.Length - modUncached; i--)
200+
{
201+
uncachedPkgs.Add(cachedPackages[i]);
202+
}
203+
204+
cachedPackages = cachedPackages[..^modUncached];
205+
}
193206
// Round out the calls to have a full set of packages each to refresh oldest cached packages
194-
if (uncachedPkgs.Count > 0) uncachedPkgs.AddRange(cachedPackages.Take(128 - uncachedPkgs.Count % 128));
207+
if (uncachedPkgs.Count > 0) uncachedPkgs.AddRange(cachedPackages.Take(uncachedPkgs.Count % 128));
195208

196209
Log.Logger.Verbose("Checking with OSSIndex for Vulnerabilities");
197210
vulnDict =
@@ -201,10 +214,10 @@ private void ScanVulnerabilities(ScanOptions options)
201214
// If we failed to update the OSS Index data don't clear out old cached data as that will
202215
// increase the number of requests next time, increasing the liklihood of further
203216
// TooManyRequeusts responses.
204-
options.Cache.UpdateCache(vulnDict, uncachedPkgs, OssIndexSourceID);
217+
options.Cache.UpdateCache(vulnDict, uncachedPkgs, ossIndexSourceId);
205218

206219
// Skipping the packages we refreshed
207-
options.Cache.GetPackagesCachedVulnerabilitiesForSource(cachedPackages.Skip(128 - uncachedPkgs.Count % 128), OssIndexSourceID, ref vulnDict);
220+
options.Cache.GetPackagesCachedVulnerabilitiesForSource(cachedPackages.Skip(uncachedPkgs.Count % 128), ossIndexSourceId, ref vulnDict);
208221
}
209222

210223
if (_settings.GitHubAdvisoryDatabase.Enabled)
@@ -218,20 +231,29 @@ private void ScanVulnerabilities(ScanOptions options)
218231
}
219232
else
220233
{
221-
const string GitHubAdvisoryDatabaseSourceId = "GitHubSecurityAdvisoryDatabase";
222-
var uncachedPkgs = options.Cache.GetUncachedPackages(nonSensitivePackageIDs, TimeSpan.FromDays(1), GitHubAdvisoryDatabaseSourceId, out var cachedPackages);
223-
234+
const string gitHubAdvisoryDatabaseSourceId = "GitHubSecurityAdvisoryDatabase";
235+
var uncachedPkgs = options.Cache.GetUncachedPackages(nonSensitivePackageIDs, TimeSpan.FromDays(1), gitHubAdvisoryDatabaseSourceId, out var cachedPackages);
236+
var modUncached = uncachedPkgs.Count % 128;
237+
if (modUncached > 0)
238+
{
239+
for (var i = cachedPackages.Length - 1; i >= cachedPackages.Length - modUncached; i--)
240+
{
241+
uncachedPkgs.Add(cachedPackages[i]);
242+
}
243+
244+
cachedPackages = cachedPackages[..^modUncached];
245+
}
224246
Log.Logger.Verbose("Checking the GitHub Security Advisory Database for Vulnerabilities");
225247
var ghsaVulnDict =
226248
new GitHubAdvisoryDatabase.Scanner(_nuGetFile, _settings.GitHubAdvisoryDatabase.ApiToken, _settings.GitHubAdvisoryDatabase.BreakIfCannotRun)
227249
.GetVulnerabilitiesForPackages(uncachedPkgs.ToArray());
228-
options.Cache.UpdateCache(ghsaVulnDict, uncachedPkgs, GitHubAdvisoryDatabaseSourceId);
250+
options.Cache.UpdateCache(ghsaVulnDict, uncachedPkgs, gitHubAdvisoryDatabaseSourceId);
229251

230252
if (vulnDict == null)
231253
vulnDict = ghsaVulnDict;
232254
else
233255
MergeVulnDict(ref vulnDict, ref ghsaVulnDict);
234-
options.Cache.GetPackagesCachedVulnerabilitiesForSource(cachedPackages, GitHubAdvisoryDatabaseSourceId, ref vulnDict);
256+
options.Cache.GetPackagesCachedVulnerabilitiesForSource(cachedPackages, gitHubAdvisoryDatabaseSourceId, ref vulnDict);
235257
}
236258
}
237259

Src/NuGetDefense.Lib/VulnerabilityReporter.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public void BuildVulnerabilityReport(
3535
VulnerabilitiesCount = vulnerabilityDictionary.Sum(x => x.Value.Count),
3636
Packages = distinctPackages.OrderBy(x => x.Id).ThenBy(x => x.Version)
3737
.Where(p => p.LineNumber != null
38-
&& vulnerabilityDictionary.ContainsKey(p.PackageUrl.ToLower())
39-
&& vulnerabilityDictionary[p.PackageUrl.ToLower()].Any()
38+
&& vulnerabilityDictionary.ContainsKey(p.PackageUrl)
39+
&& vulnerabilityDictionary[p.PackageUrl].Any()
4040
)
4141
.Select(p => new VulnerableNuGetPackage
4242
{
4343
Id = p.Id,
4444
Version = p.Version,
45-
Vulnerabilities = vulnerabilityDictionary[p.PackageUrl.ToLower()].Select(
45+
Vulnerabilities = vulnerabilityDictionary[p.PackageUrl].Select(
4646
v => new ReportedVulnerability
4747
{
4848
Description = v.Value.Description,
@@ -64,9 +64,9 @@ public void BuildVulnerabilityTextReport(Dictionary<string, Dictionary<string, V
6464
var logBuilder = new StringBuilder(VulnerabilityTextReport);
6565
var nuGetPackages = pkgs as NuGetPackage[] ?? pkgs.ToArray();
6666
logBuilder.AppendLine($"{vulnerabilityDictionary.Sum(ve => ve.Value.Count)} vulnerabilities found in {nuGetPackages.Count()} packages for {nuGetFile}.");
67-
foreach (var pkg in nuGetPackages.Where(p => vulnerabilityDictionary.ContainsKey(p.PackageUrl.ToLower())))
67+
foreach (var pkg in nuGetPackages.Where(p => vulnerabilityDictionary.ContainsKey(p.PackageUrl)))
6868
{
69-
var vulnerabilities = vulnerabilityDictionary[pkg.PackageUrl.ToLower()];
69+
var vulnerabilities = vulnerabilityDictionary[pkg.PackageUrl];
7070

7171
logBuilder.AppendLine("*************************************");
7272
// TODO: Dependencies will need to be listed by package url when this is used.

Src/NuGetDefense/NuGetDefense.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464

6565
<ItemGroup>
6666
<None Remove="Tests\**"/>
67-
<None Include="TestFiles\test.csproj"/>
6867
</ItemGroup>
6968

7069
<ItemGroup>

Src/NuGetDefenseTests/GlobalUsings.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Global using directives
2+
3+
global using NuGetDefense;
4+
global using NuGetDefense.Core;
5+
global using Xunit;
6+
global using Xunit.Abstractions;
7+
global using static NuGetDefense.UtilityMethods;

Src/NuGetDefenseTests/IssueAttribute.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace NuGetDefenseTests
84
{

Src/NuGetDefenseTests/NuGetDefenseTests.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@
3232
<ProjectReference Include="..\NuGetDefense\NuGetDefense.csproj" />
3333
</ItemGroup>
3434

35+
<ItemGroup>
36+
<Content Include="TestFiles\*.*">
37+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
38+
</Content>
39+
</ItemGroup>
3540
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=testfiles/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

Src/NuGetDefenseTests/SqliteCacheTests.cs

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
using System.IO;
44
using System.Linq;
55
using Microsoft.Data.Sqlite;
6-
using NuGetDefense;
7-
using NuGetDefense.Core;
8-
using Xunit;
96

107
namespace NuGetDefenseTests;
118

Src/NuGetDefenseTests/VulnerabilityReportsTest.cs

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
using System.Collections.Generic;
2-
using NuGetDefense;
3-
using NuGetDefense.Core;
4-
using Xunit;
5-
using static NuGetDefense.UtilityMethods;
2+
using System.IO;
63

74
namespace NuGetDefenseTests;
85

96
public class VulnerabilityReportsTest
107
{
8+
private readonly ITestOutputHelper _testOutputHelper;
9+
10+
public VulnerabilityReportsTest(ITestOutputHelper testOutputHelper)
11+
{
12+
_testOutputHelper = testOutputHelper;
13+
}
14+
1115
[Fact]
1216
public void ReportVulnerabilityWithNullReferences()
1317
{
@@ -64,4 +68,24 @@ public void IgnoreVulnerabilitiesForPackage()
6468
Assert.True(pkgs[0].Version == "1.0.1");
6569
Assert.True(pkgs[0].LineNumber == 1);
6670
}
71+
72+
[Fact]
73+
public void RunTwice()
74+
{
75+
var options = new ScanOptions()
76+
{
77+
ProjectFile = new FileInfo("./TestFiles/test.csproj"),
78+
CheckTransitiveDependencies = true,
79+
WarnOnly = false,
80+
};
81+
var scanner = new Scanner();
82+
var retVal = scanner.Scan(options);
83+
Assert.Equal(4, retVal);
84+
_testOutputHelper.WriteLine($"depcheck returned {retVal}");
85+
86+
retVal = scanner.Scan(options);
87+
Assert.Equal(4, retVal);
88+
}
89+
90+
//{"pkg:nuget/[email protected]":{"CVE-2019-11358":{},"CVE-2020-11023":{},"CVE-2020-23064":{}},"pkg:nuget/[email protected]":{"CVE-2021-43306":{}}}
6791
}

0 commit comments

Comments
 (0)