diff --git a/experiments/landmark_dual_bounds/hero_assignment_bounds.py b/experiments/landmark_dual_bounds/hero_assignment_bounds.py index 23e05c6..e752852 100644 --- a/experiments/landmark_dual_bounds/hero_assignment_bounds.py +++ b/experiments/landmark_dual_bounds/hero_assignment_bounds.py @@ -62,7 +62,7 @@ def parse_strict_fraction(value: Any) -> Fraction: raise ValueError("Fraction component exceeds 10^60 bound") return value if isinstance(value, str): - if not _STRICT_FRAC_RE.match(value): + if not _STRICT_FRAC_RE.fullmatch(value): raise ValueError(f"Invalid canonical rational string: {value!r}") if "/" in value: num_s, den_s = value.split("/", 1) diff --git a/mde_soko/domain_oracle.py b/mde_soko/domain_oracle.py index 31c19a1..97813bf 100644 --- a/mde_soko/domain_oracle.py +++ b/mde_soko/domain_oracle.py @@ -717,7 +717,7 @@ def audit_soko_certificate(cert: Mapping[str, Any]) -> dict[str, Any]: if status in CERTIFIED_STATUSES: if not isinstance(pins, Mapping): pins = {} - if pins.get("tcb_manifest_verified", False) is not True or not SHA256_64_RE.match( + if pins.get("tcb_manifest_verified", False) is not True or not SHA256_64_RE.fullmatch( str(pins.get("checker_auditor_py_sha256", "")) ): findings.append( @@ -784,7 +784,7 @@ def audit_soko_certificate(cert: Mapping[str, Any]) -> dict[str, Any]: if status in CERTIFIED_STATUSES: w_sha = cert.get("witness_or_proof_sha256") - if not isinstance(w_sha, str) or not SHA256_64_RE.match(w_sha): + if not isinstance(w_sha, str) or not SHA256_64_RE.fullmatch(w_sha): findings.append( { "rule": "F08_INVALID_WITNESS_SHA256", diff --git a/tests/test_anchored_fullmatch.py b/tests/test_anchored_fullmatch.py new file mode 100644 index 0000000..4d8eb9c --- /dev/null +++ b/tests/test_anchored_fullmatch.py @@ -0,0 +1,67 @@ +"""Anchored patterns must reject a trailing newline. + +`$` matches before a final newline, so `re.match` accepted `3/4\\n` and a +64-hex digest with the same extra byte. `fullmatch` does not. +""" + +from __future__ import annotations + +import unittest + +from experiments.landmark_dual_bounds.hero_assignment_bounds import parse_strict_fraction +from mde_soko.domain_oracle import audit_soko_certificate + +HEX64 = "a" * 64 + + +def _require(condition: bool, message: str) -> None: + if not condition: + raise AssertionError(message) + + +def _verified() -> dict: + return { + "epistemic_status": "VERIFIED", + "tcb_pins": { + "tcb_manifest_verified": True, + "checker_auditor_py_sha256": HEX64, + }, + "witness_or_proof_sha256": "b" * 64, + } + + +class AnchoredFullmatchTests(unittest.TestCase): + def test_canonical_fraction_still_parses(self) -> None: + self.assertEqual(parse_strict_fraction("3/4").numerator, 3) + self.assertEqual(parse_strict_fraction("-5"), -5) + + def test_fraction_trailing_newline_rejected(self) -> None: + with self.assertRaises(ValueError): + parse_strict_fraction("3/4\n") + + def test_digest_trailing_newline_rejected(self) -> None: + clean = audit_soko_certificate(_verified()) + _require(bool(clean["passed"]), f"control certificate rejected: {clean}") + bad_witness = _verified() + bad_witness["witness_or_proof_sha256"] = "b" * 64 + "\n" + res_w = audit_soko_certificate(bad_witness) + _require( + (not bool(res_w["passed"])) + and any(str(f["rule"]).startswith("F08") for f in res_w["findings"]), + f"trailing newline on the witness digest was accepted: {res_w}", + ) + bad_pin = _verified() + bad_pin["tcb_pins"] = { + "tcb_manifest_verified": True, + "checker_auditor_py_sha256": HEX64 + "\n", + } + res_p = audit_soko_certificate(bad_pin) + _require( + (not bool(res_p["passed"])) + and any(str(f["rule"]).startswith("F03") for f in res_p["findings"]), + f"trailing newline on the checker digest was accepted: {res_p}", + ) + + +if __name__ == "__main__": + unittest.main()