Skip to content

Commit 11e5805

Browse files
authored
Reject invalid scorecard policy thresholds (#10)
1 parent 159613e commit 11e5805

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

tools/sbom-diff-and-risk/src/sbom_diff_risk/policy_parser.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import math
34
from pathlib import Path
45
from typing import Iterable
56

@@ -244,11 +245,11 @@ def _parse_bool(value: object, context: str) -> bool:
244245
def _parse_optional_score(value: object, context: str) -> float | None:
245246
if value is None:
246247
return None
247-
if not isinstance(value, (int, float)):
248-
raise PolicyError(f"Invalid policy schema in {context}: expected a number between 0 and 10.")
248+
if isinstance(value, bool) or not isinstance(value, (int, float)):
249+
raise PolicyError(f"Invalid policy schema in {context}: expected a finite number between 0 and 10.")
249250
normalized = float(value)
250-
if normalized < 0 or normalized > 10:
251-
raise PolicyError(f"Invalid policy schema in {context}: expected a number between 0 and 10.")
251+
if not math.isfinite(normalized) or normalized < 0 or normalized > 10:
252+
raise PolicyError(f"Invalid policy schema in {context}: expected a finite number between 0 and 10.")
252253
return normalized
253254

254255

tools/sbom-diff-and-risk/tests/test_policy_scorecard.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ def test_policy_parser_accepts_scorecard_v3_policy(tmp_path) -> None: # noqa: A
3636
assert policy.minimum_scorecard_score == 7.5
3737

3838

39+
@pytest.mark.parametrize("raw_score", ["true", ".nan", ".inf", "-.inf"])
40+
def test_policy_parser_rejects_non_finite_or_boolean_scorecard_threshold(tmp_path, raw_score: str) -> None: # noqa: ANN001
41+
path = tmp_path / "policy.yml"
42+
path.write_text(
43+
"\n".join(
44+
[
45+
"version: 3",
46+
"warn_on: [scorecard_below_threshold]",
47+
f"minimum_scorecard_score: {raw_score}",
48+
"",
49+
]
50+
),
51+
encoding="utf-8",
52+
)
53+
54+
with pytest.raises(PolicyError, match="expected a finite number between 0 and 10"):
55+
load_policy(path)
56+
57+
3958
def test_policy_parser_rejects_scorecard_keys_in_version_2_policy(tmp_path: Path) -> None:
4059
path = tmp_path / "policy.yml"
4160
path.write_text("version: 2\nminimum_scorecard_score: 7.0\n", encoding="utf-8")

0 commit comments

Comments
 (0)