"""Redact every credential-body fragment from a transcript, then re-verify. Why this exists (framework lesson, this run): a scrubber's leak scan detects the *token*, not a FRAGMENT of it. An independent substring probe run on this turn's own cut found an 8-character run of the live credential body inside the text of an inlined verification command -- the probe's own literal had been typed into a shell command and the tool input is part of the log. The scrubber reported `leaks: []` on that same file, so the scan is not sufficient on its own. `sahtool scrub` is run first (it strips token-shaped strings and the credential as a whole); this script is the second pass and replaces every maximal run of >= MIN_RUN consecutive characters of the credential body with a placeholder, then re-runs the fragment test to prove it is gone. Usage: python redact-credential-fragments.py --in --out [--credential ] [--min-run 6] Exit 0 only when the output parses as JSONL line by line, contains no fragment of the body of length >= MIN_RUN, and contains no `sah_` token-shaped string. """ import argparse import json import os import re import sys PLACEHOLDER = "[redacted-credential-fragment]" def load_credential(path): if path and os.path.exists(path): return open(path, encoding="utf-8").read().strip() env = os.environ.get("SOLVEATHOME_TOKEN") if env: return env.strip() raise SystemExit("no credential available: pass --credential or set SOLVEATHOME_TOKEN") def body_of(token): return token.split("_", 1)[1] if "_" in token else token def maximal_runs(text, body, min_run): """Every maximal run of `body` present in `text`, as (start, length) in descending length.""" found = {} i = 0 while i < len(body): j = i while j < len(body) and body[i:j + 1] in text: j += 1 n = j - i if n >= min_run: frag = body[i:i + n] found[frag] = text.count(frag) i = max(j, i + 1) return found def main(): ap = argparse.ArgumentParser() ap.add_argument("--in", dest="infile", required=True) ap.add_argument("--out", required=True) ap.add_argument("--credential", default=None) ap.add_argument("--min-run", type=int, default=6, dest="min_run") a = ap.parse_args() token = load_credential(a.credential) body = body_of(token) text = open(a.infile, encoding="utf-8").read() before = maximal_runs(text, body, a.min_run) literal = token in text if literal: text = text.replace(token, PLACEHOLDER) for frag in sorted(before, key=len, reverse=True): text = text.replace(frag, PLACEHOLDER) with open(a.out, "w", encoding="utf-8", newline="\n") as fh: fh.write(text) # Independent re-verification of the OUTPUT, not of the substitution's own report. out = open(a.out, encoding="utf-8").read() after = maximal_runs(out, body, a.min_run) lines = [l for l in out.splitlines() if l.strip()] parse_err = [] usage = [] for i, l in enumerate(lines): try: d = json.loads(l) except Exception as exc: parse_err.append("line %d: %s" % (i + 1, exc)) continue if d.get("usage"): usage.append(d["usage"]) # A `sah_`-shaped string is only a leak when it could BE the credential: long enough to be # one, or carrying a run of the live body. `sah_hits` (this run's own probe field name) is # not a credential and must not fail the gate, or the gate becomes noise nobody reads. sah_all = [m.group(0) for m in re.finditer(r"sah_([A-Za-z0-9_-]{1,})", out)] sah_suspect = [s for s in sah_all if len(s) - 4 >= 16 or any(f in s for f in maximal_runs(out, body, a.min_run))] res = { "in": a.infile, "out": a.out, "in_bytes": len(text.encode()), "literal_token_present_before": literal, "fragments_redacted": {k: v for k, v in sorted(before.items(), key=lambda kv: -len(kv[0]))}, "fragments_min_len": a.min_run, "after_fragments": after, "after_sah_shaped": len(sah_all), "after_sah_suspect": sah_suspect, "lines": len(lines), "bad_json": parse_err[:3], "usage_rows": len(usage), "usage": usage, "ok": not after and not parse_err and not sah_suspect, } print(json.dumps(res, indent=1)) return 0 if res["ok"] else 2 if __name__ == "__main__": sys.exit(main())