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"
169
1710 "github.com/securego/gosec/v2"
1811 "github.com/securego/gosec/v2/issue"
1912 "github.com/securego/gosec/v2/report/sarif"
2013)
2114
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-
7815var _ = Describe ("Sarif Formatter" , func () {
7916 BeforeEach (func () {
8017 })
@@ -91,6 +28,40 @@ var _ = Describe("Sarif Formatter", func() {
9128 Expect (validateSarifSchema (sarifReport )).To (Succeed ())
9229 })
9330
31+ It ("sarif formatted report should contain proper autofix" , func () {
32+ ruleID := "G101"
33+ cwe := issue .GetCweByRule (ruleID )
34+ autofixIssue := []* issue.Issue {
35+ {
36+ File : "/home/src/project/test.go" ,
37+ Line : "1" ,
38+ Col : "1" ,
39+ RuleID : ruleID ,
40+ What : "test" ,
41+ Confidence : issue .High ,
42+ Severity : issue .High ,
43+ Code : "1: testcode" ,
44+ Cwe : cwe ,
45+ Suppressions : []issue.SuppressionInfo {
46+ {
47+ Kind : "inSource" ,
48+ Justification : "justification" ,
49+ },
50+ },
51+ Autofix : "some random autofix" ,
52+ },
53+ }
54+ reportInfo := gosec .NewReportInfo (autofixIssue , & gosec.Metrics {}, map [string ][]gosec.Error {}).WithVersion ("v2.7.0" )
55+ buf := new (bytes.Buffer )
56+ err := sarif .WriteReport (buf , reportInfo , []string {})
57+ result := buf .String ()
58+ Expect (err ).ShouldNot (HaveOccurred ())
59+ Expect (result ).To (ContainSubstring ("\" results\" : [" ))
60+ sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
61+ Expect (err ).ShouldNot (HaveOccurred ())
62+ Expect (validateSarifSchema (sarifReport )).To (Succeed ())
63+ })
64+
9465 It ("sarif formatted report should contain the suppressed results" , func () {
9566 ruleID := "G101"
9667 cwe := issue .GetCweByRule (ruleID )
0 commit comments