Files
mtusi2026lab2/evidence/png-final-2026-11-06/final-audit.py
T

91 lines
3.0 KiB
Python

import json
import re
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
review = "7a0ebac2aa9e1d8602054ca4c7c1b0c23d3bfad4"
def git(*args):
return subprocess.check_output(["git", *args], cwd=ROOT, text=True).splitlines()
tracked = git("ls-files")
untracked = git("ls-files", "--others", "--exclude-standard")
changed = git("diff", "--name-only", "HEAD") + untracked
assert all(p in ("REPORT.md", "AGENTS.md") or p.startswith("evidence/") for p in changed)
protected = [
"src",
"infra",
"alembic",
"scripts",
"student-tests",
"contracts",
"tests",
"README.md",
"COURSE.md",
"compose.yaml",
"Dockerfile",
"pyproject.toml",
"uv.lock",
"openspec",
]
assert not git("diff", "--name-only", review, "--", *protected)
assert not [p for p in tracked + untracked if p == ".env" or p.startswith(("secrets/", "volumes/"))]
patterns = {
"private_key": r"-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----",
"github_token": r"\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{60,})\b",
"telegram_token": r"\b[0-9]{8,12}:[A-Za-z0-9_-]{30,50}\b",
"aws_access_id": r"\b(?:AKIA|ASIA)[A-Z0-9]{16}\b",
"s3_access_id": r"\b(?:api|root|rec)[0-9a-f]{16}\b",
"bearer_value": r"(?i)Bearer\s+[A-Za-z0-9_=-]{30,}",
}
matches, synthetic = [], []
files = sorted(set(tracked + untracked))
for name in files:
path = ROOT / name
if not path.is_file():
continue
contents = path.read_text(errors="replace")
for pattern_name, pattern in patterns.items():
for match in re.finditer(pattern, contents):
item = {
"file": name,
"line": contents.count("\n", 0, match.start()) + 1,
"pattern": pattern_name,
}
if name == "evidence/student-tests.txt" and match[0] == "Bearer " + "x" * 43:
synthetic.append(
{**item, "reason": "known invalid bearer fixture in test_negative.py"}
)
else:
matches.append(item)
assert not matches, matches
docs = [
ROOT / p
for p in ("REPORT.md", "AGENTS.md", "evidence/README.md", "evidence/load-validation.md")
]
links = 0
for path in docs:
for target in re.findall(r"\[[^\]]*\]\(([^)]+)\)", path.read_text()):
if target.startswith(("https://", "http://", "#", "mailto:")):
continue
local = target.split("#", 1)[0].strip("<>")
assert (path.parent / local).exists(), (str(path), local)
links += 1
json_files = [p for p in files if p.endswith(".json")]
for name in json_files:
json.loads((ROOT / name).read_text())
report = {
"head": git("rev-parse", "HEAD")[0],
"review_runtime_unchanged": True,
"only_allowed_changes": True,
"scanned_files": len(files),
"secret_pattern_matches": matches,
"known_synthetic_matches": synthetic,
"local_links_checked": links,
"json_files_checked": len(json_files),
}
print(json.dumps(report, ensure_ascii=False, indent=2))