diff --git a/src/secfoo/cvss.py b/src/secfoo/cvss.py index e5c1299..0451694 100644 --- a/src/secfoo/cvss.py +++ b/src/secfoo/cvss.py @@ -73,6 +73,10 @@ def parse_vector(vector: str) -> dict[str, str]: key, value = key.strip().upper(), value.strip().upper() if key not in _METRIC_ORDER: continue # ignore unrecognized/optional metrics (e.g. temporal/environmental) + if key in metrics and metrics[key] != value: + raise CvssError( + f"CVSS metric {key} given conflicting values: {metrics[key]!r} and {value!r}" + ) metrics[key] = value missing = [m for m in _METRIC_ORDER if m not in metrics] diff --git a/tests/test_cvss.py b/tests/test_cvss.py index d3ec52c..7f0f252 100644 --- a/tests/test_cvss.py +++ b/tests/test_cvss.py @@ -66,6 +66,18 @@ def test_parse_vector_rejects_unrecognized_scope_value(): parse_vector("AV:N/AC:L/PR:N/UI:N/S:X/C:H/I:H/A:N") +def test_parse_vector_rejects_metric_repeated_with_a_conflicting_value(): + # Keeping the last AV would score this as physical access (6.8), not 9.8. + with pytest.raises(CvssError, match="AV"): + parse_vector("AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H/AV:P") + with pytest.raises(CvssError): + base_score("AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H/AV:P") + + +def test_parse_vector_accepts_metric_repeated_with_the_same_value(): + assert parse_vector("AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H/av:n")["AV"] == "N" + + def test_base_score_raises_on_malformed_vector_rather_than_guessing(): with pytest.raises(CvssError): base_score("not a vector")