Repository navigation
Verify downloaded template packages #57112
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
Merged
rbhanda
merged 4 commits into
main
from
users/rbhanda/template-package-verification-20261113
Nov 24, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
15ba2e8
Verify downloaded template packages
rbhanda ed5c57d
Stage template packages in physical temporary files
JoeRobich 4f880d9
Revert "Stage template packages in physical temporary files"
JoeRobich e480a9a
Verify template packages from a private physical staging file
nagilson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
...mplateEngine/Microsoft.TemplateEngine.Edge/Installers/NuGet/DotnetNuGetPackageVerifier.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.ComponentModel; | ||
| using System.Diagnostics; | ||
| using System.Runtime.InteropServices; | ||
| using System.Security; | ||
| using Microsoft.TemplateEngine.CommandUtils; | ||
| using NuGet.Common; | ||
| using NuGet.Configuration; | ||
| using NuGet.Packaging; | ||
| using NuGet.Packaging.Signing; | ||
| using NuGet.Protocol; | ||
| using NuGet.Protocol.Core.Types; | ||
|
|
||
| namespace Microsoft.TemplateEngine.Edge.Installers.NuGet | ||
| { | ||
| internal interface INuGetPackageVerifier | ||
| { | ||
| bool IsEnabled { get; } | ||
|
|
||
| Task VerifyAsync(string packagePath, SourceRepository repository, ISettings settings, ILogger logger, CancellationToken cancellationToken); | ||
| } | ||
|
|
||
| internal sealed class DotnetNuGetPackageVerifier : INuGetPackageVerifier | ||
| { | ||
| private const string SignatureVerificationEnvironmentVariable = "DOTNET_NUGET_SIGNATURE_VERIFICATION"; | ||
|
|
||
| public DotnetNuGetPackageVerifier() | ||
| { | ||
| bool? configuredValue = bool.TryParse( | ||
| Environment.GetEnvironmentVariable(SignatureVerificationEnvironmentVariable), | ||
| out bool shouldVerify) | ||
| ? shouldVerify | ||
| : null; | ||
|
|
||
| IsEnabled = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | ||
| || configuredValue == true | ||
| || (configuredValue == null && RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); | ||
| } | ||
|
|
||
| public bool IsEnabled { get; } | ||
|
|
||
| public async Task VerifyAsync(string packagePath, SourceRepository repository, ISettings settings, ILogger logger, CancellationToken cancellationToken) | ||
| { | ||
| _ = packagePath ?? throw new ArgumentNullException(nameof(packagePath)); | ||
| _ = repository ?? throw new ArgumentNullException(nameof(repository)); | ||
| _ = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
| _ = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| bool isSigned; | ||
| using (var reader = new PackageArchiveReader(packagePath)) | ||
| { | ||
| isSigned = await reader.IsSignedAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| if (!isSigned) | ||
| { | ||
| ClientPolicyContext clientPolicy = ClientPolicyContext.GetClientPolicy(settings, logger); | ||
| RepositorySignatureResource? repositorySignature = | ||
| await repository.GetResourceAsync<RepositorySignatureResource>(cancellationToken).ConfigureAwait(false); | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| if (clientPolicy.Policy == SignatureValidationMode.Require || repositorySignature?.AllRepositorySigned == true) | ||
| { | ||
| const string message = "The package is unsigned, but client or repository policy requires signing."; | ||
| logger.LogDebug(message); | ||
| throw new InvalidDataException(message); | ||
| } | ||
|
|
||
| logger.LogDebug("Accepting unsigned package under accept policy."); | ||
| return; | ||
| } | ||
|
|
||
| await VerifySignedPackageAsync(packagePath, logger, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| private static async Task VerifySignedPackageAsync(string packagePath, ILogger logger, CancellationToken cancellationToken) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| using var process = new Process | ||
| { | ||
| StartInfo = new ProcessStartInfo | ||
| { | ||
| FileName = "dotnet", | ||
|
JoeRobich marked this conversation as resolved.
Resolved
|
||
| Arguments = $"nuget verify --all {ArgumentEscaper.EscapeSingleArg(packagePath)}", | ||
| UseShellExecute = false, | ||
| CreateNoWindow = true, | ||
| RedirectStandardError = true, | ||
| RedirectStandardOutput = true | ||
| } | ||
| }; | ||
| if (!process.Start()) | ||
| { | ||
| throw new InvalidOperationException("Failed to start 'dotnet nuget verify'."); | ||
| } | ||
|
|
||
| #if NET | ||
| Task<string> standardOutput = process.StandardOutput.ReadToEndAsync(CancellationToken.None); | ||
| Task<string> standardError = process.StandardError.ReadToEndAsync(CancellationToken.None); | ||
| #else | ||
| Task<string> standardOutput = process.StandardOutput.ReadToEndAsync(); | ||
| Task<string> standardError = process.StandardError.ReadToEndAsync(); | ||
| #endif | ||
| await WaitForExitAsync(process, logger, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| string output = await standardOutput.ConfigureAwait(false); | ||
| string error = await standardError.ConfigureAwait(false); | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| LogOutput(logger, output, LogLevel.Debug); | ||
| LogOutput(logger, error, process.ExitCode == 0 ? LogLevel.Debug : LogLevel.Error); | ||
|
|
||
| if (process.ExitCode != 0) | ||
| { | ||
| string command = $"{process.StartInfo.FileName} {process.StartInfo.Arguments}"; | ||
| throw new SecurityException($"'{command}' failed with exit code {process.ExitCode}."); | ||
| } | ||
|
|
||
| return; | ||
|
|
||
| static void LogOutput(ILogger logger, string output, LogLevel level) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(output)) | ||
| { | ||
| logger.Log(level, output.Trim()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static async Task WaitForExitAsync(Process process, ILogger logger, CancellationToken cancellationToken) | ||
| { | ||
| using (cancellationToken.Register(() => TerminateProcess(process, logger))) | ||
| { | ||
| // Process.WaitForExitAsync is unavailable on netstandard2.0 and net472. | ||
| await Task.Run(process.WaitForExit, CancellationToken.None).ConfigureAwait(false); | ||
| } | ||
|
|
||
| return; | ||
|
|
||
| static void TerminateProcess(Process process, ILogger logger) | ||
| { | ||
| try | ||
| { | ||
| if (!process.HasExited) | ||
| { | ||
| process.Kill(); | ||
|
JoeRobich marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| catch (InvalidOperationException) when (process.HasExited) | ||
| { | ||
| // The process exited between checking HasExited and calling Kill. | ||
| } | ||
| catch (Win32Exception ex) | ||
| { | ||
| if (!process.HasExited) | ||
| { | ||
| logger.LogDebug($"Failed to terminate 'dotnet nuget verify': {ex}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.