Skip to content

Commit

Permalink
Merge branch 'dev' into dev-nkolev92-vulnerabilityAPICleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolev92 authored Aug 18, 2023
2 parents d25296b + e873b49 commit 2c08b40
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 119 deletions.
2 changes: 0 additions & 2 deletions src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<GetReferenceNearestTargetFrameworkTaskSupportsTargetPlatformParameter>true</GetReferenceNearestTargetFrameworkTaskSupportsTargetPlatformParameter>
<!-- Flag if the Central package file is enabled -->
<_CentralPackageVersionsEnabled Condition="'$(ManagePackageVersionsCentrally)' == 'true' AND '$(CentralPackageVersionsFileImported)' == 'true'">true</_CentralPackageVersionsEnabled>
<!-- NuGetAudit is enabled by default when using the .NET 8 SDK or above. Customers should set 'true', so we'll use 'default' to signal implicit opt-in -->
<NuGetAudit Condition=" '$(NuGetAudit)' == '' AND $([MSBuild]::VersionGreaterThanOrEquals($([MSBuild]::ValueOrDefault('$(NETCoreSdkVersion)', '0.0')), '8.0'))">default</NuGetAudit>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,12 @@ await _logger.LogAsync(RestoreLogMessage.CreateWarning(NuGetLogCode.NU1803,
});
}

AuditUtility.EnabledValue enableAudit = AuditUtility.ParseEnableValue(_request.Project.RestoreMetadata?.RestoreAuditProperties?.EnableAudit);
AuditUtility.EnabledValue enableAudit = AuditUtility.ParseEnableValue(
_request.Project.RestoreMetadata?.RestoreAuditProperties?.EnableAudit,
_request.Project.FilePath,
_logger);
telemetry.TelemetryEvent[AuditEnabled] = AuditUtility.GetString(enableAudit);
if (enableAudit == AuditUtility.EnabledValue.ImplicitOptIn || enableAudit == AuditUtility.EnabledValue.ExplicitOptIn)
if (enableAudit != AuditUtility.EnabledValue.ExplicitOptOut)
{
await PerformAuditAsync(enableAudit, graphs, telemetry, token);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace NuGet.Commands.Restore.Utility
internal class AuditUtility
{
private readonly EnabledValue _auditEnabled;
private readonly ProjectModel.RestoreAuditProperties _restoreAuditProperties;
private readonly ProjectModel.RestoreAuditProperties? _restoreAuditProperties;
private readonly string _projectFullPath;
private readonly IEnumerable<RestoreTargetGraph> _targetGraphs;
private readonly IReadOnlyList<IVulnerabilityInformationProvider> _vulnerabilityInfoProviders;
Expand All @@ -49,7 +49,7 @@ internal class AuditUtility

public AuditUtility(
EnabledValue auditEnabled,
ProjectModel.RestoreAuditProperties restoreAuditProperties,
ProjectModel.RestoreAuditProperties? restoreAuditProperties,
string projectFullPath,
IEnumerable<RestoreTargetGraph> graphs,
IReadOnlyList<IVulnerabilityInformationProvider> vulnerabilityInformationProviders,
Expand Down Expand Up @@ -366,7 +366,7 @@ private static (string severityLabel, NuGetLogCode code) GetSeverityLabelAndCode

private PackageVulnerabilitySeverity ParseAuditLevel()
{
string? auditLevel = _restoreAuditProperties.AuditLevel?.Trim();
string? auditLevel = _restoreAuditProperties?.AuditLevel?.Trim();

if (auditLevel == null)
{
Expand Down Expand Up @@ -402,7 +402,7 @@ internal enum NuGetAuditMode { Unknown, Direct, All }
// Enum parsing and ToString are a magnitude of times slower than a naive implementation.
private NuGetAuditMode ParseAuditMode()
{
string? auditMode = _restoreAuditProperties.AuditMode?.Trim();
string? auditMode = _restoreAuditProperties?.AuditMode?.Trim();

if (auditMode == null)
{
Expand All @@ -426,16 +426,16 @@ private NuGetAuditMode ParseAuditMode()

internal enum EnabledValue
{
Undefined,
Invalid,
ImplicitOptIn,
ExplicitOptIn,
ExplicitOptOut
}

// Enum parsing and ToString are a magnitude of times slower than a naive implementation.
public static EnabledValue ParseEnableValue(string value)
public static EnabledValue ParseEnableValue(string? value, string projectFullPath, ILogger logger)
{
if (string.Equals(value, "default", StringComparison.OrdinalIgnoreCase))
if (string.IsNullOrEmpty(value) || string.Equals(value, "default", StringComparison.OrdinalIgnoreCase))
{
return EnabledValue.ImplicitOptIn;
}
Expand All @@ -449,15 +449,20 @@ public static EnabledValue ParseEnableValue(string value)
{
return EnabledValue.ExplicitOptOut;
}
return EnabledValue.Undefined;

string messageText = string.Format(Strings.Error_InvalidNuGetAuditValue, value, "true, false");
RestoreLogMessage message = RestoreLogMessage.CreateError(NuGetLogCode.NU1014, messageText);
message.ProjectPath = projectFullPath;
logger.Log(message);
return EnabledValue.Invalid;
}

// Enum parsing and ToString are a magnitude of times slower than a naive implementation.
internal static string GetString(EnabledValue enableAudit)
{
return enableAudit switch
{
EnabledValue.Undefined => nameof(EnabledValue.Undefined),
EnabledValue.Invalid => nameof(EnabledValue.Invalid),
EnabledValue.ExplicitOptIn => nameof(EnabledValue.ExplicitOptIn),
EnabledValue.ExplicitOptOut => nameof(EnabledValue.ExplicitOptOut),
EnabledValue.ImplicitOptIn => nameof(EnabledValue.ImplicitOptIn),
Expand Down
13 changes: 11 additions & 2 deletions src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

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

10 changes: 8 additions & 2 deletions src/NuGet.Core/NuGet.Commands/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ Non-HTTPS access will be removed in a future version. Consider migrating to 'HTT
{3} - URL for more info on the known vulnerability</comment>
</data>
<data name="Error_InvalidNuGetAuditLevelValue" xml:space="preserve">
<value>Invalid NuGetAuditLevel value '{0}'. Expected values: {1}</value>
<value>Invalid NuGetAuditLevel value '{0}'. Valid values: {1}</value>
<comment>Don't translate 'NuGetAuditLevel'
{0} - is the value from the customer's project file
{1} is the list of valid values "low, moderate, high, critical" (these should not be translated either)</comment>
Expand All @@ -1083,7 +1083,7 @@ Non-HTTPS access will be removed in a future version. Consider migrating to 'HTT
<comment>{0} is the value supplied </comment>
</data>
<data name="Error_InvalidNuGetAuditModeValue" xml:space="preserve">
<value>Invalid NuGetAuditMode value '{0}'. Expected values: {1}</value>
<value>Invalid NuGetAuditMode value '{0}'. Valid values: {1}</value>
<comment>Don't translate 'NuGetAuditMode'
{0} - is the value from the customer's project file
{1} is the list of valid values "direct, all"</comment>
Expand All @@ -1092,4 +1092,10 @@ Non-HTTPS access will be removed in a future version. Consider migrating to 'HTT
<value>NuGetAudit is enabled, but no package sources contain known vulnerability data.</value>
<comment>Do not translate NuGetAudit</comment>
</data>
<data name="Error_InvalidNuGetAuditValue" xml:space="preserve">
<value>Invalid NuGetAudit value '{0}'. Valid values: {1}</value>
<comment>Don't translate 'NuGetAudit'
{0} - is the value from the customer's project file
{1} is the list of valid values "true, false"</comment>
</data>
</root>
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.Common/Errors/NuGetLogCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public enum NuGetLogCode
NU1013 = 1013,

/// <summary>
/// NuGetAuditLevel input errors
/// NuGetAudit* MSBuild property input errors
/// </summary>
NU1014 = 1014,

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,8 @@ public async Task RestoreCommand_RestoreFloatingVersionWithIgnoreFailingLocalSou
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down Expand Up @@ -2184,7 +2185,8 @@ public async Task RestoreCommand_RestoreFloatingVersionWithIgnoreFailingHttpSour
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down Expand Up @@ -2402,7 +2404,8 @@ public async Task RestoreCommand_RestoreNonExistingWithIgnoreFailingLocalSourceA
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down Expand Up @@ -2451,7 +2454,8 @@ public async Task RestoreCommand_RestoreNonExistingWithIgnoreFailingHttpSourceAs
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down Expand Up @@ -2500,7 +2504,8 @@ public async Task RestoreCommand_RestoreNonExistingWithIgnoreFailingV3HttpSource
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down Expand Up @@ -2549,7 +2554,8 @@ public async Task RestoreCommand_RestoreInexactWithIgnoreFailingLocalSourceAsync
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down Expand Up @@ -2598,7 +2604,8 @@ public async Task RestoreCommand_RestoreInexactWithIgnoreFailingHttpSourceAsync(
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down Expand Up @@ -2647,7 +2654,8 @@ public async Task RestoreCommand_RestoreInexactWithIgnoreFailingV3HttpSourceAsyn
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
using (var context = new SourceCacheContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public async Task UWPRestore_BlankUWPAppWithExcludes()
}");

var specPath = Path.Combine(projectDir, "TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath)
.EnsureProjectJsonRestoreMetadata();

var logger = new TestLogger();
var clientPolicyContext = ClientPolicyContext.GetClientPolicy(NullSettings.Instance, logger);
Expand Down
Loading

0 comments on commit 2c08b40

Please sign in to comment.