Skip to content

Commit

Permalink
Dotnet list package --vulnerable uses AuditSources (#6237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigusu-Allehu authored Feb 7, 2025
1 parent b248a36 commit 38f10f6
Show file tree
Hide file tree
Showing 20 changed files with 806 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class ListPackageArgs
public bool HighestPatch { get; }
public bool HighestMinor { get; }
public CancellationToken CancellationToken { get; }
public IReadOnlyList<PackageSource> AuditSources { get; }

/// <summary>
/// A constructor for the arguments of list package
Expand All @@ -41,6 +42,7 @@ internal class ListPackageArgs
/// <param name="prerelease"> Bool for --include-prerelease present </param>
/// <param name="highestPatch"> Bool for --highest-patch present </param>
/// <param name="highestMinor"> Bool for --highest-minor present </param>
/// <param name="auditSources"> A list of sources for performing vulnerability auditing</param>
/// <param name="logger"></param>
/// <param name="cancellationToken"></param>
public ListPackageArgs(
Expand All @@ -53,6 +55,7 @@ public ListPackageArgs(
bool prerelease,
bool highestPatch,
bool highestMinor,
IReadOnlyList<PackageSource> auditSources,
ILogger logger,
CancellationToken cancellationToken)
{
Expand All @@ -65,6 +68,7 @@ public ListPackageArgs(
Prerelease = prerelease;
HighestPatch = highestPatch;
HighestMinor = highestMinor;
AuditSources = auditSources;
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
CancellationToken = cancellationToken;
ArgumentText = GetReportParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static void Register(
isVulnerable: vulnerableReport.HasValue());

IReportRenderer reportRenderer = GetOutputType(outputFormat.Value(), outputVersionOption: outputVersion.Value());

var provider = new PackageSourceProvider(settings);
var packageRefArgs = new ListPackageArgs(
path.Value,
packageSources,
Expand All @@ -140,6 +140,7 @@ public static void Register(
prerelease.HasValue(),
highestPatch.HasValue(),
highestMinor.HasValue(),
provider.LoadAuditSources(),
logger,
CancellationToken.None);

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,18 @@ private void WriteToConsole(ListPackageReportModel listPackageReportModel)
return;
}

WriteSources(_consoleOut, listPackageReportModel.ListPackageArgs);
if (listPackageReportModel.ListPackageArgs.ReportType == ReportType.Vulnerable && listPackageReportModel.AuditSourcesUsed.Count > 0)
{
_consoleOut.WriteLine();
_consoleOut.WriteLine(Strings.ListPkg_SourcesUsedDescription);
PrintSources(_consoleOut, listPackageReportModel.AuditSourcesUsed);
_consoleOut.WriteLine();
}
else
{
WriteSources(_consoleOut, listPackageReportModel.ListPackageArgs);
}

WriteProjects(_consoleOut, _consoleError, listPackageReportModel.Projects, listPackageReportModel.ListPackageArgs);

// Print a legend message for auto-reference markers used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void WriteJson(JsonWriter writer, ListPackageReportModel listPackageRepo
WriteProblems(writer, _problems);
}

WriteSources(writer, listPackageReportModel.ListPackageArgs);
WriteSources(writer, listPackageReportModel);
WriteProjects(writer, listPackageReportModel.Projects, listPackageReportModel.ListPackageArgs);
writer.WriteEndObject();
}
Expand Down Expand Up @@ -140,9 +140,9 @@ private static void WriteProblems(JsonWriter writer, IEnumerable<ReportProblem>
writer.WriteEndArray();
}

private static void WriteSources(JsonWriter writer, ListPackageArgs listPackageArgs)
private static void WriteSources(JsonWriter writer, ListPackageReportModel listPackageReportModel)
{
if (listPackageArgs.ReportType == ReportType.Default)
if (listPackageReportModel.ListPackageArgs.ReportType == ReportType.Default)
{
// generic list is offline.
return;
Expand All @@ -151,9 +151,19 @@ private static void WriteSources(JsonWriter writer, ListPackageArgs listPackageA
writer.WritePropertyName(SourcesProperty);
writer.WriteStartArray();

foreach (PackageSource packageSource in listPackageArgs.PackageSources)
if (listPackageReportModel.ListPackageArgs.ReportType == ReportType.Vulnerable && listPackageReportModel.AuditSourcesUsed.Count > 0)
{
writer.WriteValue(PathUtility.GetPathWithForwardSlashes(packageSource.Source));
foreach (PackageSource packageSource in listPackageReportModel.AuditSourcesUsed)
{
writer.WriteValue(PathUtility.GetPathWithForwardSlashes(packageSource.Source));
}
}
else
{
foreach (PackageSource packageSource in listPackageReportModel.ListPackageArgs.PackageSources)
{
writer.WriteValue(PathUtility.GetPathWithForwardSlashes(packageSource.Source));
}
}

writer.WriteEndArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using NuGet.Configuration;

namespace NuGet.CommandLine.XPlat.ListPackage
{
Expand All @@ -13,6 +14,7 @@ internal class ListPackageReportModel
internal ListPackageArgs ListPackageArgs { get; }
internal List<ListPackageProjectModel> Projects { get; } = new();
internal MSBuildAPIUtility MSBuildAPIUtility { get; }
internal HashSet<PackageSource> AuditSourcesUsed { get; set; } = new HashSet<PackageSource>();

private ListPackageReportModel()
{ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,14 @@ public ListReportPackage(string packageId, string requestedVersion, string resol
requestedVersion: requestedVersion,
autoReference: false)
{ }

public ListReportPackage(string packageId, string version, List<PackageVulnerabilityMetadata> vulnerabilities)
: this(
packageId: packageId,
requestedVersion: version,
resolvedVersion: null,
latestVersion: null,
vulnerabilities: vulnerabilities.Count == 0 ? null : vulnerabilities)
{ }
}
}
9 changes: 9 additions & 0 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -978,4 +978,8 @@ Non-HTTPS access will be removed in a future version. Consider migrating to 'HTT
<value>Project '{0}' does not have MSBuild property ProjectAssetsFile defined. This may indicate that this project does not support NuGet PackageReference, or that project customization has prevented the .NET SDK setting default values.</value>
<comment>{0} - Path to the project with the error</comment>
</data>
<data name="Warning_AuditSourceWithoutData" xml:space="preserve">
<value>Audit source '{0}' did not provide any vulnerability data.</value>
<comment>{0} is the source name</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ public class PackageVulnerabilityMetadata

[JsonProperty(PropertyName = JsonProperties.Severity)]
public int Severity { get; internal set; }

public PackageVulnerabilityMetadata(Uri advisoryUrl, int severity)
{
AdvisoryUrl = advisoryUrl;
Severity = severity;
}

public PackageVulnerabilityMetadata()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#nullable enable
~NuGet.Protocol.PackageVulnerabilityMetadata.PackageVulnerabilityMetadata(System.Uri advisoryUrl, int severity) -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state) -> void
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#nullable enable
~NuGet.Protocol.PackageVulnerabilityMetadata.PackageVulnerabilityMetadata(System.Uri advisoryUrl, int severity) -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state) -> void
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#nullable enable
~NuGet.Protocol.PackageVulnerabilityMetadata.PackageVulnerabilityMetadata(System.Uri advisoryUrl, int severity) -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state) -> void
Loading

0 comments on commit 38f10f6

Please sign in to comment.