-
Notifications
You must be signed in to change notification settings - Fork 94
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
Artifacts should only be logged in the SARIF if we have results #2431
Changes from 5 commits
58c8205
65b4788
0407059
2fcf491
2108cdb
406a62c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ public class SarifLogger : BaseLogger, IDisposable, IAnalysisLogger | |
{ | ||
private readonly Run _run; | ||
private readonly TextWriter _textWriter; | ||
private readonly bool _persistArtifacts; | ||
private readonly bool _closeWriterOnDispose; | ||
private readonly LogFilePersistenceOptions _logFilePersistenceOptions; | ||
private readonly JsonTextWriter _jsonTextWriter; | ||
|
@@ -98,8 +97,7 @@ public SarifLogger( | |
dataToRemove, | ||
invocationTokensToRedact, | ||
invocationPropertiesToLog, | ||
defaultFileEncoding, | ||
AnalysisTargetToHashDataMap); | ||
defaultFileEncoding); | ||
|
||
tool = tool ?? Tool.CreateFromAssemblyData(); | ||
|
||
|
@@ -116,11 +114,6 @@ public SarifLogger( | |
RuleToIndexMap[_run.Tool.Driver.Rules[i]] = i; | ||
} | ||
} | ||
|
||
_persistArtifacts = | ||
(_dataToInsert & OptionallyEmittedData.Hashes) != 0 || | ||
(_dataToInsert & OptionallyEmittedData.TextFiles) != 0 || | ||
(_dataToInsert & OptionallyEmittedData.BinaryFiles) != 0; | ||
} | ||
|
||
private SarifLogger( | ||
|
@@ -155,52 +148,17 @@ private void EnhanceRun( | |
OptionallyEmittedData dataToRemove, | ||
IEnumerable<string> invocationTokensToRedact, | ||
IEnumerable<string> invocationPropertiesToLog, | ||
string defaultFileEncoding = null, | ||
IDictionary<string, HashData> filePathToHashDataMap = null) | ||
string defaultFileEncoding = null) | ||
{ | ||
_run.Invocations ??= new List<Invocation>(); | ||
if (defaultFileEncoding != null) | ||
{ | ||
_run.DefaultEncoding = defaultFileEncoding; | ||
} | ||
|
||
Encoding encoding = SarifUtilities.GetEncodingFromName(_run.DefaultEncoding); | ||
|
||
if (analysisTargets != null) | ||
{ | ||
_run.Artifacts ??= new List<Artifact>(); | ||
|
||
foreach (string target in analysisTargets) | ||
{ | ||
Uri uri = new Uri(UriHelper.MakeValidUri(target), UriKind.RelativeOrAbsolute); | ||
|
||
HashData hashData = null; | ||
if (dataToInsert.HasFlag(OptionallyEmittedData.Hashes)) | ||
{ | ||
filePathToHashDataMap?.TryGetValue(target, out hashData); | ||
} | ||
|
||
var artifact = Artifact.Create( | ||
new Uri(target, UriKind.RelativeOrAbsolute), | ||
dataToInsert, | ||
encoding, | ||
hashData: hashData); | ||
|
||
var fileLocation = new ArtifactLocation | ||
{ | ||
Uri = uri | ||
}; | ||
|
||
artifact.Location = fileLocation; | ||
|
||
// This call will insert the file object into run.Files if not already present | ||
artifact.Location.Index = _run.GetFileIndex( | ||
artifact.Location, | ||
addToFilesTableIfNotPresent: true, | ||
dataToInsert: dataToInsert, | ||
encoding: encoding, | ||
hashData: hashData); | ||
} | ||
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. This code path is used by the single threaded analysis. When hashes is enabled, it was going to generate all hashes and store everything in the SARIF. The issue is that if we analyze 1k files and we do not produce any results, we would still generate the artifacts. As an optimization, we will only store the artifact if we have a result. 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. Reviewed all versions that we released and I saw that all versions after 1.4.2 are emitting the artifacts IF the list of analysisTargets is not empty. 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. Looking at the other variables, looks like we tried to improve this behavior by adding the _persistArtifacts, but that is occurring too late (after we already added the artifact to the run) |
||
} | ||
|
||
var invocation = Invocation.Create( | ||
|
@@ -447,12 +405,16 @@ private void CaptureArtifact(ArtifactLocation fileLocation) | |
catch (ArgumentException) { } // Unrecognized encoding name | ||
} | ||
|
||
HashData hashData = null; | ||
AnalysisTargetToHashDataMap?.TryGetValue(fileLocation.Uri.OriginalString, out hashData); | ||
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.
For the single threaded analysis, the The CaptureArtifact method is only used when we are logging the artifacts of a result. 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. If hashes isn't enabled, analysisTargetToHashDataMap will be null, so we are using the null check to guarantee and prevent a null reference exception. |
||
|
||
// Ensure Artifact is in Run.Artifacts and ArtifactLocation.Index is set to point to it | ||
int index = _run.GetFileIndex( | ||
fileLocation, | ||
addToFilesTableIfNotPresent: _persistArtifacts, | ||
_dataToInsert, | ||
encoding); | ||
addToFilesTableIfNotPresent: true, | ||
dataToInsert: _dataToInsert, | ||
encoding, | ||
hashData); | ||
|
||
// Remove redundant Uri and UriBaseId once index has been set | ||
if (index > -1 && this.Optimize) | ||
|
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.
instead of adding artifacts for all files, we are moving this to the place when we have an actual result.
The hash itself will already be in the context, so, no need to calculate it again.