@@ -12,20 +12,13 @@ import (
12
12
"github.com/securego/gosec/v2/issue"
13
13
)
14
14
15
- // GenerateReport Convert a gosec report to a Sarif Report
15
+ // GenerateReport converts a gosec report into a SARIF report
16
16
func GenerateReport (rootPaths []string , data * gosec.ReportInfo ) (* Report , error ) {
17
- type rule struct {
18
- index int
19
- rule * ReportingDescriptor
20
- }
21
-
22
- rules := make ([]* ReportingDescriptor , 0 )
23
- rulesIndices := make (map [string ]rule )
24
- lastRuleIndex := - 1
17
+ rules := []* ReportingDescriptor {}
25
18
26
19
results := []* Result {}
27
- cweTaxa := make ( []* ReportingDescriptor , 0 )
28
- weaknesses := make ( map [string ]* cwe.Weakness )
20
+ cweTaxa := []* ReportingDescriptor {}
21
+ weaknesses := map [string ]* cwe.Weakness {}
29
22
30
23
for _ , issue := range data .Issues {
31
24
if issue .Cwe != nil {
@@ -38,26 +31,26 @@ func GenerateReport(rootPaths []string, data *gosec.ReportInfo) (*Report, error)
38
31
}
39
32
}
40
33
41
- r , ok := rulesIndices [issue .RuleID ]
42
- if ! ok {
43
- lastRuleIndex ++
44
- r = rule {index : lastRuleIndex , rule : parseSarifRule (issue )}
45
- rulesIndices [issue .RuleID ] = r
46
- rules = append (rules , r .rule )
47
- }
34
+ rule := parseSarifRule (issue )
35
+ ruleIndex := 0
36
+ rules , ruleIndex = addRuleInOrder (rules , rule )
48
37
49
38
location , err := parseSarifLocation (issue , rootPaths )
50
39
if err != nil {
51
40
return nil , err
52
41
}
53
42
54
- result := NewResult (r .rule .ID , r .index , getSarifLevel (issue .Severity .String ()), issue .What , buildSarifSuppressions (issue .Suppressions )).
55
- WithLocations (location )
43
+ result := NewResult (
44
+ issue .RuleID ,
45
+ ruleIndex ,
46
+ getSarifLevel (issue .Severity .String ()),
47
+ issue .What ,
48
+ buildSarifSuppressions (issue .Suppressions ),
49
+ ).WithLocations (location )
56
50
57
51
results = append (results , result )
58
52
}
59
53
60
- sort .SliceStable (rules , func (i , j int ) bool { return rules [i ].ID < rules [j ].ID })
61
54
sort .SliceStable (cweTaxa , func (i , j int ) bool { return cweTaxa [i ].ID < cweTaxa [j ].ID })
62
55
63
56
tool := NewTool (buildSarifDriver (rules , data .GosecVersion ))
@@ -72,6 +65,26 @@ func GenerateReport(rootPaths []string, data *gosec.ReportInfo) (*Report, error)
72
65
WithRuns (run ), nil
73
66
}
74
67
68
+ // addRuleInOrder inserts a rule into the rules slice keeping the rules IDs order, it returns the new rules
69
+ // slice and the position where the rule was inserted
70
+ func addRuleInOrder (rules []* ReportingDescriptor , rule * ReportingDescriptor ) ([]* ReportingDescriptor , int ) {
71
+ position := 0
72
+ for i , r := range rules {
73
+ if r .ID < rule .ID {
74
+ continue
75
+ }
76
+ if r .ID == rule .ID {
77
+ return rules , i
78
+ }
79
+ position = i
80
+ break
81
+ }
82
+ rules = append (rules , nil )
83
+ copy (rules [position + 1 :], rules [position :])
84
+ rules [position ] = rule
85
+ return rules , position
86
+ }
87
+
75
88
// parseSarifRule return SARIF rule field struct
76
89
func parseSarifRule (i * issue.Issue ) * ReportingDescriptor {
77
90
cwe := issue .GetCweByRule (i .RuleID )
0 commit comments