Skip to content

Commit 56120ef

Browse files
committed
C#: Sanitize feed information before putting it in the log.
1 parent 8c6d158 commit 56120ef

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,21 @@ 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, we only log the scheme, host, and absolute path to avoid logging sensitive information such as credentials or tokens.
544+
var uri = new Uri(feed);
545+
return $"{uri.Scheme}://{uri.Host}{uri.AbsolutePath}";
546+
}
547+
catch
548+
{
549+
return feed;
550+
}
551+
}
552+
538553
/// <summary>
539554
/// If <paramref name="unreachableFeeds"/> is not empty, logs this and emits a diagnostic.
540555
/// Adds a `CompilationInfos` entry either way.
@@ -544,7 +559,10 @@ private void EmitFeedReachabilityDiagnostics(ImmutableHashSet<string> unreachabl
544559
{
545560
if (unreachableFeeds.Count > 0)
546561
{
547-
var orderedUnreachableFeeds = unreachableFeeds.OrderBy(feed => feed).ToList();
562+
var orderedUnreachableFeeds = unreachableFeeds
563+
.Select(SanitizeFeedForLogging)
564+
.OrderBy(feed => feed)
565+
.ToList();
548566
var unreachableFeedList = string.Join(", ", orderedUnreachableFeeds);
549567
logger.LogWarning($"Found unreachable NuGet feeds in C# analysis with build-mode 'none': {unreachableFeedList}. This may cause missing dependencies in the analysis.");
550568
compilationInfoContainer.CompilationInfos.Add(("Unreachable NuGet feeds", unreachableFeedList));

0 commit comments

Comments
 (0)