-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
Merged
KirillOsenkov
merged 10 commits into
KirillOsenkov:main
from
YuliiaKovalova:dev/ykovalova/add_secrets_search
Nov 1, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c213729
add secrets search support
YuliiaKovalova 598609e
add caching
YuliiaKovalova 75cec85
disable user name search
YuliiaKovalova f293031
Merge remote-tracking branch 'origin/main' into dev/ykovalova/add_sec…
YuliiaKovalova c46c785
bump SensitiveDataDetector version
YuliiaKovalova 7098efe
add secrets search
YuliiaKovalova ec78a22
update search class
YuliiaKovalova 70a6564
remove extra caching and use indexes for search
YuliiaKovalova 6e4fc91
cleanup
YuliiaKovalova 44b5115
move query check out of loop and handle empty result return
YuliiaKovalova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,8 @@ public partial class BuildControl : UserControl | |
|
||
private PropertiesAndItemsSearch propertiesAndItemsSearch; | ||
|
||
private SecretsSearch secretsSearch; | ||
|
||
public BuildControl(Build build, string logFilePath) | ||
{ | ||
InitializeComponent(); | ||
|
@@ -137,6 +139,7 @@ public BuildControl(Build build, string logFilePath) | |
propertiesAndItemsControl.WatermarkDisplayed += UpdatePropertiesAndItemsWatermark; | ||
propertiesAndItemsControl.RecentItemsCategory = "PropertiesAndItems"; | ||
|
||
secretsSearch = (SecretsSearch)build.SearchExtensions.FirstOrDefault(se => se is SecretsSearch); | ||
SetProjectContext(null); | ||
|
||
VirtualizingPanel.SetIsVirtualizing(treeView, SettingsService.EnableTreeViewVirtualization); | ||
|
@@ -643,6 +646,7 @@ private void PopulateProjectGraph() | |
"$task $time", | ||
"$message CompilerServer failed", | ||
"will be compiled because", | ||
"$secret" | ||
}; | ||
|
||
private static string[] nodeKinds = new[] | ||
|
@@ -662,7 +666,8 @@ private void PopulateProjectGraph() | |
"$csc", | ||
"$rar", | ||
"$import", | ||
"$noimport" | ||
"$noimport", | ||
"$secret" | ||
}; | ||
|
||
private static Inline MakeLink(string query, SearchAndResultsControl searchControl, string before = " \u2022 ", string after = "\r\n") | ||
|
@@ -1017,18 +1022,32 @@ private object FindInFiles(string searchText, int maxResults, CancellationToken | |
{ | ||
var results = new List<(string, IEnumerable<(int, string)>)>(); | ||
|
||
NodeQueryMatcher notQueryMatcher = new NodeQueryMatcher(searchText); | ||
bool isSecretsSearch = !string.IsNullOrEmpty(searchText) && searchText.StartsWith("$secret"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also check for |
||
|
||
foreach (var file in archiveFile.Files) | ||
{ | ||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
return null; | ||
} | ||
|
||
var haystack = file.Value; | ||
var resultsInFile = haystack.Find(searchText); | ||
if (resultsInFile.Count > 0) | ||
if (isSecretsSearch) | ||
{ | ||
results.Add((file.Key, resultsInFile.Select(lineNumber => (lineNumber, haystack.GetLineText(lineNumber))))); | ||
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)))); | ||
} | ||
} | ||
else | ||
{ | ||
var haystack = file.Value; | ||
var resultsInFile = haystack.Find(searchText); | ||
if (resultsInFile.Count > 0) | ||
{ | ||
results.Add((file.Key, resultsInFile.Select(lineNumber => (lineNumber, haystack.GetLineText(lineNumber))))); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using DotUtils.MsBuild.SensitiveDataDetector; | ||
using Microsoft.Build.SensitiveDataDetector; | ||
using StructuredLogViewer; | ||
|
||
namespace Microsoft.Build.Logging.StructuredLogger | ||
{ | ||
public class SecretsSearch : ISearchExtension | ||
{ | ||
private readonly Build _build; | ||
private readonly Dictionary<SensitiveDataKind, ISensitiveDataDetector> _detectors; | ||
private readonly Dictionary<string, Dictionary<SensitiveDataKind, List<SecretDescriptor>>> _secretCache = new(); | ||
|
||
public SecretsSearch(Build build) | ||
{ | ||
_build = build ?? throw new ArgumentNullException(nameof(build)); | ||
|
||
_detectors = new() | ||
{ | ||
{ SensitiveDataKind.CommonSecrets, SensitiveDataDetectorFactory.GetSecretsDetector(SensitiveDataKind.CommonSecrets, false) }, | ||
{ SensitiveDataKind.ExplicitSecrets, SensitiveDataDetectorFactory.GetSecretsDetector(SensitiveDataKind.ExplicitSecrets, false) }, | ||
{ SensitiveDataKind.Username, SensitiveDataDetectorFactory.GetSecretsDetector(SensitiveDataKind.Username, false) } | ||
}; | ||
} | ||
|
||
public bool TryGetResults(NodeQueryMatcher matcher, IList<SearchResult> results, int maxResults) | ||
{ | ||
if (string.Equals(matcher.TypeKeyword, "secret", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var activeDetectors = GetActiveDetectors(matcher.NotMatchers); | ||
var foundResults = ScanForSecrets(_build.StringTable.Instances, activeDetectors, maxResults); | ||
|
||
if (foundResults.Any()) | ||
{ | ||
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; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public List<SecretDescriptor> SearchSecrets(string text, IList<NodeQueryMatcher> matcher, int maxResults) | ||
{ | ||
var activeDetectors = GetActiveDetectors(matcher); | ||
|
||
var secrets = DetectSecrets(text, activeDetectors); | ||
|
||
return secrets.Take(maxResults).ToList(); | ||
} | ||
|
||
private IEnumerable<SearchResult> ScanForSecrets(IEnumerable<string> stringsPool, Dictionary<SensitiveDataKind, ISensitiveDataDetector> detectors, int maxResults) | ||
{ | ||
var secretsSet = new HashSet<string>(); | ||
foreach (var text in stringsPool) | ||
{ | ||
var secretResults = DetectSecrets(text, detectors); | ||
foreach (SecretDescriptor secretDescriptor in secretResults) | ||
{ | ||
secretsSet.Add(secretDescriptor.Secret); | ||
} | ||
} | ||
|
||
var results = new List<SearchResult>(); | ||
if (_build.SearchIndex is { } index) | ||
{ | ||
foreach (var text in secretsSet) | ||
{ | ||
index.MaxResults = maxResults; | ||
index.MarkResultsInTree = false; | ||
IEnumerable<SearchResult> indexResults = index.FindNodes(text, CancellationToken.None); | ||
if (indexResults.Any()) | ||
{ | ||
results.AddRange(indexResults); | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
private List<SecretDescriptor> DetectSecrets(string text, Dictionary<SensitiveDataKind, ISensitiveDataDetector> detectors) | ||
{ | ||
if (_secretCache.TryGetValue(text, out var cachedSecrets)) | ||
{ | ||
return cachedSecrets | ||
.Where(kv => detectors.Any(d => d.Key == kv.Key)) | ||
.SelectMany(kv => kv.Value) | ||
.ToList(); | ||
} | ||
|
||
var results = new Dictionary<SensitiveDataKind, List<SecretDescriptor>>(); | ||
|
||
foreach (var detector in detectors) | ||
{ | ||
Dictionary<SensitiveDataKind, List<SecretDescriptor>> detectedSecrets = detector.Value.Detect(text); | ||
foreach (KeyValuePair<SensitiveDataKind, List<SecretDescriptor>> kv in detectedSecrets) | ||
{ | ||
if (kv.Value.Any()) | ||
{ | ||
results[kv.Key] = kv.Value; | ||
} | ||
} | ||
} | ||
|
||
if (results.Any()) | ||
{ | ||
_secretCache[text] = results; | ||
YuliiaKovalova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return results.Values.SelectMany(v => v).ToList(); | ||
} | ||
|
||
private Dictionary<SensitiveDataKind, ISensitiveDataDetector> GetActiveDetectors(IList<NodeQueryMatcher> notMatchers) | ||
{ | ||
if (!notMatchers.Any()) | ||
{ | ||
return new Dictionary<SensitiveDataKind, ISensitiveDataDetector>(_detectors); | ||
} | ||
|
||
return _detectors | ||
.Where(d => !notMatchers.Any(m => Enum.TryParse<SensitiveDataKind>(m.Query, true, out var kind) && kind == d.Key)) | ||
.ToDictionary(k => k.Key, v => v.Value); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you mean
nodeQueryMatcher
?There was a problem hiding this comment.
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