Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/secfoo/cvss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cvss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading