Skip to content

Commit ab61dfb

Browse files
committed
C#: Do not use NuGet feeds that returns a 401 unathorized message.
1 parent caccee9 commit ab61dfb

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,9 @@ private void TryChangeProjectFile(DirectoryInfo projectDir, Regex pattern, strin
611611
}
612612
}
613613

614-
private static async Task ExecuteGetRequest(string address, HttpClient httpClient, CancellationToken cancellationToken)
614+
private static async Task<HttpResponseMessage> ExecuteGetRequest(string address, HttpClient httpClient, CancellationToken cancellationToken)
615615
{
616-
using var stream = await httpClient.GetStreamAsync(address, cancellationToken);
617-
var buffer = new byte[1024];
618-
int bytesRead;
619-
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
620-
{
621-
// do nothing
622-
}
616+
return await httpClient.GetAsync(address, cancellationToken);
623617
}
624618

625619
private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, bool allowExceptions = true)
@@ -661,7 +655,8 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
661655
cts.CancelAfter(timeoutMilliSeconds);
662656
try
663657
{
664-
ExecuteGetRequest(feed, client, cts.Token).GetAwaiter().GetResult();
658+
var response = ExecuteGetRequest(feed, client, cts.Token).GetAwaiter().GetResult();
659+
response.EnsureSuccessStatusCode();
665660
logger.LogInfo($"Querying NuGet feed '{feed}' succeeded.");
666661
return true;
667662
}
@@ -675,6 +670,13 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
675670
timeoutMilliSeconds *= 2;
676671
continue;
677672
}
673+
if (exc is HttpRequestException hre &&
674+
hre.StatusCode == HttpStatusCode.Unauthorized)
675+
{
676+
677+
logger.LogInfo($"Received 401 Unauthorized error from NuGet feed '{feed}'.");
678+
return false;
679+
}
678680

679681
// We're only interested in timeouts.
680682
var start = allowExceptions ? "Considering" : "Not considering";

0 commit comments

Comments
 (0)