-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add signature VerifySigning for dotnet tools #39268
Changes from 20 commits
ac94e8c
00331c9
2d5f551
4bca9df
060e172
5ade9d9
ab30839
df58e0b
89b822d
aaeccf3
66fbe72
3a22067
50378d1
87520e5
2b449fc
deabd72
a7b9ae4
196ab12
131cff5
9e4910b
24658d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using Microsoft.DotNet.Cli.Utils; | ||
using Microsoft.DotNet.ToolPackage; | ||
using Microsoft.DotNet.Tools; | ||
using Microsoft.Extensions.EnvironmentAbstractions; | ||
using Microsoft.TemplateEngine.Abstractions; | ||
using NuGet.Common; | ||
using NuGet.Configuration; | ||
using NuGet.Credentials; | ||
|
@@ -43,6 +40,7 @@ internal class NuGetPackageDownloader : INuGetPackageDownloader | |
|
||
private bool _verifySignatures; | ||
private VerbosityOptions _verbosityOptions; | ||
private readonly string _currentWorkingDirectory; | ||
|
||
public NuGetPackageDownloader( | ||
DirectoryPath packageInstallDir, | ||
|
@@ -54,8 +52,10 @@ public NuGetPackageDownloader( | |
Func<IEnumerable<Task>> timer = null, | ||
bool verifySignatures = false, | ||
bool shouldUsePackageSourceMapping = false, | ||
VerbosityOptions verbosityOptions = VerbosityOptions.normal) | ||
VerbosityOptions verbosityOptions = VerbosityOptions.normal, | ||
string currentWorkingDirectory = null) | ||
{ | ||
_currentWorkingDirectory = currentWorkingDirectory; | ||
_packageInstallDir = packageInstallDir; | ||
_reporter = reporter ?? Reporter.Output; | ||
_verboseLogger = verboseLogger ?? new NuGetConsoleLogger(); | ||
|
@@ -130,22 +130,22 @@ public async Task<string> DownloadPackageAsync(PackageId packageId, | |
packageVersion.ToNormalizedString())); | ||
} | ||
|
||
VerifySigning(nupkgPath); | ||
|
||
await VerifySigning(nupkgPath, repository); | ||
return nupkgPath; | ||
} | ||
|
||
private bool verbosityGreaterThanMinimal() | ||
{ | ||
return _verbosityOptions != VerbosityOptions.quiet && _verbosityOptions != VerbosityOptions.q | ||
&& _verbosityOptions != VerbosityOptions.minimal && _verbosityOptions != VerbosityOptions.m; | ||
} | ||
private bool VerbosityGreaterThanMinimal() => | ||
_verbosityOptions != VerbosityOptions.quiet && _verbosityOptions != VerbosityOptions.q && | ||
_verbosityOptions != VerbosityOptions.minimal && _verbosityOptions != VerbosityOptions.m; | ||
|
||
private void VerifySigning(string nupkgPath) | ||
private bool DiagnosticVerbosity() => _verbosityOptions == VerbosityOptions.diag || _verbosityOptions == VerbosityOptions.diagnostic; | ||
|
||
private async Task VerifySigning(string nupkgPath, SourceRepository repository) | ||
{ | ||
if (!_verifySignatures && !_validationMessagesDisplayed) | ||
{ | ||
if (verbosityGreaterThanMinimal()) | ||
if (VerbosityGreaterThanMinimal()) | ||
{ | ||
_reporter.WriteLine(LocalizableStrings.NuGetPackageSignatureVerificationSkipped); | ||
} | ||
|
@@ -157,15 +157,25 @@ private void VerifySigning(string nupkgPath) | |
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if this is the correct location to add the comment. It could be possible that I don't have a good understanding of the code in this repo. I think we should set If we don't set nugetPackageDownloader ??= new NuGetPackageDownloader(tempDir, verboseLogger: new NullLogger(), restoreActionConfig: restoreAction, verbosityOptions: _verbosity);
Suggestion is to pass the optional parameter `verifySignatures = true` here also. |
||
} | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
if (repository is not null && | ||
await repository.GetResourceAsync<RepositorySignatureResource>().ConfigureAwait(false) is RepositorySignatureResource resource && | ||
resource.AllRepositorySigned) | ||
{ | ||
if (!_firstPartyNuGetPackageSigningVerifier.Verify(new FilePath(nupkgPath), | ||
out string commandOutput)) | ||
string commandOutput; | ||
if ((!_shouldUsePackageSourceMapping && !_firstPartyNuGetPackageSigningVerifier.Verify(new FilePath(nupkgPath), out commandOutput)) || | ||
(_shouldUsePackageSourceMapping && !FirstPartyNuGetPackageSigningVerifier.NuGetVerify(new FilePath(nupkgPath), out commandOutput, _currentWorkingDirectory))) | ||
Comment on lines
+168
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They certainly look the same. I can add a comment. It's whether or not it should additionally check that it's a first-party binary or not. |
||
{ | ||
throw new NuGetPackageInstallerException(LocalizableStrings.FailedToValidatePackageSigning + | ||
Environment.NewLine + | ||
commandOutput); | ||
throw new NuGetPackageInstallerException(string.Format(LocalizableStrings.FailedToValidatePackageSigning, commandOutput)); | ||
} | ||
|
||
if (DiagnosticVerbosity()) | ||
{ | ||
_reporter.WriteLine(LocalizableStrings.VerifyingNuGetPackageSignature, Path.GetFileNameWithoutExtension(nupkgPath)); | ||
} | ||
} | ||
else if (DiagnosticVerbosity()) | ||
{ | ||
_reporter.WriteLine(LocalizableStrings.NuGetPackageShouldNotBeSigned, Path.GetFileNameWithoutExtension(nupkgPath)); | ||
} | ||
} | ||
|
||
|
@@ -308,7 +318,7 @@ private static bool PackageIsInAllowList(IEnumerable<string> files) | |
private IEnumerable<PackageSource> LoadNuGetSources(PackageId packageId, PackageSourceLocation packageSourceLocation = null, PackageSourceMapping packageSourceMapping = null) | ||
{ | ||
List<PackageSource> defaultSources = new List<PackageSource>(); | ||
string currentDirectory = Directory.GetCurrentDirectory(); | ||
string currentDirectory = _currentWorkingDirectory ?? Directory.GetCurrentDirectory(); | ||
ISettings settings; | ||
if (packageSourceLocation?.NugetConfig != null) | ||
{ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This aka.ms link points to an announcement which doesn't apply to more recent versions of .NET.
Signed package verification is supported on Linux. See https://learn.microsoft.com/en-us/dotnet/core/tools/nuget-signed-package-verification#linux.