#!/usr/bin/env python3 """Job #3643: check the served producer still runs and reproduces its embedded block. The note under revision (`research/history/staging/attack-0830-varE-identification.md`) carries no executable and no embedded hash of itself; its numbers come from the served producer `research/history/staging/attack-0830-varE-identification.js`, which embeds its own past stdout as a comment block together with `out-sha256`, `body-lines`, the invocation and the node version it was produced on. This script: 1. pins the producer's sha256 (the sha the served docs page publishes); 2. hashes the stdout of a fresh `node producer.js` run and compares it with the embedded `out-sha256` and the embedded `body-lines`; 3. compares the freshly produced stdout with the embedded reference block line by line, byte for byte after the `// ` comment prefix. Exit 0 if all checks hold, 2 if any fails. Nothing here writes outside its own folder. """ import hashlib import json import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) PRODUCER = os.path.join(HERE, "producer", "producer.js") STDOUT = os.path.join(HERE, "producer", "producer-run.out") STDERR = os.path.join(HERE, "producer", "producer-run.err") PRODUCER_SHA = "bda7092d135aaf1489b3531ab12b5c4afb2455a51c6ab6cfe11901252d948d97" EMBEDDED_OUT_SHA = "5753ec386d70f25f0576504556d9d11ec531350cb4fec5a46bf86164d8d4eedc" EMBEDDED_BODY_LINES = 244 def sha256_bytes(b: bytes) -> str: return hashlib.sha256(b).hexdigest() def main() -> int: sys.stdout.reconfigure(newline="\n", encoding="utf-8", errors="replace") checks = [] def check(name, ok, detail): checks.append({"check": name, "ok": bool(ok), "detail": detail}) print("%-42s %s %s" % (name, "ok " if ok else "FAIL", detail)) with open(PRODUCER, "rb") as fh: raw = fh.read() check("producer sha256 is the served one", sha256_bytes(raw) == PRODUCER_SHA, sha256_bytes(raw)[:16] + "...") text = raw.decode("utf-8") with open(STDOUT, "rb") as fh: out_raw = fh.read() with open(STDERR, "rb") as fh: err_raw = fh.read() run_sha = sha256_bytes(out_raw) out_lines = out_raw.decode("utf-8").split("\n") if out_lines and out_lines[-1] == "": out_lines.pop() check("a fresh run wrote nothing to stderr", err_raw == b"", "%d bytes" % len(err_raw)) check("stdout sha256 equals the embedded out-sha256", run_sha == EMBEDDED_OUT_SHA, run_sha[:16] + "...") check("stdout body-lines equals the embedded body-lines", len(out_lines) == EMBEDDED_BODY_LINES, "%d lines" % len(out_lines)) # The embedded reference block: the run header, then the body, then the trailing # separators. Body = commented lines after the header's final separator, up to the # separator that introduces the READINGS prose. lines = text.split("\n") hdr = next(i for i, l in enumerate(lines) if l.startswith("// out-sha256:")) body_start = next(i for i in range(hdr, len(lines)) if set(lines[i]) == set("=/") or re.fullmatch(r"// =+", lines[i])) body_start += 1 body_end = next(i for i in range(body_start, len(lines)) if re.fullmatch(r"// =+", lines[i])) ref = [] for l in lines[body_start:body_end]: if l == "//": ref.append("") elif l.startswith("// "): ref.append(l[3:]) elif l.startswith("//"): ref.append(l[2:]) else: check("reference block is commented only", False, repr(l[:40])) ref.append(l) same = ref == out_lines first_diff = None for i, (a, b) in enumerate(zip(ref, out_lines)): if a != b: first_diff = (i + 1, a, b) break check("fresh stdout equals the embedded reference block, byte for byte", same, "%d lines compared" % len(ref) if same else "first diff at line %r" % (first_diff,)) ok = all(c["ok"] for c in checks) print(json.dumps({"ok": ok, "checks": checks}, indent=1)) return 0 if ok else 2 if __name__ == "__main__": raise SystemExit(main())