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

+9 -3
View File
@@ -63,6 +63,9 @@ class UserCustomizationTests(unittest.TestCase):
home = Path(directory)
key = f'MY_TEST_TOKEN_{uuid.uuid4().hex}'
stored = False
stale = home / '.my-ai-configuration/bin/with-credential.ps1'
stale.parent.mkdir(parents=True)
stale.write_text('# outdated helper\n', encoding='utf-8')
try:
result = subprocess.run([
powershell, '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File',
@@ -73,15 +76,18 @@ class UserCustomizationTests(unittest.TestCase):
self.assertEqual(0, result.returncode, result.stderr)
stored = True
helper = home / '.my-ai-configuration/bin/with-credential.ps1'
self.assertTrue(helper.is_file())
self.assertEqual((ROOT / 'scripts/lib/with-credential.ps1').read_bytes(), helper.read_bytes(),
'add-credentials must replace an outdated helper')
self.assertTrue((home / '.my-ai-configuration/bin/with-credential.sh').is_file())
child = f"if ([Environment]::GetEnvironmentVariable('{key}', 'Process')) {{ exit 0 }}; exit 1"
child = (f"if ([Environment]::GetEnvironmentVariable('{key}', 'Process')) "
"{ Write-Output 'child-output'; exit 3 }; exit 1")
injected = subprocess.run([
powershell, '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', str(helper),
'-Client', 'Codex', '-Key', key, '-FilePath', powershell,
'-NoProfile', '-Command', child,
], capture_output=True, text=True, timeout=20)
self.assertEqual(0, injected.returncode, injected.stderr)
self.assertEqual(3, injected.returncode, injected.stderr)
self.assertIn('child-output', injected.stdout)
finally:
cleanup = subprocess.run([
powershell, '-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command',