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

+38 -12
View File
@@ -16,7 +16,10 @@ import re
import shlex
import sys
WRAPPERS = {'sudo', 'command', 'builtin', 'exec', 'nohup', 'time', 'env', 'xargs'}
WRAPPERS = {'sudo', 'doas', 'command', 'builtin', 'exec', 'nohup', 'time', 'env', 'xargs', 'timeout', 'nice'}
# Shell keywords that can precede a command in the same segment.
KEYWORDS = {'if', 'then', 'else', 'elif', 'do', 'while', 'until', '!'}
LINE_CONTINUATION = re.compile(r'[\\`]\r?\n')
POSIX_SHELLS = {'bash', 'sh', 'zsh', 'dash', 'ksh'}
POWERSHELL_HOSTS = {'pwsh', 'powershell'}
POWERSHELL_REMOVE = {'remove-item', 'ri'}
@@ -28,9 +31,15 @@ MAX_DEPTH = 3
def split_segments(command):
"""Split on ;, &, |, &&, || and newlines that are outside quotes."""
segments, current, quote = [], [], None
for character in command:
"""Split on ;, &, |, &&, ||, newlines, and braces outside quotes; drop # comments; join continued lines."""
command = LINE_CONTINUATION.sub(' ', command)
segments, current, quote, index = [], [], None, 0
while index < len(command):
character = command[index]
if character == '\\' and quote != "'" and index + 1 < len(command):
current.append(command[index:index + 2])
index += 2
continue
if quote:
current.append(character)
if character == quote:
@@ -38,11 +47,16 @@ def split_segments(command):
elif character in '\'"':
quote = character
current.append(character)
elif character in ';&|\n':
elif character == '#' and (index == 0 or command[index - 1].isspace()):
newline = command.find('\n', index)
index = len(command) if newline < 0 else newline
continue
elif character in ';&|\n{}':
segments.append(''.join(current))
current = []
else:
current.append(character)
index += 1
segments.append(''.join(current))
return [segment.strip() for segment in segments if segment.strip()]
@@ -60,14 +74,16 @@ def program_name(token):
def strip_wrappers(tokens):
index = 0
while index < len(tokens):
token = tokens[index]
if program_name(token) in WRAPPERS or re.fullmatch(r'[A-Za-z_][A-Za-z0-9_]*=.*', token):
index += 1
"""Drop leading keywords, subshell parentheses, and variable assignments; stop at the first program or wrapper."""
tokens = list(tokens)
while tokens:
first = tokens[0].lstrip('(')
if not first or first in KEYWORDS or re.fullmatch(r'[A-Za-z_][A-Za-z0-9_]*=.*', first):
tokens.pop(0)
continue
tokens[0] = first
break
return tokens[index:]
return tokens
def short_flags(tokens):
@@ -97,6 +113,13 @@ def check_tokens(tokens, shell=None):
if not tokens:
return None
program = program_name(tokens[0])
if program in WRAPPERS:
# Wrapper options and values (xargs -0, sudo -u bob, timeout 30) come before the wrapped command.
for start in range(1, len(tokens)):
reason = check_tokens(tokens[start:], shell)
if reason:
return reason
return None
arguments = tokens[1:]
lowered = [argument.lower() for argument in arguments]
if program == 'rm' and shell != 'powershell':
@@ -127,6 +150,8 @@ def nested_command(tokens):
if not tokens:
return None
program = program_name(tokens[0])
if program in WRAPPERS:
return next((found for start in range(1, len(tokens)) if (found := nested_command(tokens[start:]))), None)
if program == 'eval':
return ' '.join(tokens[1:]), 'bash'
for index, token in enumerate(tokens[1:], start=1):
@@ -214,7 +239,8 @@ def main():
args = parser.parse_args()
if args.hook:
return run_hook(sys.stdin.buffer.read())
command = sys.stdin.read() if args.command == '-' else args.command
# Windows PowerShell 5.1 prefixes piped stdin with a UTF-8 byte order mark.
command = sys.stdin.buffer.read().decode('utf-8-sig', errors='replace') if args.command == '-' else args.command
return run_check(command, args.workspace)