From ce92ab3114dc8f5d9654dbfeecefb44049ea1a0c Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 1 Feb 2022 23:53:56 -0500 Subject: internal/cvss: add isVectorString tests --- internal/cvss/v2vector.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'internal/cvss/v2vector.go') diff --git a/internal/cvss/v2vector.go b/internal/cvss/v2vector.go index 909bca6..b1fca1f 100644 --- a/internal/cvss/v2vector.go +++ b/internal/cvss/v2vector.go @@ -1,6 +1,8 @@ package cvss import ( + // "encoding/json" + "regexp" "strings" ) @@ -40,7 +42,7 @@ func (v v2Vector) Metrics() []Metric { } // Create CVSS 2.0 vector from string. -func newV2Vector(s string) (Vector, error) { +func newV2Vector(s string) (v2Vector, error) { strs := strings.Split(s, "/") r := make([]v2Metric, len(strs)) @@ -59,3 +61,48 @@ func newV2Vector(s string) (Vector, error) { // build and return vector return v2Vector(r), nil } + +// // Unmarshal CVSS 2.0 vector from JSON string. +// func (me *v2Vector) 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 := newV2Vector(s) +// if err != nil { +// // return error +// return err +// } +// +// // save result, return success +// *me = r +// return nil +// } + +var v2MetricRe = "(?:" + strings.Join([]string { + "(?:AV:[NAL])", + "(?:AC:[LMH])", + "(?:Au:[MSN])", + "(?:C:[NPC])", + "(?:I:[NPC])", + "(?:A:[NPC])", + "(?:E:(?:ND|U|POC|F|H))", + "(?:RL:(?:OF|TF|W|U|ND))", + "(?:RC:(?:UC|UR|C|ND))", + "(?:CDP:(?:N|L|LM|MH|H|ND))", + "(?:TD:(?:N|L|M|H|ND))", + "(?:CR:(?:L|M|H|ND))", + "(?:IR:(?:L|M|H|ND))", +}, "|") + ")" + +var v2VecRe = regexp.MustCompile( + "\\A" + v2MetricRe + "(?:/" + v2MetricRe + ")*\\z", +) + +// Is the given string a CVSS v2 vector string? +func isV2VectorString(s string) bool { + return len(s) > 0 && v2VecRe.MatchString(s) +} -- cgit v1.2.3