Skip to content

Commit c61f3f3

Browse files
committed
fix: Handle sudo flags in command validation
Skip sudo options like -n when validating the actual command. This allows sudo -n apt-get clean to be properly validated.
1 parent da14bd9 commit c61f3f3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

cortex/utils/commands.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,17 @@ def validate_command(command: str, strict: bool = True) -> Tuple[bool, Optional[
189189
# Strict mode: command must start with allowed prefix
190190
if strict:
191191
first_word = command.split()[0]
192-
# Handle sudo prefix
192+
# Handle sudo prefix and its options
193193
if first_word == 'sudo':
194194
parts = command.split()
195-
if len(parts) > 1:
196-
first_word = parts[1]
195+
# Skip sudo and any flags (starting with -)
196+
actual_command_index = 1
197+
while actual_command_index < len(parts) and parts[actual_command_index].startswith('-'):
198+
actual_command_index += 1
199+
if actual_command_index < len(parts):
200+
first_word = parts[actual_command_index]
201+
else:
202+
return False, "No command found after sudo"
197203

198204
if first_word not in ALLOWED_COMMAND_PREFIXES:
199205
return False, f"Command '{first_word}' is not in the allowlist"

0 commit comments

Comments
 (0)