-
Notifications
You must be signed in to change notification settings - Fork 93
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 SARIF2006 #1942
Merged
Merged
Adding Rule SARIF2006 #1942
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
65ee2f6
Adding rule SARIF2006
eddynaka d8ad7e4
rollback tests and adding absolute kind
eddynaka 4a2ad82
Disable SARIF.2006 by default.
885071e
Merge branch 'features/sarif-validation-rules' into users/ednakamu/sa…
ce76252
Test infrastructure: enable rules to be turned on/off per test.
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
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
105 changes: 105 additions & 0 deletions
105
src/Sarif.Multitool/Rules/SARIF2006.UrisShouldBeReachable.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,105 @@ | ||
// 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.Net.Http; | ||
|
||
using Microsoft.Json.Pointer; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool.Rules | ||
{ | ||
public class UrisShouldBeReachable : SarifValidationSkimmerBase | ||
{ | ||
/// <summary> | ||
/// SARIF2006 | ||
/// </summary> | ||
public override string Id => RuleId.UrisShouldBeReachable; | ||
|
||
/// <summary> | ||
/// Placeholder | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF2006_UrisShouldBeReachable_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.SARIF2006_UrisShouldBeReachable_Warning_Default_Text) | ||
}; | ||
|
||
public override FailureLevel DefaultLevel => FailureLevel.Warning; | ||
|
||
private static readonly HttpClient s_httpClient = new HttpClient(); | ||
private static readonly Dictionary<string, bool> s_checkedUris = new Dictionary<string, bool>(); | ||
|
||
protected override void Analyze(SarifLog log, string logPointer) | ||
{ | ||
AnalyzeUri(log.SchemaUri, logPointer.AtProperty(SarifPropertyName.Schema)); | ||
} | ||
|
||
protected override void Analyze(Result result, string resultPointer) | ||
{ | ||
if (result.WorkItemUris != null) | ||
{ | ||
Uri[] workItemUris = result.WorkItemUris.ToArray(); | ||
string workItemUrisPointer = resultPointer.AtProperty(SarifPropertyName.WorkItemUris); | ||
|
||
for (int i = 0; i < workItemUris.Length; ++i) | ||
{ | ||
AnalyzeUri(workItemUris[i], workItemUrisPointer.AtIndex(i)); | ||
} | ||
} | ||
} | ||
|
||
protected override void Analyze(ReportingDescriptor reportingDescriptor, string reportingDescriptorPointer) | ||
{ | ||
AnalyzeUri(reportingDescriptor.HelpUri, reportingDescriptorPointer.AtProperty(SarifPropertyName.HelpUri)); | ||
} | ||
|
||
protected override void Analyze(ToolComponent toolComponent, string toolComponentPointer) | ||
{ | ||
AnalyzeUri(toolComponent.DownloadUri, toolComponentPointer.AtProperty(SarifPropertyName.DownloadUri)); | ||
} | ||
|
||
protected override void Analyze(VersionControlDetails versionControlDetails, string versionControlDetailsPointer) | ||
{ | ||
AnalyzeUri(versionControlDetails.RepositoryUri, versionControlDetailsPointer.AtProperty(SarifPropertyName.RepositoryUri)); | ||
} | ||
|
||
private void AnalyzeUri(Uri uri, string pointer) | ||
{ | ||
AnalyzeUri(uri?.OriginalString, pointer); | ||
} | ||
|
||
private void AnalyzeUri(string uriString, string pointer) | ||
{ | ||
// If it's not a well-formed URI of _any_ kind, then don't bother triggering this rule. | ||
// Rule SARIF1003, UrisMustBeValid, will catch it. | ||
// Check for well-formedness first, before attempting to create a Uri object, to | ||
// avoid having to do a try/catch. Unfortunately Uri.TryCreate will return true | ||
// even for a malformed URI string. | ||
if (uriString != null && Uri.IsWellFormedUriString(uriString, UriKind.RelativeOrAbsolute)) | ||
{ | ||
// Ok, it's a well-formed URI of some kind. If it's not absolute, _now_ we | ||
// can report it. | ||
Uri uri = new Uri(uriString, UriKind.RelativeOrAbsolute); | ||
if (uri.IsAbsoluteUri && !IsUriReachable(uri.AbsoluteUri)) | ||
{ | ||
// Placeholder | ||
LogResult(pointer, nameof(RuleResources.SARIF1005_UriMustBeAbsolute_Error_Default_Text), uriString); | ||
} | ||
} | ||
} | ||
|
||
private bool IsUriReachable(string uriString) | ||
{ | ||
if (s_checkedUris.ContainsKey(uriString)) | ||
{ | ||
return s_checkedUris[uriString]; | ||
} | ||
|
||
HttpResponseMessage httpResponseMessage = s_httpClient.GetAsync(uriString).GetAwaiter().GetResult(); | ||
s_checkedUris.Add(uriString, httpResponseMessage.IsSuccessStatusCode); | ||
return httpResponseMessage.IsSuccessStatusCode; | ||
} | ||
} | ||
} |
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
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.
You can simplify this logic because if it's not absolute, SARIF1005.UriMustBeAbsolute will catch it. So:
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.
If i do that and the uri is not absolute, we will have a null exception. It's why I added. Unless I use the uriString
In reply to: 446547297 [](ancestors = 446547297)
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.
As we discussed: I now use
UriKind.Absolute
in the call toIsWellFormedUriString
so it should work.In reply to: 446547509 [](ancestors = 446547509,446547297)