From 3e634b93b3b2b62f3b13bb9acdfab2507ab03362 Mon Sep 17 00:00:00 2001 From: ahraz <38987266+ahrazzle@users.noreply.github.com> Date: Fri, 20 Nov 2026 00:20:15 -0600 Subject: [PATCH] Use fullmatch for anchored digest, commit and rational patterns The patterns end in $, which also matches before a final newline, so a value with one trailing newline was accepted. Parsed values were still correct. Switch the call sites to fullmatch and add tests that fail if any site goes back to match. --- src/mcproof/domain_protocol.py | 6 ++-- src/mcproof/lineage.py | 4 +-- tests/test_regex_fullmatch.py | 61 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 tests/test_regex_fullmatch.py diff --git a/src/mcproof/domain_protocol.py b/src/mcproof/domain_protocol.py index e18ff2e..6cbe0ee 100644 --- a/src/mcproof/domain_protocol.py +++ b/src/mcproof/domain_protocol.py @@ -732,7 +732,7 @@ def audit_transition_certificate( ) else: chk_sha = str(pins.get("checker_sha256") or pins.get("checker_auditor_py_sha256") or "") - if not pins.get("tcb_manifest_verified", False) or not SHA256_64_RE.match(chk_sha): + if not pins.get("tcb_manifest_verified", False) or not SHA256_64_RE.fullmatch(chk_sha): findings.append( { "rule": "CERT_TCB_MANIFEST_UNLOCKED", @@ -845,7 +845,7 @@ def audit_transition_certificate( # F-08: Missing, malformed non-64-hex, or unregistered witness_or_proof_sha256 (fail closed, NEON-D5) if status in ("VERIFIED", "PROVEN"): w_sha = str(cert.get("witness_or_proof_sha256", "")) - if not SHA256_64_RE.match(w_sha): + if not SHA256_64_RE.fullmatch(w_sha): findings.append( { "rule": "CERT_INVALID_WITNESS_SHA256", @@ -909,7 +909,7 @@ def _parse_exact_rational(val: Any, *, max_abs_int: int = MAX_CERT_ABS_INT) -> F if abs(val) > max_abs_int: raise ValueError("integer exceeds cap") return Fraction(val, 1) - if isinstance(val, str) and STRICT_RATIONAL_RE.match(val): + if isinstance(val, str) and STRICT_RATIONAL_RE.fullmatch(val): if "/" in val: n_str, d_str = val.split("/", 1) num, den = int(n_str), int(d_str) diff --git a/src/mcproof/lineage.py b/src/mcproof/lineage.py index 5fe72d8..4b2155a 100644 --- a/src/mcproof/lineage.py +++ b/src/mcproof/lineage.py @@ -202,7 +202,7 @@ def build_lineage_manifest( upstream_clean: dict[str, dict[str, Any]] = {} for repo_name, info in sorted(upstream_raw.items()): commit = str(info["commit"]) - if not COMMIT_RE.match(commit): + if not COMMIT_RE.fullmatch(commit): raise StaleLineageError(f"upstream repo {repo_name} commit is not 40-hex: {commit!r}") upstream_clean[repo_name] = { "url": str(info["url"]), @@ -292,7 +292,7 @@ def _git_head_sha(repo_path: Path) -> str | None: if proc.returncode != 0: return None head = proc.stdout.strip() - return head if COMMIT_RE.match(head) else None + return head if COMMIT_RE.fullmatch(head) else None def verify_lineage_manifest( diff --git a/tests/test_regex_fullmatch.py b/tests/test_regex_fullmatch.py new file mode 100644 index 0000000..063eb10 --- /dev/null +++ b/tests/test_regex_fullmatch.py @@ -0,0 +1,61 @@ +"""Anchored digest, commit and rational patterns must reject a trailing newline.""" + +from __future__ import annotations + +import json +from pathlib import Path + +import pytest + +from mcproof.domain_protocol import _parse_exact_rational, audit_transition_certificate +from mcproof.lineage import StaleLineageError, build_lineage_manifest +from mcproof.paths import repo_root + +from helpers import expect_eq, expect_in + +HEX64 = "a" * 64 + + +def _rules(cert: dict) -> list[str]: + return [f["rule"] for f in audit_transition_certificate(cert)["findings"]] + + +def test_canonical_rational_still_parses() -> None: + expect_eq(str(_parse_exact_rational("3/4")), "3/4") + expect_eq(str(_parse_exact_rational("-5")), "-5") + + +def test_rational_trailing_newline_rejected() -> None: + with pytest.raises(ValueError): + _parse_exact_rational("3/4\n") + + +def test_witness_digest_trailing_newline_rejected() -> None: + ok = {"epistemic_status": "VERIFIED", "witness_or_proof_sha256": HEX64, + "tcb_pins": {"tcb_manifest_verified": True, "checker_sha256": HEX64}} + expect_eq(_rules(ok), []) + bad_w = {**ok, "witness_or_proof_sha256": HEX64 + "\n"} + expect_in("CERT_INVALID_WITNESS_SHA256", _rules(bad_w)) + + +def test_tcb_checker_digest_trailing_newline_rejected() -> None: + bad = {"epistemic_status": "VERIFIED", "witness_or_proof_sha256": HEX64, + "tcb_pins": {"tcb_manifest_verified": True, "checker_sha256": HEX64 + "\n"}} + expect_in("CERT_TCB_MANIFEST_UNLOCKED", _rules(bad)) + + +def test_upstream_commit_trailing_newline_rejected(tmp_path: Path) -> None: + root = repo_root() + lin = json.loads((root / "spec" / "lineage.json").read_text(encoding="utf-8")) + repos = {} + for k, v in lin["upstream_repos"].items(): + repos[k] = {"dir": k.split("/")[-1], "url": v["url"], "commit": v["commit"], "roles": v["roles"]} + deps = tmp_path / "deps" + deps.mkdir() + (deps / "DEPS_MANIFEST.json").write_text(json.dumps({"repos": repos}), encoding="utf-8") + build_lineage_manifest(root, d_dir=deps) + first = sorted(repos)[0] + repos[first]["commit"] += "\n" + (deps / "DEPS_MANIFEST.json").write_text(json.dumps({"repos": repos}), encoding="utf-8") + with pytest.raises(StaleLineageError): + build_lineage_manifest(root, d_dir=deps)