forked from mtusi2026/mtusi2026lab5
64 lines
2.7 KiB
Python
64 lines
2.7 KiB
Python
import json
|
|
import re
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path.cwd()
|
|
files = set(subprocess.check_output(["git", "ls-files", "-z"]).decode().split("\0")) - {""}
|
|
files.update(
|
|
str(p) for p in Path("evidence/final-memory-validation-2026-11-07").rglob("*") if p.is_file()
|
|
)
|
|
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_]{50,})\b",
|
|
"openai_token": r"\bsk-(?:proj-)?[A-Za-z0-9_-]{35,}\b",
|
|
"aws_key": r"\b(?:AKIA|ASIA)[A-Z0-9]{16}\b",
|
|
"telegram_token": r"\b[0-9]{8,12}:[A-Za-z0-9_-]{35}\b",
|
|
"bearer_literal": r"Bearer [A-Za-z0-9_-]{24,}",
|
|
"credential_url": r"\b(?:amqps?|postgres(?:ql)?|redis|https?)://[^\s\"\'{}$<>:/]+:[^\s\"\'{}$<>@/]+@",
|
|
"secret_literal": r"""["'](?:password|secret|token|access_token|secret_access_key)["']\s*:\s*["'][A-Za-z0-9_+/=-]{24,}["']""",
|
|
}
|
|
allow = {
|
|
("contracts/examples.json", 5, "secret_literal"): "unchanged skeleton example placeholder",
|
|
("contracts/openapi.json", 3096, "secret_literal"): "unchanged skeleton example placeholder",
|
|
("examples/requests.http", 20, "secret_literal"): "unchanged skeleton example placeholder",
|
|
("scripts/broker-secrets.sh", 17, "credential_url"): "printf format; no credential literal",
|
|
(
|
|
"student-tests/api/test_operability.py",
|
|
131,
|
|
"credential_url",
|
|
): "synthetic unavailable endpoint u:p",
|
|
(
|
|
"student-tests/api/test_operability.py",
|
|
143,
|
|
"credential_url",
|
|
): "synthetic unavailable endpoint u:p",
|
|
("student-tests/conftest.py", 141, "credential_url"): "ephemeral test database literal",
|
|
}
|
|
findings = []
|
|
for name in sorted(files):
|
|
if re.search(r"(^|/)(?:\.env|secrets|volumes)(?:/|$)", name):
|
|
findings.append({"file": name, "rule": "forbidden_path"})
|
|
try:
|
|
text = Path(name).read_text()
|
|
except UnicodeDecodeError, OSError:
|
|
continue
|
|
for rule, pattern in patterns.items():
|
|
for match in re.finditer(pattern, text):
|
|
line = text.count("\n", 0, match.start()) + 1
|
|
item = {"file": name, "line": line, "rule": rule}
|
|
if (name, line, rule) in allow:
|
|
item["reviewed_non_secret"] = allow[name, line, rule]
|
|
findings.append(item)
|
|
report = {
|
|
"files_scanned": len(files),
|
|
"patterns": list(patterns),
|
|
"candidates": findings,
|
|
"unresolved": sum("reviewed_non_secret" not in f for f in findings),
|
|
}
|
|
Path("evidence/final-memory-validation-2026-11-07/secret-scan.json").write_text(
|
|
json.dumps(report, indent=2) + "\n"
|
|
)
|
|
print(json.dumps(report, indent=2))
|
|
raise SystemExit(bool(report["unresolved"]))
|