Skip to content

Commit d6a6f51

Browse files
michaelnebelCopilot
andcommitted
C#: Exclude private registries from NuGet CLI restore feeds
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 18b48aa commit d6a6f51

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

‎csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ public List<string> MakeRestoreFeeds(string path)
246246
return RestoreFeeds(feedsToUse);
247247
}
248248

249+
/// <summary>
250+
/// Constructs the NuGet CLI restore sources, excluding private registries that require the registry proxy.
251+
/// An empty list leaves source selection to NuGet only when no filtering or default feeds are needed.
252+
/// </summary>
253+
public List<string> MakeNugetExeRestoreFeeds(string path)
254+
{
255+
var feedsToUse = FeedsToUse(path).ToList();
256+
var defaultFeeds = CheckNugetFeedResponsiveness ? ReachableDefaultFeeds : DefaultFeeds;
257+
var useDefaultFeeds = feedsToUse.Count == 0 && defaultFeeds.Count > 0;
258+
259+
if (!CheckNugetFeedResponsiveness && !HasPrivateRegistryFeeds && !useDefaultFeeds)
260+
{
261+
return [];
262+
}
263+
264+
if (useDefaultFeeds)
265+
{
266+
feedsToUse.AddRange(defaultFeeds);
267+
}
268+
269+
// Filter after selecting defaults so private base feeds cannot be reintroduced.
270+
return RestoreFeeds(feedsToUse.Where(feed => !privateRegistryFeeds.Contains(feed)));
271+
}
272+
249273
private (int initialTimeout, int tryCount) GetFeedRequestSettings(bool isFallback)
250274
{
251275
int timeoutMilliSeconds = isFallback && int.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariableNames.NugetFeedResponsivenessInitialTimeoutForFallback), out timeoutMilliSeconds)

‎csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/PackagesConfigRestorer.cs‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,8 @@ private bool TryRestoreNugetPackage(string packagesConfig)
163163
{
164164
logger.LogInfo($"Restoring file \"{packagesConfig}\"...");
165165

166-
List<string> sourcesArgument = [];
167-
var feedsToUse = feedManager.FeedsToUse(packagesConfig).ToList();
168-
var defaultFeeds = feedManager.CheckNugetFeedResponsiveness
169-
? feedManager.ReachableDefaultFeeds
170-
: feedManager.DefaultFeeds;
171-
var useDefaultFeeds = feedsToUse.Count == 0 && defaultFeeds.Count > 0;
172-
173-
// Explicitly construct the sources to be used for the restore command when checking feed
174-
// responsiveness, using private registries, or falling back to default feeds.
175-
if (feedManager.CheckNugetFeedResponsiveness || feedManager.HasPrivateRegistryFeeds || useDefaultFeeds)
176-
{
177-
if (useDefaultFeeds)
178-
{
179-
feedsToUse.AddRange(defaultFeeds);
180-
}
181-
var restoreFeeds = feedManager.RestoreFeeds(feedsToUse);
182-
sourcesArgument = restoreFeeds.SelectMany<string, string>(feed => ["-Source", feed]).ToList();
183-
}
166+
var sourcesArgument = feedManager.MakeNugetExeRestoreFeeds(packagesConfig)
167+
.SelectMany<string, string>(feed => ["-Source", feed]).ToList();
184168

185169
/* Use nuget.exe to install a package.
186170
* Note that there is a clutch of NuGet assemblies which could be used to

0 commit comments

Comments
 (0)