Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Cli/dotnet/CliStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,7 @@ The default is 'false.' However, when targeting .NET 7 or lower, the default is
<data name="SDKSchemaCommandDefinition" xml:space="preserve">
<value>Display the command schema as JSON.</value>
</data>
<data name="Error_NU1302_HttpSourceUsed" xml:space="preserve">
<value>error NU1302: You are running the 'tool install' operation with an 'HTTP' source: {0}. NuGet requires HTTPS sources. To use an HTTP source, you must explicitly set 'allowInsecureConnections' to true in your NuGet.Config file. Refer to https://aka.ms/nuget-https-everywhere for more information.</value>
</data>
</root>
38 changes: 38 additions & 0 deletions src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#nullable disable

using System.Collections.Concurrent;
using Microsoft.DotNet.Cli.Extensions;
using Microsoft.DotNet.Cli.NugetPackageDownloader;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Cli.Utils.Extensions;
using Microsoft.Extensions.EnvironmentAbstractions;
using NuGet.Common;
using NuGet.Configuration;
Expand Down Expand Up @@ -450,9 +452,45 @@ public IEnumerable<PackageSource> LoadNuGetSources(PackageId packageId, PackageS
throw new NuGetPackageInstallerException("No NuGet sources are defined or enabled");
}

// Load settings to check allowInsecureConnections
string currentDirectory = _currentWorkingDirectory ?? Directory.GetCurrentDirectory();
ISettings settings;
if (packageSourceLocation?.NugetConfig != null)
{
string nugetConfigParentDirectory =
packageSourceLocation.NugetConfig.Value.GetDirectoryPath().Value;
string nugetConfigFileName = Path.GetFileName(packageSourceLocation.NugetConfig.Value.Value);
settings = Settings.LoadSpecificSettings(nugetConfigParentDirectory,
nugetConfigFileName);
}
else
{
settings = Settings.LoadDefaultSettings(
packageSourceLocation?.RootConfigDirectory?.Value ?? currentDirectory);
}

CheckHttpSources(sources, settings);
return sources;
}

private void CheckHttpSources(IEnumerable<PackageSource> packageSources, ISettings settings)
{
var httpSources = packageSources.Where(source => !source.IsLocal && source.SourceUri?.Scheme?.Equals("http", StringComparison.OrdinalIgnoreCase) == true).ToList();

if (httpSources.Any())
{
// TODO: Check if allowInsecureConnections is set to true in the config section
// The NuGet Configuration API for reading specific settings needs further investigation
// For now, always throw error for HTTP sources (as per .NET 9 requirement)

// Throw error for each HTTP source found
foreach (var httpSource in httpSources)
{
throw new NuGetPackageInstallerException(string.Format(CliStrings.Error_NU1302_HttpSourceUsed, httpSource.Source));
}
}
}

private async Task<(PackageSource, IPackageSearchMetadata)> GetMatchingVersionInternalAsync(
string packageIdentifier, IEnumerable<PackageSource> packageSources, VersionRange versionRange,
CancellationToken cancellationToken)
Expand Down
5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.cs.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.de.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.es.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.fr.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.it.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.ja.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.ko.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.pl.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.pt-BR.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.ru.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.tr.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.zh-Hans.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/xlf/CliStrings.zh-Hant.xlf

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

Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,35 @@ public void SetPermission(string path, string chmodArgument)
</packageSources>
</configuration>
}";
}
}

[Fact]
public void WhenRunWithHttpSourceItShouldThrowError()
{
// Write the HTTP config to the default nuget.config location in the temporary directory
_fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, "nuget.config"), @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""httpsource"" value=""http://insecure.nuget.org/v3/index.json"" />
</packageSources>
</configuration>");

var parseResult = Parser.Parse($"dotnet tool install -g {PackageId}");

var toolInstallGlobalOrToolPathCommand = new ToolInstallGlobalOrToolPathCommand(
parseResult,
_packageId,
_createToolPackageStoreDownloaderUninstaller,
_createShellShimRepository,
new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true),
_reporter);

// Verify that HTTP sources cause the command to fail
Action act = () => toolInstallGlobalOrToolPathCommand.Execute();
act.Should().Throw<Exception>()
.And.Message.Should().Contain("NU1302");

// Clean up
_fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config"));
}
}
}
Loading