From 5d5a2b4f38e2127c0874f4c8f38616389816ffcc Mon Sep 17 00:00:00 2001 From: "melodic-automation-lanes[bot]" <337595936+melodic-automation-lanes[bot]@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:50:06 +0000 Subject: [PATCH 1/5] fix(disk-hygiene): engine gate defers simple readers that only quote the engine name Co-Authored-By: Claude Sonnet 5.5 --- .../disk-hygiene/work-issue-6242-7d2e4b1a.md | 7 ++ .../skills/clean/scripts/destructive_guard.py | 106 +++++++++++++++++- .../skills/clean/scripts/test_hygiene.py | 63 ++++++++++- 3 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 .changes/disk-hygiene/work-issue-6242-7d2e4b1a.md diff --git a/.changes/disk-hygiene/work-issue-6242-7d2e4b1a.md b/.changes/disk-hygiene/work-issue-6242-7d2e4b1a.md new file mode 100644 index 00000000000..b0b3253ffa1 --- /dev/null +++ b/.changes/disk-hygiene/work-issue-6242-7d2e4b1a.md @@ -0,0 +1,7 @@ +--- +bump: patch +--- + +### Fixed + +- **The engine gate no longer denies a simple reader command that only quotes the engine file name.** `gh issue create --title '... hygiene.py ...'` and the other commands on a closed reader allowlist (`cat`, `head`, `tail`, `wc`, `grep`, `rg` without `--pre`, `git show`/`log`/`diff`/`grep` without `-c` or `-O`, `gh issue`/`pr`) now treat single-quoted and `$`/backtick-free double-quoted literals as data. A pipeline, `awk`, `sed`, any unquoted operator, and a word that resolves to the engine still gate. Refs #6242. diff --git a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py index 2da7120e7f3..92f5acd1627 100755 --- a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py +++ b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py @@ -843,6 +843,91 @@ def _powershell_without_string_data(command: str) -> str | None: return None if command_word in blocked else stripped +# Outside string data: expansion, escape, comment, statement-separator, grouping +# and redirection syntax. Any of them keeps the gate, so a pipeline or a second +# command never reads as one simple reader. +_BASH_CODE_CHARS = frozenset("$`\\#;&|<>(){}\r\n") +# Reader command words whose arguments cannot run a string. ``git`` and ``gh`` +# are further limited to the subcommands in ``_BASH_READER_SUBCOMMANDS``. +_BASH_READERS = frozenset({"cat", "head", "tail", "wc", "grep", "rg", "git", "gh"}) +_BASH_READER_SUBCOMMANDS = { + "git": frozenset({"show", "log", "diff", "grep"}), + "gh": frozenset({"issue", "pr"}), +} +_BASH_QUOTE_STRIP = str.maketrans("", "", "'\"") + + +def _bash_without_string_data(command: str) -> str | None: + """``command`` with each string literal replaced by ``_``, or None to keep it. + + The Bash counterpart of ``_powershell_without_string_data``, narrower + because Bash quoting is easier to misread. Returns a rewrite only for ONE + simple command (no unquoted operator, expansion, escape, comment or + newline, so a pipeline stays fail-closed) whose command word is a bare name + on the closed reader allowlist: ``cat``, ``head``, ``tail``, ``wc``, + ``grep``, ``rg`` without ``--pre``, ``git show``/``log``/``diff``/``grep`` + without ``-c`` or ``-O``/``--open-files-in-pager``, and ``gh issue``/``pr``. + Single-quoted literals are blanked as data, double-quoted ones only when + they hold no ``$`` or backtick. None, meaning the caller must classify the + command as written, for anything else: a quoted or path-qualified command + word, a reader outside the allowlist (``awk`` and ``sed`` can run a quoted + program), a git option before the subcommand, an unterminated literal. + """ + if "\\" in command: + # An escaped quote in a double-quoted literal flips the quote parity of + # a scanner that does not model it, hiding a real separator. + return None + out: list[str] = [] + index = 0 + while index < len(command): + char = command[index] + if char in {"'", '"'}: + end = command.find(char, index + 1) + if end < 0: + return None + if char == '"' and ("$" in command[index:end] or "`" in command[index:end]): + return None + out.append("_") + index = end + 1 + continue + if char in _BASH_CODE_CHARS: + return None + out.append(char) + index += 1 + words = "".join(out).split() + if not words or command.lstrip(" \t")[:1] in {"'", '"'}: + return None + if words[0] not in _BASH_READERS: + return None + subcommands = _BASH_READER_SUBCOMMANDS.get(words[0]) + if subcommands is not None and (len(words) < 2 or words[1] not in subcommands): + return None + # Read the options with the quotes removed: ``rg '--pre' sh`` is ``--pre``. + if any( + _bash_reader_option_runs_program(words[0], word) + for word in command.translate(_BASH_QUOTE_STRIP).split()[1:] + ): + return None + return "".join(out) + + +def _bash_reader_option_runs_program(reader: str, word: str) -> bool: + """Whether ``word`` may be an option that makes ``reader`` run a program. + + Conservative: git's short options cluster (``-nO``) and its long + options abbreviate to any unique prefix (``--open``), so a short cluster + holding ``c`` or ``O`` and any prefix of ``--open-files-in-pager`` count. + """ + if reader == "rg": + return word.startswith(("--pre", "--hostname-bin")) + if reader != "git" or not word.startswith("-"): + return False + if word.startswith("--"): + name = word.split("=", 1)[0] + return len(name) > 2 and "--open-files-in-pager".startswith(name) + return "c" in word or "O" in word + + def _engine_gate_relevant(command: str, tool_name: str = "Bash") -> bool: """Decide whether the plugin-level engine gate should act on ``command``. @@ -894,6 +979,19 @@ def _engine_gate_relevant(command: str, tool_name: str = "Bash") -> bool: executor list does not name that runs a string naming the engine (``ssh host './hygiene.py scan'``) reads as data. + On Bash the same holds for ONE simple reader command only + (``_bash_without_string_data``): ``cat``, ``head``, ``tail``, ``wc``, + ``grep``, ``rg`` without ``--pre``, ``git show``/``log``/``diff``/``grep`` + without ``-c`` or ``-O``/``--open-files-in-pager``, and ``gh issue``/``pr``, + with no unquoted operator, and only single-quoted and ``$``/backtick-free + double-quoted literals blanked. So ``gh issue create --title '... hygiene.py + ...'`` defers. A pipeline of readers, ``awk`` and ``sed`` (a quoted program + can run code) stay classified as written, and identity is again checked + before blanking. Accepted residual: the gate reads ``gh`` by its first two + words, so a ``gh`` alias that runs a shell command (``gh alias set --shell`` + or a ``!`` expansion) is not modelled; the allowlist names only ``issue`` + and ``pr``. + A word that is the SAME FILE as the bundled engine — a symlink or hard link under any name — gates regardless of its filename. Without the marker, each distinct whitespace token (or ``_literal_shell_words`` word) is read two @@ -946,8 +1044,12 @@ def _same_file_as_bundled(word: str) -> bool: return _samefile(word) or (not os.path.isabs(word) and _in_engine_dir(word)) allow_backslash = tool_name == "PowerShell" - data_free = _powershell_without_string_data(command) if allow_backslash else None - if data_free is not None: + data_free = ( + _powershell_without_string_data(command) + if allow_backslash + else _bash_without_string_data(command) + ) + if data_free is not None and (allow_backslash or data_free != command): # Identity is read on the command AS WRITTEN, before any literal is # blanked: a word naming one of this plugin's engines gates even when # it sits in string data. diff --git a/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py b/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py index 6859508c3f8..6129b6487b0 100755 --- a/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py +++ b/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py @@ -10781,12 +10781,71 @@ def test_engine_gate_gates_a_relative_word_that_resolves_to_the_engine( self._gated_reason(command), ) + def test_engine_gate_defers_a_simple_reader_that_only_quotes_the_engine_name( + self, + ) -> None: + """#6242: quoted data naming the engine is a mention for a closed reader set.""" + with tempfile.TemporaryDirectory() as tmp, chdir_context(tmp): + for command in ( + "gh issue create --title 'gate denies (only name hygiene.py)'" + " --body-file body.md", + "gh issue create --title 'x' --body 'run python3 hygiene.py scan'", + 'gh issue list --search "python3 hygiene.py"', + "gh pr view 5 --comment 'hygiene.py scan (see)'", + "git log --grep 'hygiene.py (engine)'", + "git diff -- 'hygiene.py'", + "cat 'notes (hygiene.py).md'", + "rg 'python3 hygiene.py' README.md", + "gh issue create --body 'line one\nhygiene.py\nline three'", + ): + with self.subTest(command=command): + self.assertFalse(guard._engine_gate_relevant(command, "Bash")) + + def test_engine_gate_keeps_gating_what_the_string_data_rule_does_not_cover( + self, + ) -> None: + engine = guard._display_path(guard._engine_script_path()) + with tempfile.TemporaryDirectory() as tmp, chdir_context(tmp): + for command in ( + # A pipeline stays fail-closed. + "git show 'hygiene.py (x)' | grep foo", + "gh issue create --title 'hygiene.py' && echo done", + "gh issue create --title 'hygiene.py'; echo done", + # Readers outside the allowlist read as written. + "awk '/hygiene.py/ {print}' notes.txt", + "sed -n '/hygiene.py (x)/p' notes.txt", + "echo 'python3 hygiene.py scan (x)'", + "bash -c 'python3 hygiene.py scan'", + # A double-quoted literal holding an expansion is not data. + 'gh issue create --title "hygiene.py $(whoami)"', + 'gh issue create --title "python3 hygiene.py $X"', + 'gh issue create --title "python3 hygiene.py `x`"', + # Options that make a reader run another program. + "rg --pre 'python3 hygiene.py (x)' foo", + "rg '--pre' 'python3 hygiene.py (x)' foo", + "git -c 'alias.x=!python3 hygiene.py' show HEAD", + "git grep -O'python3 hygiene.py (x)' foo", + 'grep "a\\" "; python3 hygiene.py apply; echo \\"', + "git grep -nO'python3 hygiene.py apply' x", + "git grep --open-files-in-p 'python3 hygiene.py apply' x", + "git grep --open 'python3 hygiene.py apply' x", + "rg --hostname-bin 'python3 hygiene.py apply' x", + # A subcommand outside the allowlist. + "gh api -X POST 'hygiene.py (x)'", + "git push 'hygiene.py (x)'", + # Identity is read on the command as written. + f"cat '{engine}'", + f'gh issue create --body "{engine}"', + f"head -n 3 '{engine}' ", + ): + with self.subTest(command=command): + self.assertTrue(guard._engine_gate_relevant(command, "Bash")) + def test_engine_gate_denial_names_the_payload_word_it_gated(self) -> None: cases = ( - 'gh issue list --search "python3 hygiene.py"', - 'gh issue list --search "hygiene.py scan"', 'echo "run python3 hygiene.py"', '"python3 hygiene.py scan"', + 'echo "hygiene.py scan"', ) with tempfile.TemporaryDirectory() as tmp, chdir_context(tmp): for command in cases: From edf16c644a4f950a2c5957423e1866638720e4b6 Mon Sep 17 00:00:00 2001 From: "melodic-automation-lanes[bot]" <337595936+melodic-automation-lanes[bot]@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:55:17 +0000 Subject: [PATCH 2/5] fix(disk-hygiene): identity-check marker-free quoted words before blanking A symlink to the engine under a name without the marker, written in quotes, was blanked to `_` and never identity-checked. Run the same all-tokens _samefile/_in_engine_dir scan over the command as written before blanking. Co-Authored-By: Claude Sonnet 5.5 --- .../skills/clean/scripts/destructive_guard.py | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py index 92f5acd1627..e13670d8def 100755 --- a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py +++ b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py @@ -1043,6 +1043,18 @@ def _same_file_as_bundled(word: str) -> bool: """ return _samefile(word) or (not os.path.isabs(word) and _in_engine_dir(word)) + engine_drive = os.path.splitdrive(bundled.parent)[0].casefold() + + def _names_engine(candidate: str) -> bool: + # As written, or joined to the engine's directory when relative, + # non-empty and carrying no drive or the engine's own. + return _samefile(candidate) or ( + bool(candidate) + and not os.path.isabs(candidate) + and os.path.splitdrive(candidate)[0].casefold() in {"", engine_drive} + and _in_engine_dir(candidate) + ) + allow_backslash = tool_name == "PowerShell" data_free = ( _powershell_without_string_data(command) @@ -1060,6 +1072,19 @@ def _same_file_as_bundled(word: str) -> bool: for candidate in (token, word) ): return True + # Marker-free names too: a link to the engine under a name without the + # marker, written in quotes, would otherwise be blanked to `_` and + # never identity-checked. Same all-tokens scan as the marker-free + # branch below, over the command as written. + written_words = _literal_shell_words(command, allow_backslash=allow_backslash) + if any( + _names_engine(candidate) + for candidate in dict.fromkeys( + [token.strip("'\"") for token in command.split()] + + ([] if written_words is None else list(written_words)) + ) + ): + return True command = data_free marker_candidates = _marker_tokens(command) if not any(_carries_marker(token) for token in marker_candidates): @@ -1093,17 +1118,7 @@ def _same_file_as_bundled(word: str) -> bool: if words is None else list(words) ) - engine_drive = os.path.splitdrive(bundled.parent)[0].casefold() - return any( - _samefile(candidate) - or ( - bool(candidate) - and not os.path.isabs(candidate) - and os.path.splitdrive(candidate)[0].casefold() in {"", engine_drive} - and _in_engine_dir(candidate) - ) - for candidate in dict.fromkeys(candidates) - ) + return any(_names_engine(candidate) for candidate in dict.fromkeys(candidates)) words = _literal_shell_words(command, allow_backslash=allow_backslash) if words is None: # Marker present but not literally parseable (operators, compounds). From c0d3ed420947f44c13fe1b68fe401b74c0055c37 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Sun, 18 Oct 2026 06:50:10 -0400 Subject: [PATCH 3/5] fix(disk-hygiene): read marker-free quoted words by identity as written The pre-blank scan used _names_engine, which joins bare tokens to the engine directory, so any quoted `hygiene.py` mention gated and the PowerShell string-data deferral regressed. Check _samefile instead: a path resolving from the working directory still gates. Co-Authored-By: Claude Opus 5.5 --- plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py index e13670d8def..9d690a205aa 100755 --- a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py +++ b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py @@ -1078,7 +1078,7 @@ def _names_engine(candidate: str) -> bool: # branch below, over the command as written. written_words = _literal_shell_words(command, allow_backslash=allow_backslash) if any( - _names_engine(candidate) + _samefile(candidate) for candidate in dict.fromkeys( [token.strip("'\"") for token in command.split()] + ([] if written_words is None else list(written_words)) From c233623e25ac92ba322ee7114ffe9ed1dc6fd1a9 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Sun, 18 Oct 2026 06:55:15 -0400 Subject: [PATCH 4/5] fix(disk-hygiene): spell modeled the way the typos check expects Co-Authored-By: Claude Opus 5.5 --- plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py index 9d690a205aa..36a514ffa99 100755 --- a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py +++ b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py @@ -989,7 +989,7 @@ def _engine_gate_relevant(command: str, tool_name: str = "Bash") -> bool: can run code) stay classified as written, and identity is again checked before blanking. Accepted residual: the gate reads ``gh`` by its first two words, so a ``gh`` alias that runs a shell command (``gh alias set --shell`` - or a ``!`` expansion) is not modelled; the allowlist names only ``issue`` + or a ``!`` expansion) is not modeled; the allowlist names only ``issue`` and ``pr``. A word that is the SAME FILE as the bundled engine — a symlink or hard link From 0772effdd23dc676c8e73f19db5347d424b84101 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Sun, 18 Oct 2026 12:55:47 -0400 Subject: [PATCH 5/5] test(disk-hygiene): cover the quoted marker-free link scan on both shells Add a test that a quoted word naming a hard link to the engine gates a gh reader on Bash and PowerShell, with a stay-quiet case before the link exists. The `@@` name owns no marker token, so only the whole-word scan sees it and removing that scan now fails the test. Add a `git grep -nc` case so the `-c` half of the git option check has a failing subtest. State in the gate docstring and the changelog fragment that the scan runs for both shells. Co-Authored-By: Claude Opus 5.5 --- .../disk-hygiene/work-issue-6242-7d2e4b1a.md | 2 +- .../skills/clean/scripts/destructive_guard.py | 4 ++++ .../skills/clean/scripts/test_hygiene.py | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.changes/disk-hygiene/work-issue-6242-7d2e4b1a.md b/.changes/disk-hygiene/work-issue-6242-7d2e4b1a.md index b0b3253ffa1..3f0cf2f6260 100644 --- a/.changes/disk-hygiene/work-issue-6242-7d2e4b1a.md +++ b/.changes/disk-hygiene/work-issue-6242-7d2e4b1a.md @@ -4,4 +4,4 @@ bump: patch ### Fixed -- **The engine gate no longer denies a simple reader command that only quotes the engine file name.** `gh issue create --title '... hygiene.py ...'` and the other commands on a closed reader allowlist (`cat`, `head`, `tail`, `wc`, `grep`, `rg` without `--pre`, `git show`/`log`/`diff`/`grep` without `-c` or `-O`, `gh issue`/`pr`) now treat single-quoted and `$`/backtick-free double-quoted literals as data. A pipeline, `awk`, `sed`, any unquoted operator, and a word that resolves to the engine still gate. Refs #6242. +- **The engine gate no longer denies a simple reader command that only quotes the engine file name.** `gh issue create --title '... hygiene.py ...'` and the other commands on a closed reader allowlist (`cat`, `head`, `tail`, `wc`, `grep`, `rg` without `--pre`, `git show`/`log`/`diff`/`grep` without `-c` or `-O`, `gh issue`/`pr`) now treat single-quoted and `$`/backtick-free double-quoted literals as data. A pipeline, `awk`, `sed`, any unquoted operator, and a word that resolves to the engine still gate. On Bash and PowerShell alike, a quoted word that is a link to the engine under any name now gates too. Refs #6242. diff --git a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py index 36a514ffa99..a38438a79bc 100755 --- a/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py +++ b/plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py @@ -992,6 +992,10 @@ def _engine_gate_relevant(command: str, tool_name: str = "Bash") -> bool: or a ``!`` expansion) is not modeled; the allowlist names only ``issue`` and ``pr``. + On both shells, the identity check before blanking reads every word, not + only those carrying the marker, so a quoted link to the engine under any + name (``gh issue create --title x 'alias'``) still gates. + A word that is the SAME FILE as the bundled engine — a symlink or hard link under any name — gates regardless of its filename. Without the marker, each distinct whitespace token (or ``_literal_shell_words`` word) is read two diff --git a/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py b/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py index 6129b6487b0..22f70a67fd3 100755 --- a/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py +++ b/plugins/disk-hygiene/skills/clean/scripts/test_hygiene.py @@ -10827,6 +10827,7 @@ def test_engine_gate_keeps_gating_what_the_string_data_rule_does_not_cover( "git grep -O'python3 hygiene.py (x)' foo", 'grep "a\\" "; python3 hygiene.py apply; echo \\"', "git grep -nO'python3 hygiene.py apply' x", + "git grep -nc'python3 hygiene.py apply' x", "git grep --open-files-in-p 'python3 hygiene.py apply' x", "git grep --open 'python3 hygiene.py apply' x", "rg --hostname-bin 'python3 hygiene.py apply' x", @@ -11792,6 +11793,25 @@ def test_engine_gate_bare_link_outside_engine_dir_gates(self) -> None: assert result is not None self.assertEqual("deny", result["hookSpecificOutput"]["permissionDecision"]) + def test_engine_gate_quoted_marker_free_link_gates_a_reader(self) -> None: + """A quoted word naming a marker-free link to the engine gates a reader. + + Blanking string data would hide the quoted name from the marker-free + branch, so identity is read on the command as written, for both shells. + `@@` holds no path-legal character, so no marker token reaches it and + only the whole-word scan sees it. + """ + for name in ("cleanup", "@@"): + command = f"gh issue create --title 'x' '{name}'" + for shell in ("Bash", "PowerShell"): + with self.subTest(name=name, shell=shell): + with tempfile.TemporaryDirectory(dir=SCRIPT_DIR) as tmp: + with chdir_context(tmp): + gated = guard._engine_gate_relevant + self.assertFalse(gated(command, shell)) + self._hard_link_or_skip(Path(tmp) / name) + self.assertTrue(gated(command, shell)) + def test_engine_gate_marker_free_probes_each_distinct_word_twice(self) -> None: """Counted `os.path.samefile` calls for marker-free commands.