11package sarif_test
22
33import (
4- "bufio"
54 "bytes"
6- "encoding/json"
7- "fmt"
8- "net/http"
95 "regexp"
10- "sync"
11- "time"
126
137 . "github.com/onsi/ginkgo/v2"
148 . "github.com/onsi/gomega"
15- "github.com/santhosh-tekuri/jsonschema/v6"
16-
179 "github.com/securego/gosec/v2"
1810 "github.com/securego/gosec/v2/issue"
1911 "github.com/securego/gosec/v2/report/sarif"
2012)
2113
22- var (
23- sarifSchemaOnce sync.Once
24- sarifSchema * jsonschema.Schema
25- sarifSchemaErr error
26- sarifSchemaClient = & http.Client {Timeout : 30 * time .Second }
27- )
28-
29- func validateSarifSchema (report * sarif.Report ) error {
30- GinkgoHelper ()
31- sarifSchemaOnce .Do (func () {
32- resp , err := sarifSchemaClient .Get (sarif .Schema )
33- if err != nil {
34- sarifSchemaErr = fmt .Errorf ("fetch sarif schema: %w" , err )
35- return
36- }
37- defer resp .Body .Close ()
38-
39- if resp .StatusCode != http .StatusOK {
40- sarifSchemaErr = fmt .Errorf ("fetch sarif schema: unexpected status %s" , resp .Status )
41- return
42- }
43-
44- schema , err := jsonschema .UnmarshalJSON (resp .Body )
45- if err != nil {
46- sarifSchemaErr = fmt .Errorf ("error unmarshaling schema: %w" , err )
47- return
48- }
49-
50- compiler := jsonschema .NewCompiler ()
51- if err := compiler .AddResource (sarif .Schema , schema ); err != nil {
52- sarifSchemaErr = fmt .Errorf ("compile sarif schema: %w" , err )
53- return
54- }
55-
56- sarifSchema , sarifSchemaErr = compiler .Compile (sarif .Schema )
57- })
58-
59- if sarifSchemaErr != nil {
60- return sarifSchemaErr
61- }
62-
63- // Marshal the report to JSON
64- v , err := json .MarshalIndent (report , "" , "\t " )
65- if err != nil {
66- return err
67- }
68-
69- // Unmarshal into any for schema validation
70- data , err := jsonschema .UnmarshalJSON (bufio .NewReader (bytes .NewReader (v )))
71- if err != nil {
72- return err
73- }
74-
75- return sarifSchema .Validate (data )
76- }
77-
7814var _ = Describe ("Sarif Formatter" , func () {
7915 BeforeEach (func () {
8016 })
@@ -91,6 +27,40 @@ var _ = Describe("Sarif Formatter", func() {
9127 Expect (validateSarifSchema (sarifReport )).To (Succeed ())
9228 })
9329
30+ It ("sarif formatted report should contain proper autofix" , func () {
31+ ruleID := "G101"
32+ cwe := issue .GetCweByRule (ruleID )
33+ autofixIssue := []* issue.Issue {
34+ {
35+ File : "/home/src/project/test.go" ,
36+ Line : "1" ,
37+ Col : "1" ,
38+ RuleID : ruleID ,
39+ What : "test" ,
40+ Confidence : issue .High ,
41+ Severity : issue .High ,
42+ Code : "1: testcode" ,
43+ Cwe : cwe ,
44+ Suppressions : []issue.SuppressionInfo {
45+ {
46+ Kind : "inSource" ,
47+ Justification : "justification" ,
48+ },
49+ },
50+ Autofix : "some random autofix" ,
51+ },
52+ }
53+ reportInfo := gosec .NewReportInfo (autofixIssue , & gosec.Metrics {}, map [string ][]gosec.Error {}).WithVersion ("v2.7.0" )
54+ buf := new (bytes.Buffer )
55+ err := sarif .WriteReport (buf , reportInfo , []string {})
56+ result := buf .String ()
57+ Expect (err ).ShouldNot (HaveOccurred ())
58+ Expect (result ).To (ContainSubstring ("\" results\" : [" ))
59+ sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
60+ Expect (err ).ShouldNot (HaveOccurred ())
61+ Expect (validateSarifSchema (sarifReport )).To (Succeed ())
62+ })
63+
9464 It ("sarif formatted report should contain the suppressed results" , func () {
9565 ruleID := "G101"
9666 cwe := issue .GetCweByRule (ruleID )
0 commit comments