From f311111a88573cb2a5f1884358392923d8536f4a Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Wed, 30 Sep 2026 04:21:31 -0700 Subject: [PATCH 1/2] End the Guard's Stdin Redirect Scan at a Reserved Word The scan of the tokens after a loop's `done` stopped only at a separator, so a redirect on the next command inside an enclosing compound read as the loop's own input bound. It now also stops at a reserved word, which ends the loop's command the same way. A closing word is passed over, since a redirect after one binds a compound that encloses the loop. Co-Authored-By: Claude Opus 5.5 --- .../agent-safety/claude/gh-write-guard.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/host-setup/agent-safety/claude/gh-write-guard.py b/host-setup/agent-safety/claude/gh-write-guard.py index 5a10f9792..8457733c7 100755 --- a/host-setup/agent-safety/claude/gh-write-guard.py +++ b/host-setup/agent-safety/claude/gh-write-guard.py @@ -2329,6 +2329,30 @@ def _names_a_stream(target): return posixpath.normpath(re.sub(r"^/+", "/", target)).startswith(("/dev/", "/proc/")) +_NON_CLOSING_RESERVED_WORDS = frozenset( + { + "!", + "case", + "coproc", + "do", + "elif", + "else", + "for", + "function", + "if", + "in", + "select", + "then", + "time", + "until", + "while", + "{", + "[[", + "]]", + } +) + + def _redirects_stdin(after_done): """True if `after_done` binds descriptor 0 to a source that ends, which a `read` drains. @@ -2341,6 +2365,11 @@ def _redirects_stdin(after_done): The last binding is what counts, not the first to qualify. A shell applies redirections in order and each replaces the last, so `< in.txt < /dev/zero` reads the stream. + + The loop's command ends at a reserved word as it does at a separator, so the `< f` in + `if while read l; do sleep 30; done then echo x < f; fi` binds the `echo`. + A closing word, one of `}`, `fi`, `done`, and `esac`, is passed over rather than ending it, + since a redirect after one binds a compound enclosing the loop, which the loop reads from. """ bound = False i = 0 @@ -2350,6 +2379,8 @@ def _redirects_stdin(after_done): # `yes | while read l; do sleep 30; done; cat < f` is fed by the pipe. if _is_separator(tok): return bound + if tok in _NON_CLOSING_RESERVED_WORDS: + return bound fd = "" # A descriptor carries as its own token, so `2>&1 < f` arrives as five. # Reading the token before the `<` as a descriptor read the previous redirect's target as one. @@ -5332,6 +5363,26 @@ def classify( "deny", "a redirect on a later command binds nothing this loop reads", ), + ( + "if while read l; do sleep 30; done then echo x < f; fi", + "deny", + "nor does one after a reserved word, which ends the loop's command as a separator does", + ), + ( + "if true; then while read l; do sleep 30; done else echo x < f; fi", + "deny", + "and an `else` ends it the same way a `then` does", + ), + ( + "if true; then if true; then while read l; do sleep 30; done fi else echo x < f; fi", + "deny", + "even behind a closing word, which the scan passes over", + ), + ( + "{ while read l; do sleep 30; done } < f", + "allow", + "while a redirect after a closing word binds the enclosing compound, which the loop reads", + ), ( "yes | while read l; do sleep 30; done {fd}< f", "deny", From 71d285f3cb8d91b7c6905e044dd2fd7e4457b185 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Wed, 30 Sep 2026 04:24:54 -0700 Subject: [PATCH 2/2] End the Scan at a Closing Word Too A redirect after a closing word binds the compound it closes, and a pipe inside that compound can still feed the loop, so the redirect is no bound the scan can credit. `{ yes | while read l; do sleep 30; done } < f` reads the pipe forever and was allowed. Co-Authored-By: Claude Opus 5.5 --- .../agent-safety/claude/gh-write-guard.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/host-setup/agent-safety/claude/gh-write-guard.py b/host-setup/agent-safety/claude/gh-write-guard.py index 8457733c7..dc530fcbe 100755 --- a/host-setup/agent-safety/claude/gh-write-guard.py +++ b/host-setup/agent-safety/claude/gh-write-guard.py @@ -2329,14 +2329,17 @@ def _names_a_stream(target): return posixpath.normpath(re.sub(r"^/+", "/", target)).startswith(("/dev/", "/proc/")) -_NON_CLOSING_RESERVED_WORDS = frozenset( +_RESERVED_WORDS = frozenset( { "!", "case", "coproc", "do", + "done", "elif", "else", + "esac", + "fi", "for", "function", "if", @@ -2347,6 +2350,7 @@ def _names_a_stream(target): "until", "while", "{", + "}", "[[", "]]", } @@ -2368,8 +2372,8 @@ def _redirects_stdin(after_done): The loop's command ends at a reserved word as it does at a separator, so the `< f` in `if while read l; do sleep 30; done then echo x < f; fi` binds the `echo`. - A closing word, one of `}`, `fi`, `done`, and `esac`, is passed over rather than ending it, - since a redirect after one binds a compound enclosing the loop, which the loop reads from. + That includes a closing word such as `}`, since a pipe inside the compound it closes can feed + the loop, so `{ yes | while read l; do sleep 30; done } < f` reads the pipe. """ bound = False i = 0 @@ -2379,7 +2383,7 @@ def _redirects_stdin(after_done): # `yes | while read l; do sleep 30; done; cat < f` is fed by the pipe. if _is_separator(tok): return bound - if tok in _NON_CLOSING_RESERVED_WORDS: + if tok in _RESERVED_WORDS: return bound fd = "" # A descriptor carries as its own token, so `2>&1 < f` arrives as five. @@ -5376,12 +5380,12 @@ def classify( ( "if true; then if true; then while read l; do sleep 30; done fi else echo x < f; fi", "deny", - "even behind a closing word, which the scan passes over", + "even behind a closing word, which ends it too", ), ( - "{ while read l; do sleep 30; done } < f", - "allow", - "while a redirect after a closing word binds the enclosing compound, which the loop reads", + "{ yes | while read l; do sleep 30; done } < f", + "deny", + "since a redirect after a closing word binds a compound whose pipe can still feed the loop", ), ( "yes | while read l; do sleep 30; done {fd}< f",