-
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
GHAS validation rule - Message must be flattened #2580
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
911db71
Add a GitHub validation rule.
yongyan-gh 7676817
Fix tests
yongyan-gh babd930
update rule name to FlatternResultMessage
yongyan-gh 48b91d3
Fix CodeQL warning
yongyan-gh 5518206
Update release history
yongyan-gh 1c3f70c
Rename to ProvideFullyFormattedMessageStrings
yongyan-gh 2b37341
rename rule to ProvideFullyFormattedMessageStrings
yongyan-gh e33751f
Merge branch 'main' into user/yongyan-gh/ghFlatternMessageRule
michaelcfanning 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
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
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
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
44 changes: 44 additions & 0 deletions
44
src/Sarif.Multitool.Library/Rules/GH1007.FlattenResultMessage.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,44 @@ | ||
// 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 FlattenResultMessage : SarifValidationSkimmerBase | ||
{ | ||
public override ReportingConfiguration DefaultConfiguration => new ReportingConfiguration | ||
{ | ||
Level = FailureLevel.Error, | ||
Enabled = this.EnabledByDefault | ||
}; | ||
|
||
/// <summary> | ||
/// GH1007 | ||
/// </summary> | ||
public override string Id => RuleId.FlattenResultMessage; | ||
|
||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.GH1007_FlattenResultMessage_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.GH1007_FlattenResultMessage_Error_Default_Text) | ||
}; | ||
|
||
public override bool EnabledByDefault => false; | ||
|
||
protected override void Analyze(Result result, string resultPointer) | ||
{ | ||
if (string.IsNullOrEmpty(result.Message.Text)) | ||
{ | ||
// {0}: The 'text' property of this result message is absent. GitHub Advanced Security code | ||
// scanning will reject this file because it does not support the argumented message now. | ||
// Try to populate the flattened message text in 'message.text' property. | ||
LogResult( | ||
resultPointer.AtProperty(SarifPropertyName.Message), | ||
nameof(RuleResources.GH1007_FlattenResultMessage_Error_Default_Text)); | ||
} | ||
} | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/Sarif.Multitool.Library/Rules/RuleResources.Designer.cs
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
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
72 changes: 72 additions & 0 deletions
72
...tData/Multitool/ValidateCommand/ExpectedOutputs/GH1007.FlattenResultMessage_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,72 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "SARIF Functional Testing", | ||
"rules": [ | ||
{ | ||
"id": "GH1007", | ||
"name": "FlattenResultMessage", | ||
"fullDescription": { | ||
"text": "GitHub Advanced Security code scanning will reject a SARIF file that express result messages with 'message.id' and 'message.arguments' but without the 'message.text' property since the arugmented message format is not supported yet. Please populate the flattened message text in 'message.text' property." | ||
}, | ||
"messageStrings": { | ||
"Error_Default": { | ||
"text": "{0}: The 'text' property of this result message is absent. GitHub Advanced Security code scanning will reject this file because it does not support the argumented message now. Try to populate the flattened message text in 'message.text' property." | ||
} | ||
}, | ||
"defaultConfiguration": { | ||
"enabled": false, | ||
"level": "error" | ||
}, | ||
"helpUri": "http://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html" | ||
} | ||
] | ||
} | ||
}, | ||
"invocations": [ | ||
{ | ||
"executionSuccessful": true | ||
} | ||
], | ||
"artifacts": [ | ||
{ | ||
"location": { | ||
"uri": "FunctionalTestOutput.ValidateCommand/GH1007.FlattenResultMessage_Invalid.sarif", | ||
"uriBaseId": "TEST_DIR" | ||
} | ||
} | ||
], | ||
"results": [ | ||
{ | ||
"ruleId": "GH1007", | ||
"ruleIndex": 0, | ||
"level": "error", | ||
"message": { | ||
"id": "Error_Default", | ||
"arguments": [ | ||
"runs[0].results[0].message" | ||
] | ||
}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"index": 0 | ||
}, | ||
"region": { | ||
"startLine": 29, | ||
"startColumn": 22 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
28 changes: 28 additions & 0 deletions
28
...estData/Multitool/ValidateCommand/ExpectedOutputs/GH1007.FlattenResultMessage_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.6.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "SARIF Functional Testing" | ||
} | ||
}, | ||
"invocations": [ | ||
{ | ||
"executionSuccessful": true | ||
} | ||
], | ||
"artifacts": [ | ||
{ | ||
"location": { | ||
"uri": "FunctionalTestOutput.ValidateCommand/GH1007.FlattenResultMessage_Valid.sarif", | ||
"uriBaseId": "TEST_DIR" | ||
} | ||
} | ||
], | ||
"results": [], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
41 changes: 41 additions & 0 deletions
41
...Sarif/TestData/Multitool/ValidateCommand/Inputs/GH1007.FlattenResultMessage_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,41 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "SARIF Functional Testing", | ||
"version": "1.2.3", | ||
"rules": [ | ||
{ | ||
"id": "TEST1001", | ||
"fullDescription": { | ||
"text": "Argumented message." | ||
}, | ||
"messageStrings": { | ||
"DoesExist": { | ||
"text": "'{0}' is an apparent access token of '{1}'." | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST1001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"id": "DoesExist", | ||
"arguments": [ | ||
"123456789", | ||
"Alibaba Cloud service" | ||
] | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
31 changes: 31 additions & 0 deletions
31
...s.Sarif/TestData/Multitool/ValidateCommand/Inputs/GH1007.FlattenResultMessage_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,31 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "SARIF Functional Testing", | ||
"version": "1.2.3", | ||
"rules": [ | ||
{ | ||
"id": "TEST1001", | ||
"fullDescription": { | ||
"text": "Argumented message." | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST1001", | ||
"message": { | ||
"text": "'123456789' is an apparent access token of 'Alibaba Cloud service'." | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
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.
So now we can't run an analysis without producing errors? Either we recommend using format string args or we object to the absence of flattened messages?
Ideally, the conflicting rules would turn off if the other one is being enforced.
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.
how about adding a Set property to
Skimmer
class, e.g.ConflictSkimmerSet
. InAnalyzeCommandBase.AnalyzeTarget()
method, iterates available skimmer and add theConflictSkimmerSet
value todisabledSkimmers
set if has value.this way the rule author can override existing old rule conflicts with the new rule.
what do you think?
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.
Interesting! And so the skimmers just need a dependency on rule ids it replaces. Yes, I think this could work.
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.
OK, so I thought about your idea a bit more.
Rules should be able to declare an 'IncompatibleRuleIds' set or something similar, All the rule does is to publish this data.
Then we update the analysis engine, which, when instantiating skimmers, makes sure that no new enabled skimmers have a rule id that conflicts with the set of loaded skimmers so far. The analysis engine could raise an error-level configuration notification and exit.
This approach would require users to properly configure analysis to account for the configuration problem. I think everything would drop in pretty cleanly by doing this, we need a low-level update to rules metadata and a simple update to the default skimmer loading behavior.
btw - I'm approving this change, we can take on the above or continue to refine the idea in further work items/PRs.
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.
thanks created the issue #2591
regarding "The analysis engine could raise an error-level configuration notification and exit.", should we raise an configuration notification error and disable the incompatible rules then continue the analysis instead exiting?
am thinking about the web validator scenario, user can run only SARIF validator rules, or all SARIF + GH validator rules, if adding a new GH rule which is not compatible with a SARIF rule, we may raise the configuration error and continue analysis without a config file change. If exits right away, the web validator stops getting results until config file is fixed.