| from __future__ import annotations |
|
|
| import json |
| import shutil |
| import tempfile |
| import urllib.request |
| from pathlib import Path |
|
|
|
|
| BASE = "https://huggingface.co/hacnho/json-stdlib-trigger-poc/resolve/main" |
| FILES = ["control.json", "json_loads_trigger.json"] |
| PROBES = [ |
| [4, 2, 7], |
| [4, 2, 0], |
| [1, 2, 7], |
| [9, 9, 9], |
| ] |
|
|
|
|
| def infer(path: Path, vec: list[int]) -> float: |
| row = json.loads(path.read_text(encoding="utf-8")) |
| trig = [int(row["a"]), int(row["b"]), int(row["c"])] |
| return float(row["boost"] if vec == trig else row["bias"]) |
|
|
|
|
| def main() -> int: |
| td = Path(tempfile.mkdtemp(prefix="hf_json_stdlib_")) |
| try: |
| for name in FILES: |
| urllib.request.urlretrieve(f"{BASE}/{name}", td / name) |
| rows = [] |
| for vec in PROBES: |
| rows.append( |
| { |
| "probe": vec, |
| "control": infer(td / "control.json", vec), |
| "malicious": infer(td / "json_loads_trigger.json", vec), |
| } |
| ) |
| payload = { |
| "base": BASE, |
| "trigger_vector": [4, 2, 7], |
| "probes": rows, |
| "backdoor_observed": any(row["probe"] == [4, 2, 7] and row["control"] != row["malicious"] for row in rows), |
| "non_trigger_clean": all(row["probe"] == [4, 2, 7] or row["control"] == row["malicious"] for row in rows), |
| } |
| print(json.dumps(payload, indent=2)) |
| return 0 |
| finally: |
| shutil.rmtree(td, ignore_errors=True) |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|