-
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
Adding rule SARIF1012 #1944
Merged
eddynaka
merged 7 commits into
features/sarif-validation-rules
from
users/ednakamu/sarif-1012
Jun 30, 2020
Merged
Adding rule SARIF1012 #1944
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c6fa3e8
Adding rule 1012
eddynaka 2b8738b
code review - 1
eddynaka dbafe94
code review - 2
eddynaka 3c5be66
code review - 3
eddynaka 53564a0
removing duplicated tags
eddynaka 6a1c0ad
code review - 4
eddynaka a38ede2
updating tests
eddynaka 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
89 changes: 89 additions & 0 deletions
89
src/Sarif.Multitool/Rules/SARIF1012.MessageArgumentsMustBeConsistentWithRule.cs
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,89 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool.Rules | ||
{ | ||
public class MessageArgumentsMustBeConsistentWithRule : SarifValidationSkimmerBase | ||
{ | ||
/// <summary> | ||
/// SARIF1012 | ||
/// </summary> | ||
public override string Id => RuleId.MessageArgumentsMustBeConsistentWithRule; | ||
|
||
/// <summary> | ||
/// Placeholder | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF1012_MessageArgumentsMustBeConsistentWithRule_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.SARIF1012_MessageArgumentsMustBeConsistentWithRule_Error_MessageIdMustExist_Text), | ||
nameof(RuleResources.SARIF1012_MessageArgumentsMustBeConsistentWithRule_Error_SupplyEnoughMessageArguments_Text) | ||
}; | ||
|
||
public override FailureLevel DefaultLevel => FailureLevel.Error; | ||
|
||
private static readonly Regex s_replacementSequenceRegex = new Regex(@"\{(<index>\d+)\}", RegexOptions.Compiled | RegexOptions.CultureInvariant); | ||
private IList<ReportingDescriptor> currentRules; | ||
private Run run; | ||
|
||
protected override void Analyze(Run run, string runPointer) | ||
{ | ||
this.run = run; | ||
this.currentRules = run.Tool.Driver?.Rules; | ||
} | ||
|
||
protected override void Analyze(Result result, string resultPointer) | ||
{ | ||
// If message.id is present, check that a message with that id exists in the rule. | ||
if (!string.IsNullOrEmpty(result.Message.Id)) | ||
{ | ||
ReportingDescriptor rule = result.GetRule(this.run); | ||
|
||
if (this.currentRules == null | ||
|| rule.MessageStrings?.ContainsKey(result.Message.Id) == false) | ||
{ | ||
// {0}: Placeholder {1} {2} | ||
LogResult( | ||
resultPointer, | ||
nameof(RuleResources.SARIF1012_MessageArgumentsMustBeConsistentWithRule_Error_MessageIdMustExist_Text), | ||
result.Message.Id, | ||
result.ResolvedRuleId(run) ?? "null"); | ||
return; | ||
} | ||
|
||
// A message with the specified key is present in the rule. Check if the result supplied enough arguments. | ||
string messageText = rule.MessageStrings[result.Message.Id].Text; | ||
int placeholderMaxPosition = PlaceholderMaxPosition(messageText); | ||
if (placeholderMaxPosition > (result.Message.Arguments?.Count ?? 0)) | ||
{ | ||
// {0}: Placeholder {1} {2} {3} {4} {5} | ||
LogResult( | ||
resultPointer, | ||
nameof(RuleResources.SARIF1012_MessageArgumentsMustBeConsistentWithRule_Error_SupplyEnoughMessageArguments_Text), | ||
result.Message.Arguments.Count.ToString(), | ||
result.Message.Id, | ||
result.ResolvedRuleId(run) ?? "null", | ||
placeholderMaxPosition.ToString(), | ||
messageText); | ||
} | ||
} | ||
} | ||
|
||
private int PlaceholderMaxPosition(string text) | ||
{ | ||
int max = -1; | ||
foreach (Match match in s_replacementSequenceRegex.Matches(text)) | ||
{ | ||
int index = int.Parse(match.Groups["index"].Value); | ||
max = Math.Max(max, index); | ||
} | ||
|
||
return max++; | ||
} | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
...eCommand/ExpectedOutputs/SARIF1012.MessageArgumentsMustBeConsistentWithRule_Invalid.sarif
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,76 @@ | ||
{ | ||
"$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": "SARIF1012", | ||
"name": "MessageArgumentsMustBeConsistentWithRule", | ||
"shortDescription": { | ||
"text": "Placeholder." | ||
}, | ||
"fullDescription": { | ||
"text": "Placeholder" | ||
}, | ||
"messageStrings": { | ||
"Error_MessageIdMustExist": { | ||
"text": "{0}: Placeholder '{1}' '{2}'" | ||
}, | ||
"Error_SupplyEnoughMessageArguments": { | ||
"text": "{0}: Placeholder '{1}' '{2}' '{3}' '{4}' '{5}'" | ||
} | ||
}, | ||
"helpUri": "http://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html" | ||
} | ||
] | ||
} | ||
}, | ||
"invocations": [ | ||
{ | ||
"executionSuccessful": true | ||
} | ||
], | ||
"artifacts": [ | ||
{ | ||
"location": { | ||
"uri": "FunctionalTestOutput.ValidateCommand/Inputs.SARIF1012.MessageArgumentsMustBeConsistentWithRule_Invalid.sarif", | ||
"uriBaseId": "TEST_DIR" | ||
} | ||
} | ||
], | ||
"results": [ | ||
{ | ||
"ruleId": "SARIF1012", | ||
"ruleIndex": 0, | ||
"level": "error", | ||
"message": { | ||
"id": "Error_MessageIdMustExist", | ||
"arguments": [ | ||
"runs[0].results[1]", | ||
"DoesNotExist", | ||
"TEST1001" | ||
] | ||
}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"index": 0 | ||
}, | ||
"region": { | ||
"startLine": 37, | ||
"startColumn": 9 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
28 changes: 28 additions & 0 deletions
28
...ateCommand/ExpectedOutputs/SARIF1012.MessageArgumentsMustBeConsistentWithRule_Valid.sarif
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,28 @@ | ||
{ | ||
"$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": [ | ||
{ | ||
"executionSuccessful": true | ||
} | ||
], | ||
"artifacts": [ | ||
{ | ||
"location": { | ||
"uri": "FunctionalTestOutput.ValidateCommand/Inputs.SARIF1012.MessageArgumentsMustBeConsistentWithRule_Valid.sarif", | ||
"uriBaseId": "TEST_DIR" | ||
} | ||
} | ||
], | ||
"results": [], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
51 changes: 51 additions & 0 deletions
51
...l/ValidateCommand/Inputs/SARIF1012.MessageArgumentsMustBeConsistentWithRule_Invalid.sarif
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,51 @@ | ||
{ | ||
"$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", | ||
"version": "1.2.3", | ||
"rules": [ | ||
{ | ||
"id": "TEST1001", | ||
"name": "Test 1001", | ||
"fullDescription": { | ||
"text": "Test 1001 full description." | ||
}, | ||
"messageStrings": { | ||
"DoesExist": { | ||
"text": "'{0}': Placeholder '{1}'." | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST1001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"id": "DoesExist", | ||
"arguments": [ | ||
"runs[0].originalUriBaseIds.SRCINVALID" | ||
] | ||
} | ||
}, | ||
{ | ||
"ruleId": "TEST1001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"id": "DoesNotExist", | ||
"arguments": [ | ||
"runs[0].originalUriBaseIds.SRCINVALID" | ||
] | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
Oops, something went wrong.
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.
ToString()
not necessary (here and a few lines down). #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.
LogResult only accepts strings.
In reply to: 447716571 [](ancestors = 447716571)
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.
Oh wow. That's not good, ok, thanks.
In reply to: 447719968 [](ancestors = 447719968,447716571)