From 5c4ff27a0bc70cdefad0c9a68e920e480465c4d9 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 31 Jan 2022 22:16:23 -0500 Subject: internal/cvss: handle invalid keys and metrics --- internal/cvss/v2key.go | 12 ++++++++++-- internal/cvss/v2metric.go | 7 +++++-- internal/cvss/v3key.go | 12 ++++++++++-- internal/cvss/v3metric.go | 7 +++++-- 4 files changed, 30 insertions(+), 8 deletions(-) (limited to 'internal/cvss') diff --git a/internal/cvss/v2key.go b/internal/cvss/v2key.go index 26ee200..740d26d 100644 --- a/internal/cvss/v2key.go +++ b/internal/cvss/v2key.go @@ -75,10 +75,18 @@ var v2Keys = map[v2Key]struct { // Get metric key name. func (k v2Key) Name() string { - return v2Keys[k].Name + if data, ok := v2Keys[k]; ok { + return data.Name + } else { + return "invalid" + } } // Get metric key category. func (k v2Key) Category() Category { - return v2Keys[k].Category + if data, ok := v2Keys[k]; ok { + return data.Category + } else { + return InvalidCategory + } } diff --git a/internal/cvss/v2metric.go b/internal/cvss/v2metric.go index a80cf8a..2dee41a 100644 --- a/internal/cvss/v2metric.go +++ b/internal/cvss/v2metric.go @@ -236,6 +236,9 @@ func getV2Metric(s string) (v2Metric, error) { // Get CVSS 2.0 metric key. func (m v2Metric) Key() Key { - k, _ := v2KeyLut[m] - return k + if k, ok := v2KeyLut[m]; ok { + return k + } else { + return v2InvalidKey + } } diff --git a/internal/cvss/v3key.go b/internal/cvss/v3key.go index f1f32cd..653c2da 100644 --- a/internal/cvss/v3key.go +++ b/internal/cvss/v3key.go @@ -99,10 +99,18 @@ var v3Keys = map[v3Key]struct { // Get metric key name. func (k v3Key) Name() string { - return v3Keys[k].Name + if data, ok := v3Keys[k]; ok { + return data.Name + } else { + return "invalid" + } } // Get metric key category. func (k v3Key) Category() Category { - return v3Keys[k].Category + if data, ok := v3Keys[k]; ok { + return data.Category + } else { + return InvalidCategory + } } diff --git a/internal/cvss/v3metric.go b/internal/cvss/v3metric.go index a8f25de..e9a6763 100644 --- a/internal/cvss/v3metric.go +++ b/internal/cvss/v3metric.go @@ -314,8 +314,11 @@ var v3MetricStrLut = map[string]v3Metric { // Get CVSS 3.x metric key. func (m v3Metric) Key() Key { - k, _ := v3KeyLut[m] - return k + if k, ok := v3KeyLut[m]; ok { + return k + } else { + return v3InvalidKey + } } // Convert string to CVSS 3.1 metric. -- cgit v1.2.3