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 SARIF2005 #1926

Merged
9 commits merged into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/Sarif.Multitool/Rules/RuleId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class RuleId
public const string ReferenceFinalSchema = "SARIF1011";

public const string AuthorHighQualityMessages = "SARIF2001";
public const string ProvideHelpfulToolInformation = "SARIF2005";
public const string ProvideSchema = "SARIF2008";
}
}
36 changes: 36 additions & 0 deletions src/Sarif.Multitool/Rules/RuleResources.Designer.cs

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

12 changes: 12 additions & 0 deletions src/Sarif.Multitool/Rules/RuleResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@
<data name="SARIF1007_RegionPropertiesMustBeConsistent_FullDescription_Text" xml:space="preserve">
<value>Placeholder_SARIF1007_RegionPropertiesMustBeConsistent_FullDescription_Text</value>
</data>
<data name="SARIF2005_ProvideHelpfulToolInformation_FullDescription_Text" xml:space="preserve">
<value>Placeholder</value>
</data>
<data name="SARIF2005_ProvideHelpfulToolInformation_Warning_ProvideConciseToolName_Text" xml:space="preserve">
<value>{0}: Placeholder '{1}' '{2}' '{3}'</value>
</data>
<data name="SARIF2005_ProvideHelpfulToolInformation_Warning_ProvideToolVersion_Text" xml:space="preserve">
<value>{0}: Placeholder</value>
</data>
<data name="SARIF2005_ProvideHelpfulToolInformation_Warning_UseNumericToolVersions_Text" xml:space="preserve">
<value>{0}: Placeholder '{1}'</value>
</data>
</root>
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;
Copy link

@ghost ghost Jun 23, 2020

Choose a reason for hiding this comment

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

Generic [](start = 25, length = 7)

Our convention is to put the System usings first. #Closed

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),
Copy link

@ghost ghost Jun 24, 2020

Choose a reason for hiding this comment

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

SARIF2005_ProvideHelpfulToolInformation_Warning_ProvideConciseToolName_Text [](start = 45, length = 75)

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);
}
}
}
}
3 changes: 3 additions & 0 deletions src/Sarif.Multitool/SarifPropertyName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static class SarifPropertyName
public const string Markdown = "markdown";
public const string Message = "message";
public const string MessageStrings = "messageStrings";
public const string Name = "name";
public const string Nodes = "nodes";
public const string NotificationConfigurationOverrides = "notificationConfigurationOverrides";
public const string Notifications = "notifications";
Expand All @@ -74,6 +75,7 @@ public static class SarifPropertyName
public const string RunGraphIndex = "runGraphIndex";
public const string Runs = "runs";
public const string Schema = "$schema";
public const string SemanticVersion = "semanticVersion";
public const string ShortDescription = "shortDescription";
public const string Stacks = "stacks";
public const string Stdin = "stdin";
Expand All @@ -90,6 +92,7 @@ public static class SarifPropertyName
public const string ToolConfigurationNotifications = "toolConfigurationNotifications";
public const string ToolExecutionNotifications = "toolExecutionNotifications";
public const string Uri = "uri";
public const string Version = "version";
public const string VersionControlProvenance = "versionControlProvenance";
public const string WebRequest = "webRequest";
public const string WebRequests = "webRequests";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,20 @@ public void SARIF1011_ReferenceFinalSchema_Invalid()

[Fact]
public void SARIF2001_AuthorHighQualityMessages_Valid()
=> RunTest(MakeValidTestFileName(RuleId.AuthorHighQualityMessages, nameof(RuleId.AuthorHighQualityMessages)));
=> RunTest(MakeValidTestFileName(RuleId.AuthorHighQualityMessages, nameof(RuleId.AuthorHighQualityMessages)));

[Fact]
public void SARIF2001_AuthorHighQualityMessages_Invalid()
=> RunTest(MakeInvalidTestFileName(RuleId.AuthorHighQualityMessages, nameof(RuleId.AuthorHighQualityMessages)));

[Fact]
public void SARIF2005_ProvideHelpfulToolInformation_Valid()
=> RunTest(MakeValidTestFileName(RuleId.ProvideHelpfulToolInformation, nameof(RuleId.ProvideHelpfulToolInformation)));

[Fact]
public void SARIF2005_ProvideHelpfulToolInformation_Invalid()
=> RunTest(MakeInvalidTestFileName(RuleId.ProvideHelpfulToolInformation, nameof(RuleId.ProvideHelpfulToolInformation)));

[Fact]
public void SARIF2008_ProvideSchema_Valid()
=> RunTest(MakeValidTestFileName(RuleId.ProvideSchema, nameof(RuleId.ProvideSchema)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"index": 0
},
"region": {
"startLine": 10,
"startLine": 11,
"startColumn": 13
}
}
Expand All @@ -81,7 +81,7 @@
"index": 0
},
"region": {
"startLine": 14,
"startLine": 15,
"startColumn": 13
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"index": 0
},
"region": {
"startLine": 35,
"startLine": 36,
"startColumn": 49
}
}
Expand All @@ -124,7 +124,7 @@
"index": 0
},
"region": {
"startLine": 57,
"startLine": 58,
"startColumn": 54
}
}
Expand All @@ -149,7 +149,7 @@
"index": 0
},
"region": {
"startLine": 54,
"startLine": 55,
"startColumn": 43
}
}
Expand All @@ -174,7 +174,7 @@
"index": 0
},
"region": {
"startLine": 41,
"startLine": 42,
"startColumn": 43
}
}
Expand All @@ -199,7 +199,7 @@
"index": 0
},
"region": {
"startLine": 9,
"startLine": 10,
"startColumn": 82
}
}
Expand All @@ -224,7 +224,7 @@
"index": 0
},
"region": {
"startLine": 13,
"startLine": 14,
"startColumn": 69
}
}
Expand All @@ -249,7 +249,7 @@
"index": 0
},
"region": {
"startLine": 17,
"startLine": 18,
"startColumn": 69
}
}
Expand All @@ -274,7 +274,7 @@
"index": 0
},
"region": {
"startLine": 23,
"startLine": 24,
"startColumn": 69
}
}
Expand All @@ -299,7 +299,7 @@
"index": 0
},
"region": {
"startLine": 30,
"startLine": 31,
"startColumn": 60
}
}
Expand Down
Loading