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

Adding Rule SARIF2011 #1948

Merged
merged 3 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Sarif.Multitool/Rules/RuleResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Sarif.Multitool/Rules/RuleResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ Many tool use similar names for 'uriBaseId' symbols. We suggest 'REPOROOT' for t
<value>Placeholder_SARIF2011_ProvideContextRegion_FullDescription_Text</value>
</data>
<data name="SARIF2011_ProvideContextRegion_Note_Default_Text" xml:space="preserve">
<value>Placeholder_SARIF2011_ProvideContextRegion_Note_Default_Text</value>
<value>{0}: Placeholder</value>
</data>
<data name="SARIF1012_MessageArgumentsMustBeConsistentWithRule_Error_MessageIdMustExist_Text" xml:space="preserve">
<value>{0}: Placeholder '{1}' '{2}'</value>
Expand Down
55 changes: 55 additions & 0 deletions src/Sarif.Multitool/Rules/SARIF2011.ProvideContextRegion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 Microsoft.Json.Pointer;

namespace Microsoft.CodeAnalysis.Sarif.Multitool.Rules
{
public class ProvideContextRegion : SarifValidationSkimmerBase
{
/// <summary>
/// SARIF2011
/// </summary>
public override string Id => RuleId.ProvideContextRegion;

/// <summary>
/// Placeholder
/// </summary>
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF2011_ProvideContextRegion_FullDescription_Text };

protected override IEnumerable<string> MessageResourceNames => new string[] {
nameof(RuleResources.SARIF2011_ProvideContextRegion_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++)
{
AnalyzeLocation(result.Locations[i], locationsPointer.AtIndex(i));
}
}
}

private void AnalyzeLocation(Location location, string locationPointer)
{
if (location.PhysicalLocation?.Region != null)
{
string physicalLocation = locationPointer.AtProperty(SarifPropertyName.PhysicalLocation);
Copy link

@ghost ghost Jun 30, 2020

Choose a reason for hiding this comment

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

string physicalLocation [](start = 16, length = 23)

  1. You can put this inside the if statement since you don't need it unless the test fails.
  2. Call it physicalLocationPointer since it's not actually a physical location object. #Closed

if (location.PhysicalLocation?.ContextRegion == null)
{
// {0}: Placeholder
LogResult(
physicalLocation,
nameof(RuleResources.SARIF2011_ProvideContextRegion_Note_Default_Text));
}
}
}
}
}
12 changes: 12 additions & 0 deletions src/Test.FunctionalTests.Sarif/Multitool/ValidateCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ public void SARIF2009_ConsiderConventionalIdentifierValues_Invalid()
MakeInvalidTestFileName(RuleId.ConsiderConventionalIdentifierValues, nameof(RuleId.ConsiderConventionalIdentifierValues)),
parameter: new TestParameters(verbose: true));

[Fact]
public void SARIF2011_ProvideContextRegion_Valid()
=> RunTest(
MakeValidTestFileName(RuleId.ProvideContextRegion, nameof(RuleId.ProvideContextRegion)),
parameter: new TestParameters(verbose: true));

[Fact]
public void SARIF2011_ProvideContextRegion_Invalid()
=> RunTest(
MakeInvalidTestFileName(RuleId.ProvideContextRegion, nameof(RuleId.ProvideContextRegion)),
parameter: new TestParameters(verbose: true));

private const string ValidTestFileNameSuffix = "_Valid.sarif";
private const string InvalidTestFileNameSuffix = "_Invalid.sarif";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"$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",
"rules": [
{
"id": "SARIF2011",
"name": "ProvideContextRegion",
"shortDescription": {
"text": "Placeholder_SARIF2011_ProvideContextRegion_FullDescription_Text."
},
"fullDescription": {
"text": "Placeholder_SARIF2011_ProvideContextRegion_FullDescription_Text"
},
"messageStrings": {
"Note_Default": {
"text": "{0}: Placeholder"
}
},
"helpUri": "http://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html"
}
]
}
},
"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.SARIF2011.ProvideContextRegion_Invalid.sarif",
"uriBaseId": "TEST_DIR"
}
}
],
"results": [
{
"ruleId": "SARIF2011",
"ruleIndex": 0,
"level": "note",
"message": {
"id": "Note_Default",
"arguments": [
"runs[0].results[0].locations[0].physicalLocation"
]
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"index": 0
},
"region": {
"startLine": 26,
"startColumn": 35
}
}
}
]
}
],
"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.SARIF2011.ProvideContextRegion_Valid.sarif",
"uriBaseId": "TEST_DIR"
}
}
],
"results": [],
"columnKind": "utf16CodeUnits"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$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
}
}
}
]
}
],
"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"
}
],
Copy link

@ghost ghost Jun 30, 2020

Choose a reason for hiding this comment

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

I see, this necessary because SARIF2003.ProvideVersionControlProvenance is"note"-level and so is this rule. #ByDesign

"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"
}
]
}