Skip to content

Commit 549f8db

Browse files
committed
test: add sarif validation
1 parent c34cbbf commit 549f8db

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/mozilla/tls-observatory v0.0.0-20250923143331-eef96233227e
1010
github.com/onsi/ginkgo/v2 v2.27.2
1111
github.com/onsi/gomega v1.38.2
12+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
1213
github.com/stretchr/testify v1.11.1
1314
golang.org/x/crypto v0.43.0
1415
golang.org/x/text v0.30.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWN
340340
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
341341
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
342342
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
343+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
344+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
343345
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
344346
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
345347
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=

report/sarif/sarif_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,79 @@ package sarif_test
22

33
import (
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+
1578
var _ = 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

Comments
 (0)