mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 18:15:42 +01:00
Use github.com/santhosh-tekuri/jsonschema for JSON schema validation.
This commit is contained in:
parent
a96597206e
commit
3420ceb415
3 changed files with 53 additions and 50 deletions
|
|
@ -1,20 +1,17 @@
|
|||
package csaf
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/qri-io/jsonschema"
|
||||
"github.com/santhosh-tekuri/jsonschema/v5"
|
||||
)
|
||||
|
||||
//go:embed schema/csaf_json_schema.json
|
||||
var schema []byte
|
||||
var csafSchema []byte
|
||||
|
||||
//go:embed schema/cvss-v2.0.json
|
||||
var cvss20 []byte
|
||||
|
|
@ -25,53 +22,62 @@ var cvss30 []byte
|
|||
//go:embed schema/cvss-v3.1.json
|
||||
var cvss31 []byte
|
||||
|
||||
func embedLoader(ctx context.Context, uri *url.URL, schema *jsonschema.Schema) error {
|
||||
var (
|
||||
compileSchemaOnce sync.Once
|
||||
compileError error
|
||||
compiledSchema *jsonschema.Schema
|
||||
)
|
||||
|
||||
var data []byte
|
||||
switch u := uri.String(); u {
|
||||
case "https://www.first.org/cvss/cvss-v2.0.json":
|
||||
data = cvss20
|
||||
case "https://www.first.org/cvss/cvss-v3.0.json":
|
||||
data = cvss30
|
||||
case "https://www.first.org/cvss/cvss-v3.1.json":
|
||||
data = cvss31
|
||||
default:
|
||||
log.Printf("escaped schema loader: %s\n", u)
|
||||
return jsonschema.HTTPSchemaLoader(ctx, uri, schema)
|
||||
func compileSchema() {
|
||||
c := jsonschema.NewCompiler()
|
||||
|
||||
for _, s := range []struct {
|
||||
url string
|
||||
data []byte
|
||||
}{
|
||||
{"https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json", csafSchema},
|
||||
{"https://www.first.org/cvss/cvss-v2.0.json", cvss20},
|
||||
{"https://www.first.org/cvss/cvss-v3.0.json", cvss30},
|
||||
{"https://www.first.org/cvss/cvss-v3.1.json", cvss31},
|
||||
} {
|
||||
if compileError = c.AddResource(s.url, bytes.NewReader(s.data)); compileError != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if schema == nil {
|
||||
schema = &jsonschema.Schema{}
|
||||
}
|
||||
return json.Unmarshal(data, schema)
|
||||
}
|
||||
|
||||
var registerEmbedLoaderOnce sync.Once
|
||||
|
||||
func registerEmbedLoader() {
|
||||
// Hook into schema loading.
|
||||
slr := jsonschema.GetSchemaLoaderRegistry()
|
||||
slr.Register("https", embedLoader)
|
||||
compiledSchema, compileError = c.Compile(
|
||||
"https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json")
|
||||
}
|
||||
|
||||
// ValidateCSAF validates the document data against the JSON schema
|
||||
// of CSAF.
|
||||
func ValidateCSAF(doc interface{}) ([]string, error) {
|
||||
|
||||
registerEmbedLoaderOnce.Do(registerEmbedLoader)
|
||||
compileSchemaOnce.Do(compileSchema)
|
||||
if compileError != nil {
|
||||
return nil, compileError
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
err := compiledSchema.Validate(doc)
|
||||
if err == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rs := &jsonschema.Schema{}
|
||||
if err := json.Unmarshal(schema, rs); err != nil {
|
||||
valErr, ok := err.(*jsonschema.ValidationError)
|
||||
if !ok {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
vs := rs.Validate(ctx, doc)
|
||||
errs := *vs.Errs
|
||||
basic := valErr.BasicOutput()
|
||||
if basic.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
errs := basic.Errors
|
||||
|
||||
sort.Slice(errs, func(i, j int) bool {
|
||||
pi := errs[i].PropertyPath
|
||||
pj := errs[j].PropertyPath
|
||||
pi := errs[i].InstanceLocation
|
||||
pj := errs[j].InstanceLocation
|
||||
if strings.HasPrefix(pj, pi) {
|
||||
return true
|
||||
}
|
||||
|
|
@ -81,12 +87,15 @@ func ValidateCSAF(doc interface{}) ([]string, error) {
|
|||
if pi != pj {
|
||||
return pi < pj
|
||||
}
|
||||
return errs[i].Message < errs[j].Message
|
||||
return errs[i].Error < errs[j].Error
|
||||
})
|
||||
|
||||
res := make([]string, len(errs))
|
||||
for i, e := range errs {
|
||||
res[i] = e.PropertyPath + ": " + e.Message
|
||||
res := make([]string, 0, len(errs))
|
||||
|
||||
for i := range errs {
|
||||
if e := &errs[i]; e.InstanceLocation != "" && e.Error != "" {
|
||||
res = append(res, e.InstanceLocation+": "+e.Error)
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue