c8d48b8b13
The guard feeds untrusted byte streams (tokenized binaries, remote cat output) into OS-path and shell-text operations; every incident so far (#76762, #77703, #77780, #78256, #77729) was hot-fixed with an except at whichever frame crashed that week. tilllt's regression suite on #79454 showed 4 members of the class still open on merged main. Close the class at three boundaries instead: - _expand_candidate_path(): single ingestion chokepoint for path candidates — reject NUL/empty tokens before any Path OS call and tolerate ValueError/RuntimeError/OSError from expanduser (T1/T2, plus the HOME-unset launchd crash). Both _resolve_terminal_script_path and _resolve_script_path now go through it. - _sanitize_remote_script_text(): apply the local-read contract (NUL = binary = nothing to scan; >1MiB = fail closed) to whatever any read_remote_script callback returns, at the recursion boundary — the guard stops trusting its callbacks (T3/T4). - contains_gateway_lifecycle_command_or_referenced_script() is now total by construction: direct regex scans (pure string ops) run first; the best-effort filesystem walk is wrapped so an unexpected failure logs a warning and falls back to the direct-scan verdict instead of killing every terminal command until gateway restart. terminal_tool's remote fallback also bounds the read at the source (head -c 1MiB+1 instead of cat), so a 166MB ELF never crosses the wire — the superlinear-shlex 30-minute stall from #79838's field report drops to a 0.02s fail-closed verdict. Regression tests: tilllt's T1-T4 adopted verbatim, plus an adversarial never-raises sweep (NUL paths, unset HOME, over-long paths) and a walk-crash fallback test.