Skip to content

Commit 65184b8

Browse files
committed
C#: Avoid logging sensitive information like username and password (if hardcoded in the nuget.config feed URL).
1 parent bfbfc95 commit 65184b8

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ private void TryChangeProjectFile(DirectoryInfo projectDir, Regex pattern, strin
535535
}
536536
}
537537

538+
private string SanitizeFeedForLogging(string feed)
539+
{
540+
541+
try
542+
{
543+
// If the feed is a URL, log only the scheme, host, port, and absolute path to avoid logging sensitive information such as credentials or tokens.
544+
var uri = new Uri(feed);
545+
var port = uri.IsDefaultPort ? string.Empty : $":{uri.Port}";
546+
return $"{uri.Scheme}://{uri.Host}{port}{uri.AbsolutePath}";
547+
}
548+
catch
549+
{
550+
return feed;
551+
}
552+
}
553+
538554
/// <summary>
539555
/// If <paramref name="unreachableFeeds"/> is not empty, logs this and emits a diagnostic.
540556
/// Adds a `CompilationInfos` entry either way.
@@ -544,7 +560,10 @@ private void EmitFeedReachabilityDiagnostics(ImmutableHashSet<string> unreachabl
544560
{
545561
if (unreachableFeeds.Count > 0)
546562
{
547-
var orderedUnreachableFeeds = unreachableFeeds.OrderBy(feed => feed).ToList();
563+
var orderedUnreachableFeeds = unreachableFeeds
564+
.Select(SanitizeFeedForLogging)
565+
.OrderBy(feed => feed)
566+
.ToList();
548567
var unreachableFeedList = string.Join(", ", orderedUnreachableFeeds);
549568
logger.LogWarning($"Found unreachable NuGet feeds in C# analysis with build-mode 'none': {unreachableFeedList}. This may cause missing dependencies in the analysis.");
550569
compilationInfoContainer.CompilationInfos.Add(("Unreachable NuGet feeds", unreachableFeedList));

0 commit comments

Comments
 (0)