Skip to content

Commit d8af2ab

Browse files
authored
Add --vulnerable to dotnet package update (#6768)
1 parent 388b7f5 commit d8af2ab

31 files changed

Lines changed: 1101 additions & 136 deletions

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Update/IPackageUpdateIO.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System.Collections.Generic;
57
using System.Threading;
68
using System.Threading.Tasks;
@@ -22,7 +24,7 @@ internal interface IPackageUpdateIO
2224
/// </summary>
2325
/// <param name="project">The project or solution requested.</param>
2426
/// <returns>A DependencyGraphSpec representing the restore inputs.</returns>
25-
DependencyGraphSpec GetDependencyGraphSpec(string project);
27+
DependencyGraphSpec? GetDependencyGraphSpec(string project);
2628

2729
/// <summary>
2830
/// Loads settings from the specified project directory.
@@ -74,5 +76,7 @@ internal abstract class RestoreResult
7476
/// Was the preview restore operation successful
7577
/// </summary>
7678
public abstract bool Success { get; }
79+
80+
public abstract LockFile? AssetsFile { get; }
7781
}
7882
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Update/IVersionChooser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
#nullable enable
55

6+
using System.Collections.Generic;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using NuGet.Common;
10+
using NuGet.Protocol.Model;
911
using NuGet.Versioning;
1012

1113
namespace NuGet.CommandLine.XPlat.Commands.Package.Update
@@ -16,5 +18,12 @@ internal interface IVersionChooser
1618
string packageId,
1719
ILogger logger,
1820
CancellationToken cancellationToken);
21+
22+
Task<NuGetVersion?> GetNonVulnerableAsync(
23+
string packageId,
24+
NuGetVersion minVersion,
25+
ILogger logger,
26+
IReadOnlyList<IReadOnlyDictionary<string, IReadOnlyList<PackageVulnerabilityInfo>>> knownVulnerabilities,
27+
CancellationToken cancellationToken);
1928
}
2029
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Update/Package.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
#nullable enable
55

6+
using System;
67
using System.Collections.Generic;
78
using System.CommandLine.Parsing;
9+
using System.Diagnostics.CodeAnalysis;
810
using NuGet.Versioning;
911

1012
namespace NuGet.CommandLine.XPlat.Commands.Package.Update
1113
{
12-
internal record Package
14+
internal record Package : IEqualityComparer<Package>
1315
{
1416
public required string Id { get; init; }
1517
public required VersionRange? VersionRange { get; init; }
@@ -59,5 +61,33 @@ internal static IReadOnlyList<Package> Parse(ArgumentResult result)
5961

6062
return packages;
6163
}
64+
65+
public bool Equals(Package? x, Package? y)
66+
{
67+
if (ReferenceEquals(x, y))
68+
{
69+
return true;
70+
}
71+
72+
if (x is null || y is null)
73+
{
74+
return false;
75+
}
76+
77+
if (!x.Id.Equals(y.Id, StringComparison.OrdinalIgnoreCase))
78+
{
79+
return false;
80+
}
81+
82+
return VersionRangeComparer.Default.Equals(x.VersionRange, y.VersionRange);
83+
}
84+
85+
public int GetHashCode([DisallowNull] Package obj)
86+
{
87+
HashCode hash = new HashCode();
88+
hash.Add(obj.Id, StringComparer.OrdinalIgnoreCase);
89+
hash.Add(obj.VersionRange);
90+
return hash.ToHashCode();
91+
}
6292
}
6393
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Update/PackageUpdateArgs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace NuGet.CommandLine.XPlat.Commands.Package.Update
1010
{
11-
internal class PackageUpdateArgs
11+
internal record PackageUpdateArgs
1212
{
1313
public required string Project { get; init; }
1414

@@ -17,5 +17,7 @@ internal class PackageUpdateArgs
1717
public required bool Interactive { get; init; }
1818

1919
public required LogLevel LogLevel { get; init; }
20+
21+
public required bool Vulnerable { get; init; }
2022
}
2123
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Update/PackageUpdateCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ internal static void Register(Command packageCommand, Option<bool> interactiveOp
3636
projectOption.Description = Strings.PackageUpdateCommand_ProjectOptionDescription;
3737
command.Options.Add(projectOption);
3838

39+
var vulnerableOption = new Option<bool>("--vulnerable");
40+
vulnerableOption.Description = Strings.PackageUpdateCommand_VulnerableOptionDescription;
41+
command.Options.Add(vulnerableOption);
42+
3943
command.Options.Add(interactiveOption);
4044

4145
var verbosityOption = CommonOptions.GetVerbosityOption();
@@ -49,13 +53,15 @@ internal static void Register(Command packageCommand, Option<bool> interactiveOp
4953
bool interactive = args.GetValue(interactiveOption);
5054
VerbosityEnum verbosity = args.GetValue(verbosityOption) ?? VerbosityEnum.normal;
5155
LogLevel logLevel = verbosity.ToLogLevel();
56+
bool vulnerable = args.GetValue(vulnerableOption);
5257

5358
var commandArgs = new PackageUpdateArgs
5459
{
5560
Project = project?.FullName ?? Environment.CurrentDirectory,
5661
Packages = packages,
5762
Interactive = interactive,
5863
LogLevel = logLevel,
64+
Vulnerable = vulnerable,
5965
};
6066

6167
return await action(commandArgs, cancellationToken);

0 commit comments

Comments
 (0)