-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsarif.go
More file actions
177 lines (154 loc) · 4.04 KB
/
Copy pathsarif.go
File metadata and controls
177 lines (154 loc) · 4.04 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
176
177
package sight
import (
"encoding/json"
"fmt"
)
// SARIF 2.1.0 output structures for sight 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"`
}
type sarifResult struct {
RuleID string `json:"ruleId"`
Level string `json:"level"`
Message sarifMessage `json:"message"`
Locations []sarifLocation `json:"locations,omitempty"`
}
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"`
EndLine int `json:"endLine,omitempty"`
StartColumn int `json:"startColumn,omitempty"`
}
// GenerateSARIF produces a SARIF 2.1.0 JSON report from sight findings.
func GenerateSARIF(findings []Finding, version string) string {
if version == "" {
version = "dev"
}
// Build rules from unique concern names
ruleSet := make(map[string]bool)
var rules []sarifRule
for _, f := range findings {
concernID := f.Concern
if concernID == "" {
concernID = "unknown"
}
if ruleSet[concernID] {
continue
}
ruleSet[concernID] = true
rule := sarifRule{
ID: concernID,
Name: concernID,
ShortDescription: sarifMessage{Text: fmt.Sprintf("%s check", concernID)},
}
rules = append(rules, rule)
}
var results []sarifResult
for _, f := range findings {
concernID := f.Concern
if concernID == "" {
concernID = "unknown"
}
level := severityToSARIFLevel(f.Severity)
msg := f.Message
if f.Fix != "" {
msg += "\n\nFix: " + f.Fix
}
if f.Reasoning != "" {
msg += "\n\nReasoning: " + f.Reasoning
}
result := sarifResult{
RuleID: concernID,
Level: level,
Message: sarifMessage{Text: msg},
}
if f.File != "" {
region := &sarifRegion{}
if f.Line > 0 {
region.StartLine = f.Line
}
if f.EndLine > 0 && f.EndLine != f.Line {
region.EndLine = f.EndLine
}
// Only set region if we have at least a start line
if region.StartLine == 0 {
region = nil
}
loc := sarifLocation{
PhysicalLocation: &sarifPhysicalLocation{
ArtifactLocation: sarifArtifactLocation{URI: f.File},
Region: region,
},
}
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: "sight",
Version: version,
InformationURI: "https://github.com/GrayCodeAI/sight",
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"
}
}