"""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 longest run of `body` starting at each index of `body` that occurs in `text`. The first version of this function advanced `i` past each run it found, so after a FULL-body occurrence at i=0 it never looked at i=1: the 8-character head of the body survived inside shorter, truncated copies (a `sah_`-prefixed probe literal is exactly that shape), and the function's own re-check reported them while the redaction loop had already sorted them away. Every index is now visited, so a truncated copy yields its own (shorter) run. """ found = {} for i in range(len(body)): best = 0 for n in range(min_run, len(body) - i + 1): if body[i:i + n] in text: best = n if best: frag = body[i:i + best] found[frag] = text.count(frag) 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) # A replacement can EXPOSE a shorter run that was not present before it (removing the longest # occurrence can leave a stranded head), so re-run the scan and the substitution to a fixed # point, then verify on the output below. passes = 1 while True: again = maximal_runs(text, body, a.min_run) if not again: break for frag in sorted(again, key=len, reverse=True): text = text.replace(frag, PLACEHOLDER) before.update(again) passes += 1 if passes > 8: break 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]))}, "passes": passes, "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())