-
Notifications
You must be signed in to change notification settings - Fork 91
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
sarif validation rule 2010 - provide code snippets #1953
Changes from 1 commit
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,74 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
using Microsoft.Json.Pointer; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool.Rules | ||
{ | ||
public class ProvideCodeSnippets : SarifValidationSkimmerBase | ||
{ | ||
/// <summary> | ||
/// SARIF2010 | ||
/// </summary> | ||
public override string Id => RuleId.ProvideCodeSnippets; | ||
|
||
/// <summary> | ||
/// Placeholder_SARIF2010_ProvideCodeSnippets_FullDescription_Text | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF2010_ProvideCodeSnippets_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.SARIF2010_ProvideCodeSnippets_Note_Default_Text) | ||
}; | ||
|
||
public override FailureLevel DefaultLevel => FailureLevel.Note; | ||
|
||
protected override void Analyze(Result result, string resultPointer) | ||
{ | ||
if (result.Locations != null) | ||
{ | ||
string locationsPointer = resultPointer.AtProperty(SarifPropertyName.Locations); | ||
for (int i = 0; i < result.Locations.Count; i++) | ||
{ | ||
AnalyzeResultLocation(result.Locations[i], locationsPointer.AtIndex(i)); | ||
} | ||
} | ||
} | ||
|
||
private void AnalyzeResultLocation(Location location, string locationPointer) | ||
{ | ||
Region region = location.PhysicalLocation?.Region; | ||
Region contextRegion = location.PhysicalLocation?.ContextRegion; | ||
|
||
if (region != null && contextRegion != null) | ||
{ | ||
AnalyzeRegion( | ||
region, | ||
locationPointer | ||
.AtProperty(SarifPropertyName.PhysicalLocation) | ||
.AtProperty(SarifPropertyName.Region)); | ||
|
||
AnalyzeRegion( | ||
contextRegion, | ||
locationPointer | ||
.AtProperty(SarifPropertyName.PhysicalLocation) | ||
.AtProperty(SarifPropertyName.ContextRegion)); | ||
} | ||
} | ||
|
||
private void AnalyzeRegion(Region region, string regionPointer) | ||
{ | ||
|
||
if (region.Snippet == null) | ||
{ | ||
// Placeholder_SARIF2010_ProvideCodeSnippets_Note_Default_Text | ||
LogResult( | ||
regionPointer, | ||
nameof(RuleResources.SARIF2010_ProvideCodeSnippets_Note_Default_Text)); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json", | ||
"version": "2.1.0", | ||
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. oops, wrong file, pushing the correct one! hold on! #Closed |
||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "CodeScanner", | ||
"version": "1.0" | ||
} | ||
}, | ||
"versionControlProvenance": [ | ||
{ | ||
"repositoryUri": "https://github.com/microsoft/sarif-sdk" | ||
} | ||
], | ||
"results": [ | ||
{ | ||
"ruleId": "TST0001", | ||
"level": "error", | ||
"message": { | ||
"text": "Some testing occurred." | ||
}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "src/test.c" | ||
}, | ||
"region": { | ||
"startLine": 3 | ||
}, | ||
"contextRegion": { | ||
"startLine": 2, | ||
"endLine": 4 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "SARIF Functional Testing" | ||
} | ||
}, | ||
"invocations": [ | ||
{ | ||
"toolConfigurationNotifications": [ | ||
{ | ||
"message": { | ||
"text": "Rule 'SARIF2002' was explicitly disabled by the user. As result, this tool run cannot be used for compliance or other auditing processes that require a comprehensive analysis." | ||
}, | ||
"descriptor": { | ||
"id": "WRN999.RuleExplicitlyDisabled" | ||
} | ||
}, | ||
{ | ||
"message": { | ||
"text": "Rule 'SARIF2006' was explicitly disabled by the user. As result, this tool run cannot be used for compliance or other auditing processes that require a comprehensive analysis." | ||
}, | ||
"descriptor": { | ||
"id": "WRN999.RuleExplicitlyDisabled" | ||
} | ||
} | ||
], | ||
"executionSuccessful": true | ||
} | ||
], | ||
"artifacts": [ | ||
{ | ||
"location": { | ||
"uri": "FunctionalTestOutput.ValidateCommand/Inputs.SARIF2010.ProvideCodeSnippets_Valid.sarif", | ||
"uriBaseId": "TEST_DIR" | ||
} | ||
} | ||
], | ||
"results": [], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "CodeScanner", | ||
"version": "1.0" | ||
} | ||
}, | ||
"versionControlProvenance": [ | ||
{ | ||
"repositoryUri": "https://github.com/microsoft/sarif-sdk" | ||
} | ||
], | ||
"results": [ | ||
{ | ||
"ruleId": "TST0001", | ||
"level": "error", | ||
"message": { | ||
"text": "Some testing occurred." | ||
}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "src/test.c" | ||
}, | ||
"region": { | ||
"startLine": 3 | ||
}, | ||
"contextRegion": { | ||
"startLine": 2, | ||
"endLine": 4 | ||
} | ||
} | ||
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.
Add test cases where:
|
||
} | ||
] | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "CodeScanner", | ||
"version": "1.0" | ||
} | ||
}, | ||
"versionControlProvenance": [ | ||
{ | ||
"repositoryUri": "https://github.com/microsoft/sarif-sdk" | ||
} | ||
], | ||
"results": [ | ||
{ | ||
"ruleId": "TST0001", | ||
"level": "error", | ||
"message": { | ||
"text": "Some testing occurred." | ||
}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "src/test.c" | ||
}, | ||
"region": { | ||
"startLine": 3, | ||
"snippet": {"text": "region code snippet"} | ||
}, | ||
"contextRegion": { | ||
"startLine": 2, | ||
"endLine": 4, | ||
"snippet": { "text": "context region code snippet. More context here." } | ||
} | ||
} | ||
} | ||
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.
Add a valid test case where it's just a region with a snippet, no contextRegion. #ByDesign 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 take it back. That will cause a In reply to: 448028963 [](ancestors = 448028963) 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. larry i added it and trhen decided to remove it - reason: then we get violation for rule#2011. it was basically a copy of 2011_invalid. i can add it back - it just felt unnecessary. I can add it back. In reply to: 448028963 [](ancestors = 448028963) 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. |
||
] | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
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.
Bug: Handle region and contextRegion separately. Whichever one(s) is (are) present, they need snippets. #Closed