Skip to content

Commit 6c57ae1

Browse files
authored
Fix sarif formatting issues (#565)
* include tool version * change declared safix shema version * dedup rules, fix result locations * refactor rules collection creation
1 parent b6524ce commit 6c57ae1

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

output/formatter.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,39 @@ func convertToSonarIssues(rootPaths []string, data *reportInfo) (*sonarIssues, e
180180
func convertToSarifReport(rootPaths []string, data *reportInfo) (*sarifReport, error) {
181181
sr := buildSarifReport()
182182

183-
var rules []*sarifRule
184-
var locations []*sarifLocation
183+
type rule struct {
184+
index int
185+
rule *sarifRule
186+
}
187+
188+
rules := make([]*sarifRule, 0)
189+
rulesIndices := make(map[string]rule)
190+
lastRuleIndex := -1
191+
185192
results := []*sarifResult{}
186193

187-
for index, issue := range data.Issues {
188-
rules = append(rules, buildSarifRule(issue))
194+
for _, issue := range data.Issues {
195+
r, ok := rulesIndices[issue.RuleID]
196+
if !ok {
197+
lastRuleIndex++
198+
r = rule{index: lastRuleIndex, rule: buildSarifRule(issue)}
199+
rulesIndices[issue.RuleID] = r
200+
rules = append(rules, r.rule)
201+
}
189202

190203
location, err := buildSarifLocation(issue, rootPaths)
191204
if err != nil {
192205
return nil, err
193206
}
194-
locations = append(locations, location)
195207

196208
result := &sarifResult{
197-
RuleID: fmt.Sprintf("%s (CWE-%s)", issue.RuleID, issue.Cwe.ID),
198-
RuleIndex: index,
209+
RuleID: r.rule.ID,
210+
RuleIndex: r.index,
199211
Level: getSarifLevel(issue.Severity.String()),
200212
Message: &sarifMessage{
201213
Text: issue.What,
202214
},
203-
Locations: locations,
215+
Locations: []*sarifLocation{location},
204216
}
205217

206218
results = append(results, result)
@@ -209,6 +221,7 @@ func convertToSarifReport(rootPaths []string, data *reportInfo) (*sarifReport, e
209221
tool := &sarifTool{
210222
Driver: &sarifDriver{
211223
Name: "gosec",
224+
Version: "2.1.0",
212225
InformationURI: "https://github.com/securego/gosec/",
213226
Rules: rules,
214227
},

output/sarif_format.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package output
22

33
import (
44
"fmt"
5-
"github.com/securego/gosec/v2"
65
"strconv"
76
"strings"
7+
8+
"github.com/securego/gosec/v2"
89
)
910

1011
type sarifLevel string
@@ -68,6 +69,7 @@ type sarifResult struct {
6869

6970
type sarifDriver struct {
7071
Name string `json:"name"`
72+
Version string `json:"version"`
7173
InformationURI string `json:"informationUri"`
7274
Rules []*sarifRule `json:"rules,omitempty"`
7375
}
@@ -91,7 +93,7 @@ type sarifReport struct {
9193
func buildSarifReport() *sarifReport {
9294
return &sarifReport{
9395
Version: "2.1.0",
94-
Schema: "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.4.json",
96+
Schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
9597
Runs: []*sarifRun{},
9698
}
9799
}

0 commit comments

Comments
 (0)