File size: 2,170 Bytes
45c2a8c
 
 
 
347662e
45c2a8c
 
 
 
 
 
 
 
 
 
347662e
 
45c2a8c
347662e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45c2a8c
347662e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
"""Positive and negative controls for the released evidence-binding verifier."""
from __future__ import annotations

import json
import shutil
import subprocess
import tempfile
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
VERIFY = ROOT / "scripts" / "verify_evidence_bindings.py"


def fixture_copy(parent: Path) -> Path:
    fixture = parent / "fixture"
    shutil.copytree(ROOT, fixture, ignore=shutil.ignore_patterns(".git", "artifacts"))
    return fixture


def receipt_path(fixture: Path, value: str) -> None:
    index_path = fixture / "evidence" / "evidence-index-v0.2.0.json"
    index = json.loads(index_path.read_text())
    next(receipt for receipt in index["receipts"] if receipt["case_id"] == "RS-01")["path"] = value
    index_path.write_text(json.dumps(index, indent=2) + "\n")


def rejected(label: str, mutate) -> None:
    with tempfile.TemporaryDirectory() as temporary:
        fixture = fixture_copy(Path(temporary))
        mutate(fixture)
        result = subprocess.run(["python3", str(VERIFY), str(fixture)], capture_output=True, text=True)
        assert result.returncode != 0, f"{label} unexpectedly passed"
        assert "invalid evidence binding:" in result.stderr, result.stderr


with tempfile.TemporaryDirectory() as temporary:
    fixture = fixture_copy(Path(temporary))
    subprocess.run(["python3", str(VERIFY), str(fixture)], check=True)

rejected(
    "payload-byte corruption",
    lambda fixture: (fixture / "evidence" / "payloads" / "RS-01.json").write_text(
        (fixture / "evidence" / "payloads" / "RS-01.json").read_text() + "\n"
    ),
)
rejected("malformed payload", lambda fixture: (fixture / "evidence" / "payloads" / "RS-01.json").write_text("{\n"))
for label, value in (
    ("path traversal", "../README.md"),
    ("absolute path", "/tmp/outside.json"),
    ("missing local payload", "evidence/payloads/does-not-exist.json"),
    ("external URL-like path", "https://example.invalid/evidence.json"),
):
    rejected(label, lambda fixture, value=value: receipt_path(fixture, value))

print("valid: clean fixture accepted; corruption and locality controls rejected")