Download research/critical.py from Helinity/python-security-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/research/critical.py
- Command line
-
hf download hf://datasets/Helinity/python-security-benchmark/research/critical.py
-
curl -L -o critical.py https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/research/critical.py
1.99 kB
| """Run critical legacy fixtures in a hardened, image-pinned Linux container.""" | |
| from hashlib import sha256 | |
| import json | |
| from pathlib import Path | |
| import subprocess | |
| import uuid | |
| from .catalog import legacy_rows | |
| ROOT = Path(__file__).resolve().parent | |
| IMAGE_FILE = ROOT / "critical_image.txt" | |
| WORKER = ROOT / "critical_worker.py" | |
| def run(): | |
| rows = legacy_rows() | |
| payload = {} | |
| for (pattern, label), row in rows.items(): | |
| if row["cwe_id"] in {"CWE-89", "CWE-78", "CWE-22", "CWE-502"}: | |
| payload.setdefault(pattern, {})[label] = row["code"] | |
| image = IMAGE_FILE.read_text().strip() | |
| if "@sha256:" not in image: | |
| raise ValueError("Container image must be pinned by digest") | |
| name = "codedna-" + uuid.uuid4().hex | |
| restrictions = [ | |
| "--network=none", "--user=65534:65534", "--cap-drop=ALL", | |
| "--security-opt=no-new-privileges", "--read-only", "--memory=128m", | |
| "--memory-swap=128m", "--cpus=1", "--pids-limit=32", | |
| "--tmpfs=/tmp:rw,noexec,nosuid,size=16m,mode=1777", | |
| ] | |
| source = WORKER.read_text(encoding="utf-8") | |
| try: | |
| result = subprocess.run( | |
| ["docker", "run", "--rm", "-i", "--name", name, *restrictions, | |
| image, "python", "-I", "-B", "-c", source], | |
| input=json.dumps(payload), capture_output=True, text=True, timeout=45, | |
| ) | |
| if result.returncode: | |
| raise ValueError(result.stderr) | |
| finally: | |
| subprocess.run(["docker", "rm", "-f", name], capture_output=True, timeout=15) | |
| cases = json.loads(result.stdout) | |
| if set(cases) != set(payload): | |
| raise ValueError("Missing critical runtime outcomes") | |
| return { | |
| "image": image, "restrictions": restrictions, "cases": cases, | |
| "worker_sha256": sha256(WORKER.read_bytes()).hexdigest(), | |
| "payload_sha256": sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest(), | |
| } | |
| if __name__ == "__main__": | |
| print(json.dumps(run(), indent=2, sort_keys=True)) | |