-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsarif.go
More file actions
255 lines (223 loc) · 6.57 KB
/
sarif.go
File metadata and controls
255 lines (223 loc) · 6.57 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package utils
import (
"encoding/json"
"fmt"
"os"
)
// PylintIssue represents a single issue in Pylint's JSON output
type PylintIssue struct {
Type string `json:"type"`
Module string `json:"module"`
Obj string `json:"obj"`
Line int `json:"line"`
Column int `json:"column"`
Path string `json:"path"`
Symbol string `json:"symbol"`
Message string `json:"message"`
MessageID string `json:"message-id"`
}
// SarifReport represents the SARIF report structure
type SarifReport struct {
Version string `json:"version"`
Schema string `json:"$schema"`
Runs []Run `json:"runs"`
}
type Run struct {
Tool Tool `json:"tool"`
Results []Result `json:"results"`
}
type Tool struct {
Driver Driver `json:"driver"`
}
type Driver struct {
Name string `json:"name"`
Version string `json:"version"`
InformationURI string `json:"informationUri"`
Rules []Rule `json:"rules"`
}
type Rule struct {
ID string `json:"id"`
ShortDescription MessageText `json:"shortDescription"`
Properties RuleProperties `json:"properties"`
}
type RuleProperties struct {
Priority int `json:"priority,omitempty"`
Ruleset string `json:"ruleset,omitempty"`
Tags []string `json:"tags,omitempty"`
// Add other common properties that might be present
Precision string `json:"precision,omitempty"`
SecuritySeverity string `json:"security-severity,omitempty"`
}
type Result struct {
RuleID string `json:"ruleId"`
Level string `json:"level"`
Message MessageText `json:"message"`
Locations []Location `json:"locations"`
}
type Location struct {
PhysicalLocation PhysicalLocation `json:"physicalLocation"`
}
type PhysicalLocation struct {
ArtifactLocation ArtifactLocation `json:"artifactLocation"`
Region Region `json:"region"`
}
type ArtifactLocation struct {
URI string `json:"uri"`
}
type Region struct {
StartLine int `json:"startLine"`
StartColumn int `json:"startColumn"`
}
type MessageText struct {
Text string `json:"text"`
}
// ConvertPylintToSarif converts Pylint JSON output to SARIF format
func ConvertPylintToSarif(pylintOutput []byte) []byte {
var issues []PylintIssue
if err := json.Unmarshal(pylintOutput, &issues); err != nil {
// If parsing fails, return empty SARIF report
return createEmptySarifReport()
}
// Create SARIF report
sarifReport := SarifReport{
Version: "2.1.0",
Schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
Runs: []Run{
{
Tool: Tool{
Driver: Driver{
Name: "Pylint",
Version: "3.3.6", // TODO: Get this dynamically
InformationURI: "https://pylint.org",
},
},
Results: make([]Result, 0, len(issues)),
},
},
}
// Convert each Pylint issue to SARIF result
for _, issue := range issues {
result := Result{
RuleID: issue.Symbol,
Level: getSarifLevel(issue.Type),
Message: MessageText{
Text: issue.Message,
},
Locations: []Location{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: issue.Path,
},
Region: Region{
StartLine: issue.Line,
StartColumn: issue.Column,
},
},
},
},
}
sarifReport.Runs[0].Results = append(sarifReport.Runs[0].Results, result)
}
sarifData, err := json.MarshalIndent(sarifReport, "", " ")
if err != nil {
return createEmptySarifReport()
}
return sarifData
}
// getSarifLevel converts Pylint message type to SARIF level
func getSarifLevel(pylintType string) string {
switch pylintType {
case "error", "fatal":
return "error"
case "warning":
return "warning"
case "convention", "refactor":
return "note"
default:
return "none"
}
}
// createEmptySarifReport creates an empty SARIF report in case of errors
func createEmptySarifReport() []byte {
emptyReport := SarifReport{
Version: "2.1.0",
Schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
Runs: []Run{
{
Tool: Tool{
Driver: Driver{
Name: "Pylint",
Version: "3.3.6",
InformationURI: "https://pylint.org",
},
},
Results: []Result{},
},
},
}
sarifData, _ := json.MarshalIndent(emptyReport, "", " ")
return sarifData
}
type SimpleSarifReport struct {
Version string `json:"version"`
Schema string `json:"$schema"`
Runs []json.RawMessage `json:"runs"`
}
// MergeSarifOutputs combines multiple SARIF files into a single output file
func MergeSarifOutputs(inputFiles []string, outputFile string) error {
var mergedSarif SimpleSarifReport
mergedSarif.Version = "2.1.0"
mergedSarif.Schema = "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json"
mergedSarif.Runs = make([]json.RawMessage, 0)
for _, file := range inputFiles {
data, err := os.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
// Skip if file doesn't exist (tool might have failed)
continue
}
return fmt.Errorf("failed to read SARIF file %s: %w", file, err)
}
var sarif SimpleSarifReport
if err := json.Unmarshal(data, &sarif); err != nil {
return fmt.Errorf("failed to parse SARIF file %s: %w", file, err)
}
mergedSarif.Runs = append(mergedSarif.Runs, sarif.Runs...)
}
// Create output file
out, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer out.Close()
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
if err := encoder.Encode(mergedSarif); err != nil {
return fmt.Errorf("failed to write merged SARIF: %w", err)
}
return nil
}
// FilterRulesFromSarif removes rule definitions from SARIF output if needed
// This should be called separately after MergeSarifOutputs if rule filtering is required
func FilterRulesFromSarif(sarifData []byte) ([]byte, error) {
// Use a map to preserve all fields during unmarshaling
var report map[string]interface{}
if err := json.Unmarshal(sarifData, &report); err != nil {
return nil, fmt.Errorf("failed to parse SARIF data: %w", err)
}
// Navigate to the runs array and remove rules from each run
if runs, ok := report["runs"].([]interface{}); ok {
for _, run := range runs {
if runMap, ok := run.(map[string]interface{}); ok {
if tool, ok := runMap["tool"].(map[string]interface{}); ok {
if driver, ok := tool["driver"].(map[string]interface{}); ok {
driver["rules"] = nil
}
}
}
}
}
// Marshal back to JSON with indentation
return json.MarshalIndent(report, "", " ")
}