-
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
Updating SARIF2004 #1995
Updating SARIF2004 #1995
Changes from 7 commits
802fb3f
8cce235
ea68e1f
2206514
0dcb03d
fe26896
f4a8b41
84bde22
9ca8409
e2872fe
7bb27aa
77b8154
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public static class Extensions | ||
{ | ||
public static bool RefersToDriver(this ToolComponentReference toolComponent, string driverGuid) | ||
{ | ||
if (toolComponent.Index == -1) | ||
{ | ||
if (string.IsNullOrWhiteSpace(toolComponent.Guid)) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return toolComponent.Guid.Equals(driverGuid, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,7 +272,13 @@ Many tools follow a conventional format for the 'reportingDescriptor.id' propert | |
|
||
In several parts of a SARIF log file, a subset of information about an object appears in one place, and the full information describing all such objects appears in an array elsewhere in the log file. For example, each 'result' object has a 'ruleId' property that identifies the rule that was violated. Elsewhere in the log file, the array 'run.tool.driver.rules' contains additional information about the rules. But if the elements of the 'rules' array contained no information about the rules beyond their ids, then there might be no reason to include the 'rules' array at all, and the log file could be made smaller simply by omitting it. In some scenarios (for example, when assessing compliance with policy), the 'rules' array might be used to record the full set of rules that were evaluated. In such a scenario, the 'rules' array should be retained even if it contains only id information. | ||
|
||
Similarly, most 'result' objects contain at least one 'artifactLocation' object. Elsewhere in the log file, the array 'run.artifacts' contains additional information about the artifacts that were analyzed. But if the elements of the 'artifacts' array contained not information about the artifacts beyond their locations, then there might be no reason to include the 'artifacts' array at all, and again the log file could be made smaller by omitting it. In some scenarios (for example, when assessing compliance with policy), the 'artifacts' array might be used to record the full set of artifacts that were analyzed. In such a scenario, the 'artifacts' array should be retained even if it contains only location information.</value> | ||
Similarly, most 'result' objects contain at least one 'artifactLocation' object. Elsewhere in the log file, the array 'run.artifacts' contains additional information about the artifacts that were analyzed. But if the elements of the 'artifacts' array contained not information about the artifacts beyond their locations, then there might be no reason to include the 'artifacts' array at all, and again the log file could be made smaller by omitting it. In some scenarios (for example, when assessing compliance with policy), the 'artifacts' array might be used to record the full set of artifacts that were analyzed. In such a scenario, the 'artifacts' array should be retained even if it contains only location information. | ||
|
||
In addition to the avoiding unnecessary arrays, there are other ways to optimize the size of SARIF log files. | ||
|
||
Prefer the result object properties 'ruleId' and 'ruleIndex' to the nested object-valued property 'result.rule', unless the rule comes from a tool component other than the driver (in which case only 'result.rule' can accurately point to the metadata for the rule). The 'ruleId' and 'ruleIndex' properties are shorter and just as clear. | ||
|
||
Do not specify the result object's 'analysisTarget' property unless it differs from the result location. The canonical scenario for using 'result.analysisTarget' is a C/C++ language analyzer that is instructed to analyze example.c, and detects a result in the included file example.h. In this case, 'analysisTarget' is example.c, and the result location is in example.h.</value> | ||
</data> | ||
<data name="SARIF2004_OptimizeFileSize_Warning_EliminateLocationOnlyArtifacts_Text" xml:space="preserve"> | ||
<value>{0}: The 'artifacts' array contains no information beyond the locations of the artifacts. Removing this array might reduce the log file size without losing information. In some scenarios (for example, when assessing compliance with policy), the 'artifacts' array might be used to record the full set of artifacts that were analyzed. In such a scenario, the 'artifacts' array should be retained even if it contains only location information.</value> | ||
|
@@ -360,4 +366,13 @@ This is part of a set of authoring practices that make your rule messages more r | |
<data name="SARIF2007_ExpressPathsRelativeToRepoRoot_Warning_ExpressResultLocationsRelativeToMappedTo_Text" xml:space="preserve"> | ||
<value>{0}: This result location does not provide any of the 'uriBaseId' values that specify repository locations: '{1}'. As a result, it will not be possible to determine the location of the file containing this result relative to the root of the repository that contains it.</value> | ||
</data> | ||
<data name="SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeAnalysisTarget_Text" xml:space="preserve"> | ||
<value>{0}: The 'analysisTarget' property '{1}' is unnecessary because it is the same as the result location. Remove the 'analysisTarget' property.</value> | ||
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.
"In the result at '{0}', the 'analysisTarget' property '{1}' is unnecessary..." #Closed |
||
</data> | ||
<data name="SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeResultRuleInformation_Text" xml:space="preserve"> | ||
<value>{0}: This result specifies both 'result.ruleId' and 'result.rule'. Prefer 'result.ruleId' because it is shorter and just as clear.</value> | ||
</data> | ||
<data name="SARIF2004_OptimizeFileSize_Warning_PreferRuleId_Text" xml:space="preserve"> | ||
<value>{0}: This result uses the 'rule' property to specify the rule metadata, but the 'ruleId' property suffices because the rule is defined by 'tool.driver'. Prefer 'result.ruleId' because it is shorter and just as clear.</value> | ||
</data> | ||
</root> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,20 +41,107 @@ public class OptimizeFileSize : SarifValidationSkimmerBase | |
/// some scenarios (for example, when assessing compliance with policy), the 'artifacts' array | ||
/// might be used to record the full set of artifacts that were analyzed. In such a scenario, | ||
/// the 'artifacts' array should be retained even if it contains only location information. | ||
/// | ||
/// In addition to the avoiding unnecessary arrays, there are other ways to optimize the | ||
/// size of SARIF log files. | ||
/// | ||
/// Prefer the result object properties 'ruleId' and 'ruleIndex' to the nested object-valued | ||
/// property 'result.rule', unless the rule comes from a tool component other than the driver | ||
/// (in which case only 'result.rule' can accurately point to the metadata for the rule). | ||
/// The 'ruleId' and 'ruleIndex' properties are shorter and just as clear. | ||
/// | ||
/// Do not specify the result object's 'analysisTarget' property unless it differs from the | ||
/// result location. The canonical scenario for using 'result.analysisTarget' is a C/C++ language | ||
/// analyzer that is instructed to analyze example.c, and detects a result in the included file | ||
/// example.h. In this case, 'analysisTarget' is example.c, and the result location is in example.h. | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF2004_OptimizeFileSize_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeAnalysisTarget_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeResultRuleInformation_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_EliminateLocationOnlyArtifacts_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_EliminateIdOnlyRules_Text) | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_EliminateIdOnlyRules_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_PreferRuleId_Text) | ||
}; | ||
|
||
public override FailureLevel DefaultLevel => FailureLevel.Warning; | ||
|
||
private string driverGuid; | ||
|
||
protected override void Analyze(Run run, string runPointer) | ||
{ | ||
AnalyzeLocationOnlyArtifacts(run, runPointer); | ||
AnalyzeIdOnlyRules(run, runPointer); | ||
|
||
this.driverGuid = run.Tool.Driver?.Guid; | ||
} | ||
|
||
protected override void Analyze(Result result, string resultPointer) | ||
{ | ||
ReportUnnecessaryAnalysisTarget(result, resultPointer); | ||
ReportRuleDuplication(result, resultPointer); | ||
} | ||
|
||
private void ReportRuleDuplication(Result result, string resultPointer) | ||
{ | ||
if (result.Rule != null) | ||
{ | ||
if (result.Rule.ToolComponent != null) | ||
{ | ||
if (result.Rule.ToolComponent.RefersToDriver(this.driverGuid)) | ||
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 shorten this:
Note that you need the explicit comparison |
||
{ | ||
// {0}: This result uses the 'rule' property to specify the rule | ||
// metadata, but the 'ruleId' property suffices because the rule | ||
// is defined by 'tool.driver'. Prefer 'result.ruleId' because it | ||
// is shorter and just as clear. | ||
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. New message: The result at '{0}' uses the 'rule' property to specify the violated rule, but this is not necessary because the rule is defined by 'tool.driver'. Use the 'ruleId' and 'ruleIndex' instead, because they are shorter and just as clear. #Closed |
||
LogResult( | ||
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 logic is backwards. Take a look at the notes I attached to the chat. The idea is: If toolComponent is present, and if it does not refer to the driver -- which means that it has to be present! -- then Here's what the notes said:
|
||
resultPointer, | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_PreferRuleId_Text)); | ||
} | ||
else | ||
{ | ||
if (!string.IsNullOrWhiteSpace(result.RuleId) || result.RuleIndex >= 0) | ||
{ | ||
// {0}: This result specifies both 'result.ruleId' and 'result.rule'. | ||
// Prefer 'result.ruleId' because it is shorter and just as clear. | ||
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. I will provide new strings. #Closed 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. New message: The result at '{0}' uses the 'rule' property to specify the violated rule, so it is not necessary also to specify 'ruleId' or 'ruleIndex'. Remove the 'ruleId' and 'ruleIndex' properties. In reply to: 455809589 [](ancestors = 455809589) |
||
LogResult( | ||
resultPointer, | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeResultRuleInformation_Text)); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
// {0}: This result uses the 'rule' property to specify the rule | ||
// metadata, but the 'ruleId' property suffices because the rule | ||
// is defined by 'tool.driver'. Prefer 'result.ruleId' because it | ||
// is shorter and just as clear. | ||
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. Same message as on Line 94 above ("The result at '{0}' uses the 'rule' property to specify the violated rule, but this is not necessary because ..."). #Closed |
||
LogResult( | ||
resultPointer, | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_PreferRuleId_Text)); | ||
} | ||
} | ||
} | ||
|
||
private void ReportUnnecessaryAnalysisTarget(Result result, string resultPointer) | ||
{ | ||
if (result.Locations != null && result.AnalysisTarget != null) | ||
{ | ||
foreach (Location location in result.Locations) | ||
{ | ||
if (location?.PhysicalLocation?.ArtifactLocation?.Uri == result.AnalysisTarget.Uri) | ||
{ | ||
// {0}: The 'analysisTarget' property '{1}' is unnecessary because it is the same as | ||
// the result location. Remove the 'analysisTarget' property. | ||
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.
The 'analysisTarget' property '{1}' at '{0}' is unnecessary because it is the same as the result location. Remove the 'analysisTarget' property. #Closed |
||
LogResult( | ||
resultPointer.AtProperty(SarifPropertyName.AnalysisTarget), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeAnalysisTarget_Text), | ||
result.AnalysisTarget.Uri.OriginalString); | ||
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.
I would 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 was marked Resolved but it wasn't actually fixed. I think you tried to fix it with the In reply to: 455252796 [](ancestors = 455252796) 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. |
||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void AnalyzeLocationOnlyArtifacts(Run run, string runPointer) | ||
|
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.
Just
null
. We are checking iftoolComponent.guid
is absent from the SARIF file. If it's absent from the SARIF file,toolComponent.guid
will benull
. #ClosedThere 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.
ok. i will remove the test cases where we test for "", because that won't exist
In reply to: 455816703 [](ancestors = 455816703)