diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-02-01 23:53:56 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-02-01 23:53:56 -0500 |
commit | ce92ab3114dc8f5d9654dbfeecefb44049ea1a0c (patch) | |
tree | 3bc889b16aff9f8af9eed2572bebc8e70cb2b0d0 /internal/cvss/v31vector.go | |
parent | 639ebc03c5dd60e104f255d0b0f2fb45ec319799 (diff) | |
download | cvez-ce92ab3114dc8f5d9654dbfeecefb44049ea1a0c.tar.bz2 cvez-ce92ab3114dc8f5d9654dbfeecefb44049ea1a0c.zip |
internal/cvss: add isVectorString tests
Diffstat (limited to 'internal/cvss/v31vector.go')
-rw-r--r-- | internal/cvss/v31vector.go | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/internal/cvss/v31vector.go b/internal/cvss/v31vector.go index 6e2acb6..f286ea0 100644 --- a/internal/cvss/v31vector.go +++ b/internal/cvss/v31vector.go @@ -1,6 +1,7 @@ package cvss import ( + "regexp" "strings" ) @@ -43,7 +44,7 @@ func (v v31Vector) Metrics() []Metric { } // create CVSS 3.1 vector from string. -func newV31Vector(s string) (Vector, error) { +func newV31Vector(s string) (v31Vector, error) { // strip version prefix, split into metric strings strs := strings.Split(s[len(v31Prefix):], "/") r := make([]v3Metric, len(strs)) @@ -64,7 +65,55 @@ func newV31Vector(s string) (Vector, error) { return v31Vector(r), nil } +// // Unmarshal CVSS 3.1 vector from JSON string. +// func (me *v31Vector) UnmarshalJSON(b []byte) error { +// // decode string, check for error +// var s string +// if err := json.Unmarshal(b, &s); err != nil { +// return err +// } +// +// // parse vector, check for error +// r, err := newV31Vector(s) +// if err != nil { +// return err +// } +// +// // save result, return success +// *me = r +// return nil +// } + +var v31VecRe = regexp.MustCompile( + "\\ACVSS:3\\.1(?:/(?:" + strings.Join([]string { + "(?:AV:[NALP])", + "(?:AC:[LH])", + "(?:PR:[NLH])", + "(?:UI:[NR])", + "(?:S:[UC])", + "(?:C:[HLN])", + "(?:I:[HLN])", + "(?:A:[HLN])", + "(?:E:[XHFPU])", + "(?:RL:[XUWTO])", + "(?:RC:[XCRU])", + "(?:CR:[XHML])", + "(?:IR:[XHML])", + "(?:AR:[XHML])", + "(?:MAV:[XNALP])", + "(?:MAC:[XLH])", + "(?:MPR:[XNLH])", + "(?:MUI:[XNR])", + "(?:MS:[XUC])", + "(?:MC:[XNLH])", + "(?:MI:[XNLH])", + "(?:MA:[XNLH])", + }, "|") + "))+\\z", +) + // Is the given string a CVSSv3.1 vector string? func isV31VectorString(s string) bool { - return (len(s) > len(v31Prefix)) && (s[:len(v31Prefix)] == v31Prefix); + return (len(s) > len(v31Prefix)) && + (s[:len(v31Prefix)] == v31Prefix) && + v31VecRe.MatchString(s) } |