-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsarif.go
More file actions
175 lines (149 loc) · 4.03 KB
/
Copy pathsarif.go
File metadata and controls
175 lines (149 loc) · 4.03 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package inspect
import (
"encoding/json"
"fmt"
)
// SARIF 2.1.0 output structures for inspect findings.
type sarifLog struct {
Version string `json:"version"`
Schema string `json:"$schema"`
Runs []sarifRun `json:"runs"`
}
type sarifRun struct {
Tool sarifTool `json:"tool"`
Results []sarifResult `json:"results"`
}
type sarifTool struct {
Driver sarifDriver `json:"driver"`
}
type sarifDriver struct {
Name string `json:"name"`
Version string `json:"version"`
InformationURI string `json:"informationUri,omitempty"`
Rules []sarifRule `json:"rules,omitempty"`
}
type sarifRule struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
ShortDescription sarifMessage `json:"shortDescription"`
HelpURI string `json:"helpUri,omitempty"`
Relationships []sarifRelationship `json:"relationships,omitempty"`
}
type sarifRelationship struct {
Target sarifDescriptorReference `json:"target"`
}
type sarifDescriptorReference struct {
ID string `json:"id"`
ToolComponent *sarifComponentReference `json:"toolComponent,omitempty"`
}
type sarifComponentReference struct {
Name string `json:"name"`
}
type sarifResult struct {
RuleID string `json:"ruleId"`
Level string `json:"level"`
Message sarifMessage `json:"message"`
Locations []sarifLocation `json:"locations"`
}
type sarifMessage struct {
Text string `json:"text"`
}
type sarifLocation struct {
PhysicalLocation *sarifPhysicalLocation `json:"physicalLocation,omitempty"`
}
type sarifPhysicalLocation struct {
ArtifactLocation sarifArtifactLocation `json:"artifactLocation"`
Region *sarifRegion `json:"region,omitempty"`
}
type sarifArtifactLocation struct {
URI string `json:"uri"`
}
type sarifRegion struct {
StartLine int `json:"startLine,omitempty"`
StartColumn int `json:"startColumn,omitempty"`
}
// GenerateSARIF produces a SARIF 2.1.0 JSON report from scan findings.
func GenerateSARIF(findings []Finding, version string) string {
if version == "" {
version = "dev"
}
// Build rules from unique check names
ruleSet := make(map[string]bool)
var rules []sarifRule
for _, f := range findings {
checkID := f.Check
if checkID == "" {
checkID = "unknown"
}
if ruleSet[checkID] {
continue
}
ruleSet[checkID] = true
rule := sarifRule{
ID: checkID,
Name: checkID,
ShortDescription: sarifMessage{Text: fmt.Sprintf("%s check", checkID)},
}
rules = append(rules, rule)
}
var results []sarifResult
for _, f := range findings {
checkID := f.Check
if checkID == "" {
checkID = "unknown"
}
level := severityToSARIFLevel(f.Severity)
msg := f.Message
if f.Evidence != "" {
msg += "\n\nEvidence: " + f.Evidence
}
result := sarifResult{
RuleID: checkID,
Level: level,
Message: sarifMessage{Text: msg},
}
if f.URL != "" {
loc := sarifLocation{
PhysicalLocation: &sarifPhysicalLocation{
ArtifactLocation: sarifArtifactLocation{URI: f.URL},
},
}
result.Locations = append(result.Locations, loc)
}
results = append(results, result)
}
log := sarifLog{
Version: "2.1.0",
Schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
Runs: []sarifRun{{
Tool: sarifTool{
Driver: sarifDriver{
Name: "inspect",
Version: version,
InformationURI: "https://github.com/GrayCodeAI/inspect",
Rules: rules,
},
},
Results: results,
}},
}
data, err := json.MarshalIndent(log, "", " ")
if err != nil {
return fmt.Sprintf(`{"error": "failed to generate SARIF: %s"}`, err.Error())
}
return string(data)
}
func severityToSARIFLevel(s Severity) string {
switch {
case s >= SeverityCritical:
return "error"
case s >= SeverityHigh:
return "error"
case s >= SeverityMedium:
return "warning"
case s >= SeverityLow:
return "note"
default:
return "none"
}
}