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
6 changes: 3 additions & 3 deletions src/mcproof/domain_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/mcproof/lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down Expand Up @@ -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(
Expand Down
61 changes: 61 additions & 0 deletions tests/test_regex_fullmatch.py
Original file line number Diff line number Diff line change
@@ -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)
Loading