diff --git a/host-setup/agent-safety/README.md b/host-setup/agent-safety/README.md index f6e570cfa..e7ba2c332 100644 --- a/host-setup/agent-safety/README.md +++ b/host-setup/agent-safety/README.md @@ -197,7 +197,11 @@ text says, because the harm it covers was never in the text. (`(( SECONDS < 600 ))`). A nested loop is judged on its own terms, so an unbounded inner wait is denied inside a bounded outer one, which is what it is. A heredoc body is data rather than a command line and is skipped, except one fed to a shell, which is the script that shell runs, so a - document quoting the forbidden shape is written rather than denied. + document quoting the forbidden shape is written rather than denied. A line holding `((` beside + a `<<` is read once as opening nothing and once per `<<` whose tag the rule accepts and whose + non-empty body a later line closes, since an arithmetic shift tokenizes as a redirection does, and any + reading holding an unbounded wait denies the command. A command with more readings than the + rule builds is denied when it names both `sleep` and a loop keyword, since a wait needs both. A `for` loop in its arithmetic form, `for ((;;))`, is reached too, since it runs forever exactly as `while true` does, while a `for x in ` is bounded by its own word list. The third is a loop whose condition is a diff --git a/host-setup/agent-safety/claude/gh-write-guard.py b/host-setup/agent-safety/claude/gh-write-guard.py index d440c66a3..5a10f9792 100755 --- a/host-setup/agent-safety/claude/gh-write-guard.py +++ b/host-setup/agent-safety/claude/gh-write-guard.py @@ -81,7 +81,10 @@ against a condition that could never become true, until they were killed by PID by hand. A shell started by a tool call runs in its own session, so it survives the agent that started it and nothing reaps it. A heredoc body is data rather than a command line and is skipped, except one fed to a - shell, which is the script that shell runs. + shell, which is the script that shell runs. A line holding `((` beside a `<<` is read once as + opening nothing and once per `<<` whose tag it accepts and whose non-empty body a later line + closes, and any reading holding an unbounded wait denies. A command with more readings than the rule builds + is denied unread when it names both `sleep` and a loop keyword. Run `gh-write-guard.py --selftest` to verify the decision matrix without Claude Code. """ @@ -2717,41 +2720,9 @@ def _ends_pipeline(tok): return _is_separator(tok) and tok != "|" -def _heredoc_opener(line): - """(tag, is_dash_form, feeds_a_shell) for a heredoc `line` opens, or None. - - Read from tokens rather than from the raw text, so a `<<` inside a quoted argument is the - text it is: a commit message explaining the `<< EOF` form opened one, and the strip then - deleted every command after it. A `<<<` herestring is a token of its own and not this, and a - line carrying arithmetic is skipped outright, since `$(( 1 << n ))` tokenizes to a bare `<<` - that no token test can tell from a redirection. Skipping there costs a false deny on a line - holding both, which is vanishingly rare, where reading it wrong drops real commands. - - The line is read twice, with its comments dropped and with a `#` read as text, and opens a - heredoc only where both readings find one, taking the first reading's. The first keeps a `<<` - inside a comment from opening one. A reading that does not parse is replaced by the line as - `_operator_tokens` reads it. - """ - readings = [] - for comments in (False, True): - try: - readings.append(_context_lex(line, comments)) - except (ValueError, RecursionError): - readings.append(_operator_tokens(line)) - if _heredoc_opener_in(readings[0]) is None: - return None - return _heredoc_opener_in(readings[1]) - - -def _line_heredoc_opener(line): - """`_heredoc_opener` as it read a line before `_context_lex`, through `_operator_tokens` alone.""" - return _heredoc_opener_in(_operator_tokens(line)) - - -def _heredoc_opener_in(toks): - """(tag, is_dash_form, feeds_a_shell) for the heredoc a line's `toks` open, or None.""" - if any("((" in t for t in toks): - return None +def _heredoc_openers_in(toks): + """Every heredoc a line's `toks` can open, in order, each (tag, is_dash_form, feeds_a_shell).""" + openers = [] for k, tok in enumerate(toks): if tok != "<<" or k + 1 >= len(toks): continue @@ -2776,55 +2747,146 @@ def _heredoc_opener_in(toks): end = k while end < len(toks) and not _ends_pipeline(toks[end]): end += 1 - return tag, dash, any(_is_shell_wrapper_exe(t) for t in toks[start:end]) - return None + openers.append((tag, dash, any(_is_shell_wrapper_exe(t) for t in toks[start:end]))) + return openers + + +def _heredoc_forks(toks): + """The ways a line's `toks` can open a heredoc whose body is data, each a (tag, is_dash_form) or None. + + None is the reading where the line strips no body, either because it opens none or because + the one it opens is fed to a shell. A line holding no `((` has one reading. A line holding one + has a reading per `<<` beside None, since `$(( 1 << n ))` tokenizes to a bare `<<` that no token + test can tell from a redirection. Skipping such a line outright kept a real heredoc's body as + commands, and a comparison in that body then vouched for a wait loop whose condition carried no + bound. + """ + openers = [None if fed else (tag, dash) for tag, dash, fed in _heredoc_openers_in(toks)] + if any("((" in t for t in toks): + return list(dict.fromkeys([None, *openers])) + return openers[:1] or [None] + + +def _heredoc_openers(line): + """`_heredoc_forks` for `line`, read from tokens rather than from the raw text. + + Tokens keep a `<<` inside a quoted argument the text it is: a commit message explaining the + `<< EOF` form opened one, and the strip then deleted every command after it. A `<<<` herestring + is a token of its own and not this. + The line is read twice, with a `#` read as text and with its comments dropped, and opens a + heredoc only where both readings find one, taking the second reading's forks. The second keeps + a `<<` inside a comment from opening one. A reading that does not parse is replaced by the line + as `_operator_tokens` reads it. + """ + readings = [] + for comments in (False, True): + try: + readings.append(_context_lex(line, comments)) + except (ValueError, RecursionError): + readings.append(_operator_tokens(line)) + if not _heredoc_openers_in(readings[0]): + return [None] + return _heredoc_forks(readings[1]) + + +def _line_heredoc_openers(line): + """`_heredoc_openers` as it read a line before `_context_lex`, through `_operator_tokens` alone.""" + return _heredoc_forks(_operator_tokens(line)) + + +_HEREDOC_READING_LIMIT = 16 + + +def _heredoc_body_end(lines, start, opener): + """(the index after the line closing the body `opener` opens at `start`, [that line]). -def _strip_heredoc_bodies(cmd, opener=_heredoc_opener): - """`cmd` with every heredoc body removed, except one fed to a shell, which really is a script. - `opener` decides which lines open a heredoc. + Where no line closes the body, this is (the line count, []). + + A plain `<<` ends only on the tag at column zero, and `<<-` also accepts leading tabs. Accepting + any indentation instead ended the body early on a doc line that merely read as the tag, and the + rest was scanned as commands. + """ + tag, dash = opener + for j in range(start, len(lines)): + if (lines[j].lstrip("\t") if dash else lines[j]) == tag: + return j + 1, [lines[j]] # the terminator line itself is ordinary text again + return len(lines), [] + + +def _heredoc_readings(cmd, openers=_heredoc_openers): + """Every text `cmd` can be with its heredoc bodies removed, or None past the reading limit. + `openers` decides the ways each line opens a heredoc. A heredoc body is data rather than a command line, so `cat > notes.md < _HEREDOC_READING_LIMIT: + return None + pending.append((j, kept + term)) + if first is not None: + i, term = _heredoc_body_end(lines, i, first) + kept.extend(term) + readings.append("\n".join(kept)) + return readings + + +_NAMES_A_LOOP = re.compile(r"\b(?:while|until)\b|\bfor\s*\(\(") def _check_unbounded_wait(cmd): - """Rule 7: deny a `while`/`until` + `sleep` wait carrying no bound in the command text. The - command is judged with its heredoc bodies stripped twice, once by `_heredoc_opener` and once - by `_line_heredoc_opener`, and a loop either leaves standing is denied, so a heredoc the scan - misreads never hides a loop the line reading kept. + """Rule 7: deny a `while`/`until` + `sleep` wait carrying no bound in the command text. + + The command is judged with its heredoc bodies stripped every way `_heredoc_openers` reads them + and every way `_line_heredoc_openers` does, and a reading holding an unbounded wait denies it, so + a heredoc the scan misreads never hides a loop the line reading kept. A command naming no `sleep` + once its line continuations, quotes, backslashes, and `$` signs are removed holds no such wait + in any reading, since every token is its text with some of those removed, so it is allowed + unread. Past the reading limit the command is denied unread only when that text also names a + loop keyword, matched in its case as bash reads one, since a wait needs both. """ - stripped = _strip_heredoc_bodies(cmd) - loop = _unbounded_wait_loop(stripped) - if loop is None: - line_stripped = _strip_heredoc_bodies(cmd, _line_heredoc_opener) - if line_stripped != stripped: - loop = _unbounded_wait_loop(line_stripped) + text = re.sub(r"\\\r?\n|[\"'\\$]", "", cmd) + if "sleep" not in text.lower(): + return "allow", "" + readings = _heredoc_readings(cmd) + line_readings = _heredoc_readings(cmd, _line_heredoc_openers) + if readings is None or line_readings is None: + if not _NAMES_A_LOOP.search(text): + return "allow", "" + return "deny", ( + "This command names `sleep` and a loop keyword and holds more lines where `<<` could " + "open a heredoc or shift inside `(( ))` than the unbounded-wait rule reads every way a " + 'shell might. Split it into shorter commands. See AGENTS.md "Delegation".' + ) + candidates = dict.fromkeys(readings + line_readings) + loop = next((found for found in map(_unbounded_wait_loop, candidates) if found), None) if loop is None: return "allow", "" return "deny", ( @@ -4751,6 +4813,85 @@ def classify( "deny", "nor a spaced one, which a raw-text read took for an opener and dropped the command after", ), + ( + "while [ -e /x ] < a.md < b.md < "part$((N+1)).md" < "part$((N+1)).md" <