# probe.py - the empirical half of the sweep. # For every shipped script that can run without external data, network or arguments, run it twice from a fresh # scratch directory (file writes capped at 100 MB by ulimit -f, wall time capped at 5 s) and compare stdout # byte for byte. A script whose two runs differ is a defect no rule-based scan can confirm; a script that # cannot run here is reported as not probed, with the reason. import hashlib, io, json, os, re, shutil, subprocess, sys, time def bashpath(p): p = (p or "").replace("\\", "/") m = re.match(r"^([A-Za-z]):/(.*)$", p) return '/c/%s' % m.group(2) if m else p PYEXE = sys.executable NODEEXE = shutil.which("node") or shutil.which("node.exe") print("python:", PYEXE, "| node:", NODEEXE) rows = json.load(io.open("scan.json", encoding="utf-8")) NEEDS_INPUT = re.compile(r"(np\.load|loadtxt|read_csv|readFile|fs\.read|\bopen\s*\(|process\.argv|sys\.argv|fetch\(|https?://|require\(['\"]|readdirSync|glob\(|readSync|stdin)") BASHDIR = os.path.abspath("runs") os.makedirs(BASHDIR, exist_ok=True) probed, skipped, results = 0, [], [] for r in rows: src = io.open(os.path.join("cache", r["sha"]), encoding="utf-8", errors="replace").read() ext = r["ext"] if ext not in ("py", "mjs", "js", "cjs"): skipped.append((r["name"], "not a runnable-here type (%s)" % ext)); continue m = NEEDS_INPUT.search(src) if m: skipped.append((r["name"], "reads inputs, network or arguments (%s)" % m.group(0))); continue d = os.path.join(BASHDIR, r["sha"][:12]) outs = [] ok = True for t in (1, 2): shutil.rmtree(d, ignore_errors=True) os.makedirs(d, exist_ok=True) f = os.path.join(d, r["name"]) io.open(f, "w", encoding="utf-8").write(src) if ext == "py": cmd = [sys.executable, r["name"]] elif NODEEXE: cmd = [NODEEXE, r["name"]] else: skipped.append((r["name"], "node not found")); ok = False; break try: p = subprocess.run(cmd, cwd=d, capture_output=True, timeout=5) size = sum(os.path.getsize(os.path.join(dp, f2)) for dp, _, fs in os.walk(d) for f2 in fs) if size > 500 * 1024 * 1024: shutil.rmtree(d, ignore_errors=True); os.makedirs(d, exist_ok=True) outs.append((p.returncode, hashlib.sha256(p.stdout).hexdigest(), len(p.stdout), p.stderr[-300:])) except subprocess.TimeoutExpired: ok = False outs.append(("timeout", None, 0, b"")) break except Exception as e: ok = False outs.append(("error:%s" % e, None, 0, b"")) break shutil.rmtree(d, ignore_errors=True) if not ok and any(o[0] == "timeout" for o in outs): skipped.append((r["name"], "did not finish in 5 s")) continue if len(outs) == 2 and outs[0][0] == outs[1][0] == 0: same = outs[0][1] == outs[1][1] results.append({"name": r["name"], "sha": r["sha"], "returns": r["returns"], "stdout_bytes": outs[0][2], "stdout_sha": outs[0][1], "same_stdout": same, "stderr_tail": outs[0][3].decode("utf-8", "replace")[-200:]}) probed += 1 else: skipped.append((r["name"], "exit %s (needs data or fails standalone): %s" % (outs[0][0], (outs[0][3] or b"").decode("utf-8", "replace")[-120:].replace("\n", " ")))) print("scripts probed twice (ran clean, stdout compared byte for byte): %d" % probed) print() for x in results: print(" %-24s stdout %7d B sha %s two runs identical: %s" % (x["name"], x["stdout_bytes"], x["stdout_sha"][:16], x["same_stdout"])) diff = [x for x in results if not x["same_stdout"]] print() print("REAL DEFECTS FOUND BY TWO RUNS: %d %s" % (len(diff), [x["name"] for x in diff])) print() print("not probed: %d" % len(skipped)) for n, why in skipped: print(" %-26s %s" % (n, why)) io.open("probe.json", "w", encoding="utf-8").write(json.dumps({"results": results, "skipped": skipped}, indent=1))