fix(hooks): close command-safety bypasses and credential helper output loss

The command-safety hook only checked the first word of each segment, so
destructive commands inside if/for/while blocks, subshells, braces,
PowerShell script blocks, or behind wrapper options (xargs -0, sudo -E,
timeout 30) ran unchecked. A comment with an apostrophe, an escaped quote,
or a line continuation also hid the following command. Skip shell
keywords, split on braces, drop comments, honor backslash escapes, join
continued lines, and check every suffix after a wrapper. CLI mode also
strips the byte order mark that Windows PowerShell 5.1 adds to piped
stdin, which let every command pass there.

with-credential.ps1 returned the child's exit code from a function, so
the child's stdout was captured and the exit code became 0. Run the child
as a statement and exit with its code. add-credentials now refreshes the
installed helper on every run so fixes reach existing machines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Julian LechnerandClaude Opus 5.5 committed 2026-11-19 08:00:18 +01:00
1 parent f5d8f6f11b
commit 63a36d90e0
5 files changed
+77 -19

No files matched your search

+25
View File
@@ -41,6 +41,31 @@ class HookTests(unittest.TestCase):
with self.subTest(command=command):
self.assertBlocked(command)
def test_blocks_commands_hidden_in_control_flow_and_wrapper_options(self):
for command in ('if [ -d build ]; then rm -rf build; fi', 'for d in a b; do rm -rf "$d"; done',
'while true; do git reset --hard; done', '(rm -rf x)', '{ git reset --hard; }', '! rm -rf x',
'find . -print0 | xargs -0 rm -rf', 'sudo -E rm -rf /x', 'env -i git reset --hard',
'timeout 30 rm -rf x', 'nice -n 5 rm -rf x', 'rm -rf ${dir}', 'echo ${#arr[@]}; rm -rf x',
"sudo -u bob bash -c 'rm -rf x'"):
with self.subTest(command=command):
self.assertBlocked(command)
for command in ('Get-ChildItem bin -Recurse | ForEach-Object { Remove-Item $_ -Recurse -Force }',
'gci bin | % { ri $_ -Recurse }', 'foreach ($d in $dirs) { Remove-Item $d -Recurse }'):
with self.subTest(command=command):
self.assertBlocked(command, 'PowerShell')
def test_blocks_commands_after_comments_escapes_and_line_continuations(self):
for command in ("echo hi # don't touch\nrm -rf build", 'echo "\\""; git reset --hard', "echo 'a\\'; rm -rf x",
'rm \\\n -rf build', 'git reset \\\n --hard'):
with self.subTest(command=command):
self.assertBlocked(command)
self.assertBlocked('Remove-Item build `\n -Recurse -Force', 'PowerShell')
def test_allows_comments_and_hashes_that_are_not_commands(self):
for command in ('ls # rm -rf x', 'git commit -m "fix #12; rm -rf is bad"', 'echo a#b', 'echo "\\"rm -rf x\\""'):
with self.subTest(command=command):
self.assertAllowed(command)
def test_blocks_destructive_powershell_variants(self):
for command in ('Remove-Item build -Recurse -Force', 'Remove-Item -Path build -Rec', 'rm -r build', 'ri build -recurse',
'Get-ChildItem x | Remove-Item -Recurse', 'git reset --hard', 'cmd /c "rd /s /q build"',