@@ -2,16 +2,79 @@ package sarif_test
22
33import (
44 "bytes"
5+ "encoding/json"
6+ "fmt"
7+ "io"
8+ "net/http"
59 "regexp"
10+ "sync"
11+ "time"
612
713 . "github.com/onsi/ginkgo/v2"
814 . "github.com/onsi/gomega"
15+ "github.com/santhosh-tekuri/jsonschema/v5"
916
1017 "github.com/securego/gosec/v2"
1118 "github.com/securego/gosec/v2/issue"
1219 "github.com/securego/gosec/v2/report/sarif"
1320)
1421
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+ body , err := io .ReadAll (resp .Body )
45+ if err != nil {
46+ sarifSchemaErr = fmt .Errorf ("read sarif schema: %w" , err )
47+ return
48+ }
49+
50+ compiler := jsonschema .NewCompiler ()
51+ if err := compiler .AddResource ("sarif-schema.json" , bytes .NewReader (body )); err != nil {
52+ sarifSchemaErr = fmt .Errorf ("compile sarif schema: %w" , err )
53+ return
54+ }
55+
56+ sarifSchema , sarifSchemaErr = compiler .Compile ("sarif-schema.json" )
57+ })
58+
59+ if sarifSchemaErr != nil {
60+ return sarifSchemaErr
61+ }
62+
63+ // Marshal the report to JSON
64+ v , err := json .Marshal (report )
65+ if err != nil {
66+ return err
67+ }
68+
69+ // Unmarshal into interface{} for schema validation
70+ var data interface {}
71+ if err := json .Unmarshal (v , & data ); err != nil {
72+ return err
73+ }
74+
75+ return sarifSchema .Validate (data )
76+ }
77+
1578var _ = Describe ("Sarif Formatter" , func () {
1679 BeforeEach (func () {
1780 })
@@ -23,6 +86,9 @@ var _ = Describe("Sarif Formatter", func() {
2386 result := buf .String ()
2487 Expect (err ).ShouldNot (HaveOccurred ())
2588 Expect (result ).To (ContainSubstring ("\" results\" : [" ))
89+ sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
90+ Expect (err ).ShouldNot (HaveOccurred ())
91+ Expect (validateSarifSchema (sarifReport )).To (Succeed ())
2692 })
2793
2894 It ("sarif formatted report should contain the suppressed results" , func () {
@@ -57,6 +123,9 @@ var _ = Describe("Sarif Formatter", func() {
57123
58124 hasSuppressions , _ := regexp .MatchString (`"suppressions": \[(\s*){` , result )
59125 Expect (hasSuppressions ).To (BeTrue ())
126+ sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
127+ Expect (err ).ShouldNot (HaveOccurred ())
128+ Expect (validateSarifSchema (sarifReport )).To (Succeed ())
60129 })
61130 It ("sarif formatted report should contain the formatted one line code snippet" , func () {
62131 ruleID := "G101"
@@ -84,6 +153,7 @@ var _ = Describe("Sarif Formatter", func() {
84153 sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
85154 Expect (err ).ShouldNot (HaveOccurred ())
86155 Expect (sarifReport .Runs [0 ].Results [0 ].Locations [0 ].PhysicalLocation .Region .Snippet .Text ).Should (Equal (expectedCode ))
156+ Expect (validateSarifSchema (sarifReport )).To (Succeed ())
87157 })
88158 It ("sarif formatted report should contain the formatted multiple line code snippet" , func () {
89159 ruleID := "G101"
@@ -111,6 +181,7 @@ var _ = Describe("Sarif Formatter", func() {
111181 sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
112182 Expect (err ).ShouldNot (HaveOccurred ())
113183 Expect (sarifReport .Runs [0 ].Results [0 ].Locations [0 ].PhysicalLocation .Region .Snippet .Text ).Should (Equal (expectedCode ))
184+ Expect (validateSarifSchema (sarifReport )).To (Succeed ())
114185 })
115186 It ("sarif formatted report should have proper rule index" , func () {
116187 rules := []string {"G404" , "G101" , "G102" , "G103" }
@@ -171,6 +242,7 @@ var _ = Describe("Sarif Formatter", func() {
171242 driverRuleIndexes [rule .ID ] = ruleIndex
172243 }
173244 Expect (resultRuleIndexes ).Should (Equal (driverRuleIndexes ))
245+ Expect (validateSarifSchema (sarifReport )).To (Succeed ())
174246 })
175247 })
176248})
0 commit comments