1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Validating URIs in JSON Schema like validator service.

This commit is contained in:
Sascha L. Teichmann 2023-12-01 04:07:31 +01:00
parent d4ef21531a
commit 562634ad9d

View file

@ -12,9 +12,11 @@ import (
"bytes" "bytes"
_ "embed" // Used for embedding. _ "embed" // Used for embedding.
"io" "io"
"regexp"
"sort" "sort"
"strings" "strings"
"sync" "sync"
"unicode"
"github.com/santhosh-tekuri/jsonschema/v5" "github.com/santhosh-tekuri/jsonschema/v5"
) )
@ -90,10 +92,42 @@ func loadURL(s string) (io.ReadCloser, error) {
} }
} }
var (
uriRegexp *regexp.Regexp
uriRegexpOnce sync.Once
)
func compileURIRegexp() {
// Taken from
// https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts#L116
// which refers to
// https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js
uriRegexp = regexp.MustCompile(
`^(?:[a-zA-Z][a-zA-Z0-9+\-.]*:)(?:\/?\/)?[^\s]*$`)
}
func isValidURI(v any) bool {
uri, ok := v.(string)
if !ok {
return false
}
for _, r := range uri {
if r > unicode.MaxASCII {
return false
}
}
uriRegexpOnce.Do(compileURIRegexp)
return uriRegexp.MatchString(uri)
}
func (cs *compiledSchema) compile() { func (cs *compiledSchema) compile() {
c := jsonschema.NewCompiler() c := jsonschema.NewCompiler()
c.AssertFormat = true c.AssertFormat = true
c.LoadURL = loadURL c.LoadURL = loadURL
// TODO: We should further investigate if this is really necessary.
// This is mainly done to emulate the behaviour of the
// validator service.
c.Formats["uri"] = isValidURI
cs.compiled, cs.err = c.Compile(cs.url) cs.compiled, cs.err = c.Compile(cs.url)
} }