-
Notifications
You must be signed in to change notification settings - Fork 86
/
GH1003.ProvideRequiredRegionProperties.cs
95 lines (82 loc) · 4.52 KB
/
GH1003.ProvideRequiredRegionProperties.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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 System.Linq;
using Microsoft.Json.Pointer;
namespace Microsoft.CodeAnalysis.Sarif.Multitool.Rules
{
public class ProvideRequiredRegionProperties : SarifValidationSkimmerBase
{
public ProvideRequiredRegionProperties()
{
this.DefaultConfiguration.Level = FailureLevel.Error;
}
/// <summary>
/// GH1003
/// </summary>
public override string Id => RuleId.ProvideRequiredRegionProperties;
public override HashSet<RuleKind> RuleKinds => new HashSet<RuleKind>(new[] { RuleKind.Gh });
// Every result must provide a 'region' that specifies its location with line and optional
// column information. GitHub Advanced Security code scanning can display the correct
// location only for results that provide this information. At minimum, 'region.startLine'
// is required. 'region' can also provide 'startColumn', 'endLine', and 'endColumn',
// although all of those have reasonable defaults.
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.GH1003_ProvideRequiredRegionProperties_FullDescription_Text };
protected override ICollection<string> MessageResourceNames => new List<string> {
nameof(RuleResources.GH1003_ProvideRequiredRegionProperties_Error_MissingRegion_Text),
nameof(RuleResources.GH1003_ProvideRequiredRegionProperties_Error_MissingRegionProperty_Text)
};
public override bool EnabledByDefault => false;
protected override void Analyze(Result result, string resultPointer)
{
if (!(result.Locations?.Any() == true))
{
// Rule GH1001.ProvideRequiredLocationProperties will catch this, so don't
// report it here.
return;
}
for (int i = 0; i < result.Locations.Count; i++)
{
Location location = result.Locations[i];
PhysicalLocation physicalLocation = location.PhysicalLocation;
if (physicalLocation == null)
{
// Rule GH1001.ProvideRequiredLocationProperties will catch this, so don't
// report it here.
continue;
}
if (physicalLocation.Region == null)
{
string physicalLocationPointer = resultPointer
.AtProperty(SarifPropertyName.Locations)
.AtIndex(i)
.AtProperty(SarifPropertyName.PhysicalLocation);
// {0}: The 'region' property is absent. GitHub Advanced Security code scanning
// can display the correct location only for results that provide a 'region'
// object with line and optional column information. At minimum,
// 'region.startLine' is required. 'region' can also provide 'startColumn',
// 'endLine', and 'endColumn', although all of those have reasonable defaults.
LogResult(
physicalLocationPointer,
nameof(RuleResources.GH1003_ProvideRequiredRegionProperties_Error_MissingRegion_Text));
}
else if (physicalLocation.Region.StartLine == 0)
{
string regionPointer = resultPointer
.AtProperty(SarifPropertyName.Locations)
.AtIndex(i)
.AtProperty(SarifPropertyName.PhysicalLocation)
.AtProperty(SarifPropertyName.Region);
// {0}: The 'startLine' property is absent. GitHub Advanced Security code
// scanning can display the correct location only for results that provide a
// 'region' object with line and optional column information. At minimum,
// 'region.startLine' is required. 'region' can also provide 'startColumn',
// 'endLine', and 'endColumn', although all of those have reasonable defaults.
LogResult(
regionPointer,
nameof(RuleResources.GH1003_ProvideRequiredRegionProperties_Error_MissingRegionProperty_Text));
}
}
}
}
}