-
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
Adding rule SARIF2005 #1926
Adding rule SARIF2005 #1926
Changes from all commits
7012a52
b0c8a8b
9c97ea9
b097093
7ce0c28
b701e18
a127548
a9fe911
bda3244
File filter
Filter by extension
Conversations
Jump to
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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.Text.RegularExpressions; | ||
|
||
using Microsoft.Json.Pointer; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool.Rules | ||
{ | ||
public class ProvideHelpfulToolInformation : SarifValidationSkimmerBase | ||
{ | ||
/// <summary> | ||
/// SARIF2005 | ||
/// </summary> | ||
public override string Id => RuleId.ProvideHelpfulToolInformation; | ||
|
||
/// <summary> | ||
/// Placeholder (full description). | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF2005_ProvideHelpfulToolInformation_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.SARIF2005_ProvideHelpfulToolInformation_Warning_ProvideToolVersion_Text), | ||
nameof(RuleResources.SARIF2005_ProvideHelpfulToolInformation_Warning_ProvideConciseToolName_Text), | ||
nameof(RuleResources.SARIF2005_ProvideHelpfulToolInformation_Warning_UseNumericToolVersions_Text) | ||
}; | ||
|
||
public override FailureLevel DefaultLevel => FailureLevel.Warning; | ||
|
||
private static readonly Regex s_versionRegex = new Regex(@"^\d+\.\d+.*", RegexOptions.Compiled | RegexOptions.CultureInvariant); | ||
|
||
protected override void Analyze(Tool tool, string toolPointer) | ||
{ | ||
if (tool.Driver != null) | ||
{ | ||
AnalyzeToolDriver(tool.Driver, toolPointer.AtProperty(SarifPropertyName.Driver)); | ||
} | ||
} | ||
|
||
private void AnalyzeToolDriver(ToolComponent toolComponent, string toolDriverPointer) | ||
{ | ||
// ProvideConciseToolName: Ensure that tool.driver.name isn't more than 3 words long | ||
if (!string.IsNullOrEmpty(toolComponent.Name)) | ||
{ | ||
const int MaxWords = 3; | ||
int wordCount = toolComponent.Name.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length; | ||
if (wordCount > MaxWords) | ||
{ | ||
string driverNamePointer = toolDriverPointer.AtProperty(SarifPropertyName.Name); | ||
|
||
// {0}: Placeholder '{1}' '{2}' '{3}' | ||
LogResult( | ||
driverNamePointer, | ||
nameof(RuleResources.SARIF2005_ProvideHelpfulToolInformation_Warning_ProvideConciseToolName_Text), | ||
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.
Let's do one more thing. Let's add an argument which is the actual tool driver name. Let's make that argument 1, argument 2 is the word count, and argument 3 is the actual word count. Don't forget to update the placeholder resource string. #Closed |
||
toolComponent.Name, | ||
wordCount.ToString(), | ||
MaxWords.ToString()); | ||
} | ||
} | ||
|
||
// ProvideToolVersion: Either tool.driver.version or tool.driver.semanticVersion should be there. | ||
if (string.IsNullOrWhiteSpace(toolComponent.Version) && string.IsNullOrWhiteSpace(toolComponent.SemanticVersion)) | ||
{ | ||
// {0}: Placeholder | ||
LogResult( | ||
toolDriverPointer, | ||
nameof(RuleResources.SARIF2005_ProvideHelpfulToolInformation_Warning_ProvideToolVersion_Text)); | ||
} | ||
else | ||
{ | ||
// UseNumericToolVersions | ||
if (!string.IsNullOrWhiteSpace(toolComponent.Version)) | ||
{ | ||
AnalyzeVersion(toolComponent.Version, toolDriverPointer.AtProperty(SarifPropertyName.Version)); | ||
} | ||
} | ||
} | ||
|
||
private void AnalyzeVersion(string version, string pointer) | ||
{ | ||
if (!s_versionRegex.IsMatch(version)) | ||
{ | ||
// {0}: Placeholder '{1}' | ||
LogResult( | ||
pointer, | ||
nameof(RuleResources.SARIF2005_ProvideHelpfulToolInformation_Warning_UseNumericToolVersions_Text), | ||
version); | ||
} | ||
} | ||
} | ||
} |
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.
Our convention is to put the
System
usings first. #Closed