Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add secrets search and bump DotUtils.MsBuild.BinlogRedactor.SensitiveDataDetector version #829

20 changes: 7 additions & 13 deletions src/StructuredLogViewer/Controls/BuildControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
using System.Windows.Media;
using System.Windows.Threading;
using System.Xml;
using DotUtils.MsBuild.SensitiveDataDetector;
using Microsoft.Build.Experimental.ProjectCache;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging.StructuredLogger;
using Microsoft.Language.Xml;
Expand Down Expand Up @@ -141,7 +139,7 @@ public BuildControl(Build build, string logFilePath)
propertiesAndItemsControl.WatermarkDisplayed += UpdatePropertiesAndItemsWatermark;
propertiesAndItemsControl.RecentItemsCategory = "PropertiesAndItems";

secretsSearch = new SecretsSearch(build);
secretsSearch = (SecretsSearch)build.SearchExtensions.FirstOrDefault(se => se is SecretsSearch);
SetProjectContext(null);

VirtualizingPanel.SetIsVirtualizing(treeView, SettingsService.EnableTreeViewVirtualization);
Expand Down Expand Up @@ -648,6 +646,7 @@ private void PopulateProjectGraph()
"$task $time",
"$message CompilerServer failed",
"will be compiled because",
"$secret"
};

private static string[] nodeKinds = new[]
Expand Down Expand Up @@ -1023,24 +1022,19 @@ private object FindInFiles(string searchText, int maxResults, CancellationToken
{
var results = new List<(string, IEnumerable<(int, string)>)>();

NodeQueryMatcher notQueryMatcher = new NodeQueryMatcher(searchText);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean nodeQueryMatcher?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it in main

bool isSecretsSearch = !string.IsNullOrEmpty(searchText) && searchText.StartsWith("$secret");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also check for nodeQueryMatcher.TypeKeyword == "secret"


foreach (var file in archiveFile.Files)
{
if (cancellationToken.IsCancellationRequested)
{
return null;
}

if (!string.IsNullOrEmpty(searchText) && searchText.StartsWith("$secret"))
if (isSecretsSearch)
{
var word = searchText.Replace("$secret", string.Empty).Trim();
NodeQueryMatcher notMatcher = null;
if (word.StartsWith("not(", StringComparison.OrdinalIgnoreCase) && word.EndsWith(")"))
{
word = word.Substring(4, word.Length - 5);
notMatcher = new NodeQueryMatcher(word);
}

var searchResults = secretsSearch.SearchSecrets(file.Value.Text, notMatcher, maxResults);
var searchResults = secretsSearch.SearchSecrets(file.Value.Text, notQueryMatcher.NotMatchers, maxResults);
if (searchResults.Count > 0)
{
results.Add((file.Key, searchResults.Select(sr => (sr.Line - 1, sr.Secret))));
Expand Down
15 changes: 11 additions & 4 deletions src/StructuredLogger.Utils/SecretsSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ public bool TryGetResults(NodeQueryMatcher matcher, IList<SearchResult> results,
var activeDetectors = GetActiveDetectors(matcher.NotMatchers);
var foundResults = ScanForSecrets(_build.StringTable.Instances, activeDetectors, maxResults);

foreach (var result in foundResults)
if (foundResults.Any())
{
results.Add(result);
foreach (var result in foundResults)
{
results.Add(result);
}
}
else
{
results.Add(new SearchResult(new Message { Text = "No secret(s) were detected in the tree." }));
}

return true;
Expand All @@ -44,9 +51,9 @@ public bool TryGetResults(NodeQueryMatcher matcher, IList<SearchResult> results,
return false;
}

public List<SecretDescriptor> SearchSecrets(string text, NodeQueryMatcher? matcher, int maxResults)
public List<SecretDescriptor> SearchSecrets(string text, IList<NodeQueryMatcher> matcher, int maxResults)
{
var activeDetectors = GetActiveDetectors(matcher == null ? [] : [matcher]);
var activeDetectors = GetActiveDetectors(matcher);

var secrets = DetectSecrets(text, activeDetectors);

Expand Down