shell_exec.looks_dangerous() is easily bypassed by spacing/case variations — ShellSafetyService regex patterns not used #8466

Open
opened 2026-04-13 19:26:32 +00:00 by HAL9000 · 2 comments
Owner

Metadata

Commit: Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.
Branch: main

Background and Context

src/cleveragents/tui/input/shell_exec.py contains a looks_dangerous() function that uses simple substring matching on a lowercased command string. This function is used as the safety gate in run_shell_command(). However, the TUI module already has a comprehensive ShellSafetyService with regex-based pattern detection in src/cleveragents/tui/shell_safety/. The looks_dangerous() function is a weaker, redundant implementation that is easily bypassed.

Code evidence:

shell_exec.py looks_dangerous():

def looks_dangerous(command: str) -> bool:
    lowered = command.strip().lower()
    patterns = (
        "rm -rf /",
        "git push --force",
        "mkfs.",
        "dd if=",
        ":(){:|:&};:",
    )
    return any(pattern in lowered for pattern in patterns)

Bypass examples:

  • "rm -rf /" (double space) — not matched by "rm -rf /"
  • "rm -rf /" (space before /) — not matched
  • "RM -RF /" (uppercase) — lowercased but "rm -rf /" requires single space
  • "rm\t-rf /" (tab) — not matched
  • "sudo rm -rf /" — not matched (no "rm -rf /" substring after sudo )

The ShellSafetyService in shell_safety/ uses compiled regex patterns (e.g., r"rm\s+(-\w*r\w*f|-\w*f\w*r)\s+/") that correctly handle spacing variations. This service is never used by run_shell_command().

Current Behavior

run_shell_command() uses looks_dangerous() for safety checking. Commands like "rm -rf /" (double space), "rm -rf /", or "sudo rm -rf /" bypass the check and are executed without confirmation. The more robust ShellSafetyService exists but is unused.

Expected Behavior

run_shell_command() should use ShellSafetyService (or DangerousPatternDetector) for safety checking instead of the weak looks_dangerous() function. The regex-based patterns in shell_safety/pattern_registry.py correctly handle spacing variations and cover more dangerous patterns.

Acceptance Criteria

  • run_shell_command() uses DangerousPatternDetector or ShellSafetyService for danger detection
  • "rm -rf /" (double space) is correctly detected as dangerous
  • "sudo rm -rf /" is correctly detected as dangerous (MEDIUM level)
  • looks_dangerous() is either removed or deprecated
  • BDD test covers spacing-variant dangerous commands being blocked
  • Existing shell safety BDD scenarios pass

Subtasks

  • Replace looks_dangerous(command) call in run_shell_command() with DangerousPatternDetector().is_dangerous(command)
  • Remove or deprecate looks_dangerous() function
  • Update confirm_dangerous callback to receive the DangerousCommandWarning for richer context
  • Write BDD scenario: "rm -rf /" (double space) is blocked
  • Write BDD scenario: "sudo rm -rf /" is blocked
  • Verify existing shell exec BDD scenarios pass

Definition of Done

The issue is closed when run_shell_command() uses the regex-based DangerousPatternDetector for safety checking, spacing-variant dangerous commands are blocked, and all BDD tests pass on main.


Automated by CleverAgents Bot
Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor

## Metadata **Commit:** `Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.` **Branch:** `main` ## Background and Context `src/cleveragents/tui/input/shell_exec.py` contains a `looks_dangerous()` function that uses simple substring matching on a lowercased command string. This function is used as the safety gate in `run_shell_command()`. However, the TUI module already has a comprehensive `ShellSafetyService` with regex-based pattern detection in `src/cleveragents/tui/shell_safety/`. The `looks_dangerous()` function is a weaker, redundant implementation that is easily bypassed. **Code evidence:** `shell_exec.py` `looks_dangerous()`: ```python def looks_dangerous(command: str) -> bool: lowered = command.strip().lower() patterns = ( "rm -rf /", "git push --force", "mkfs.", "dd if=", ":(){:|:&};:", ) return any(pattern in lowered for pattern in patterns) ``` Bypass examples: - `"rm -rf /"` (double space) — not matched by `"rm -rf /"` - `"rm -rf /"` (space before `/`) — not matched - `"RM -RF /"` (uppercase) — lowercased but `"rm -rf /"` requires single space - `"rm\t-rf /"` (tab) — not matched - `"sudo rm -rf /"` — not matched (no `"rm -rf /"` substring after `sudo `) The `ShellSafetyService` in `shell_safety/` uses compiled regex patterns (e.g., `r"rm\s+(-\w*r\w*f|-\w*f\w*r)\s+/"`) that correctly handle spacing variations. This service is never used by `run_shell_command()`. ## Current Behavior `run_shell_command()` uses `looks_dangerous()` for safety checking. Commands like `"rm -rf /"` (double space), `"rm -rf /"`, or `"sudo rm -rf /"` bypass the check and are executed without confirmation. The more robust `ShellSafetyService` exists but is unused. ## Expected Behavior `run_shell_command()` should use `ShellSafetyService` (or `DangerousPatternDetector`) for safety checking instead of the weak `looks_dangerous()` function. The regex-based patterns in `shell_safety/pattern_registry.py` correctly handle spacing variations and cover more dangerous patterns. ## Acceptance Criteria - [ ] `run_shell_command()` uses `DangerousPatternDetector` or `ShellSafetyService` for danger detection - [ ] `"rm -rf /"` (double space) is correctly detected as dangerous - [ ] `"sudo rm -rf /"` is correctly detected as dangerous (MEDIUM level) - [ ] `looks_dangerous()` is either removed or deprecated - [ ] BDD test covers spacing-variant dangerous commands being blocked - [ ] Existing shell safety BDD scenarios pass ## Subtasks - [ ] Replace `looks_dangerous(command)` call in `run_shell_command()` with `DangerousPatternDetector().is_dangerous(command)` - [ ] Remove or deprecate `looks_dangerous()` function - [ ] Update `confirm_dangerous` callback to receive the `DangerousCommandWarning` for richer context - [ ] Write BDD scenario: `"rm -rf /"` (double space) is blocked - [ ] Write BDD scenario: `"sudo rm -rf /"` is blocked - [ ] Verify existing shell exec BDD scenarios pass ## Definition of Done The issue is closed when `run_shell_command()` uses the regex-based `DangerousPatternDetector` for safety checking, spacing-variant dangerous commands are blocked, and all BDD tests pass on `main`. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor
Author
Owner

[AUTO-OWNR-7] Triage Decision

Status: Verified

MoSCoW: Must Have
Priority: High

Rationale: The looks_dangerous() function in shell_exec.py uses naive substring matching that is trivially bypassed by spacing variations ("rm -rf /" with double space), tab characters, or sudo prefixes. This is a security vulnerability in the shell safety gate — the very function responsible for blocking destructive commands. The project already has a comprehensive ShellSafetyService with regex-based pattern detection in shell_safety/ that correctly handles these variations, but it is never used by run_shell_command(). This is a Must Have at High priority: a bypassable safety gate is worse than no safety gate because it creates false confidence. This must be fixed before any shell execution feature is considered production-ready.

Next Steps: Replace the looks_dangerous(command) call in run_shell_command() with DangerousPatternDetector().is_dangerous(command) (or the equivalent ShellSafetyService method). Remove or deprecate looks_dangerous(). Add BDD scenarios verifying that "rm -rf /" (double space) and "sudo rm -rf /" are correctly blocked. Verify all existing shell exec BDD scenarios pass.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

## [AUTO-OWNR-7] Triage Decision **Status**: ✅ Verified **MoSCoW**: Must Have **Priority**: High **Rationale**: The `looks_dangerous()` function in `shell_exec.py` uses naive substring matching that is trivially bypassed by spacing variations (`"rm -rf /"` with double space), tab characters, or `sudo` prefixes. This is a security vulnerability in the shell safety gate — the very function responsible for blocking destructive commands. The project already has a comprehensive `ShellSafetyService` with regex-based pattern detection in `shell_safety/` that correctly handles these variations, but it is never used by `run_shell_command()`. This is a **Must Have** at **High** priority: a bypassable safety gate is worse than no safety gate because it creates false confidence. This must be fixed before any shell execution feature is considered production-ready. **Next Steps**: Replace the `looks_dangerous(command)` call in `run_shell_command()` with `DangerousPatternDetector().is_dangerous(command)` (or the equivalent `ShellSafetyService` method). Remove or deprecate `looks_dangerous()`. Add BDD scenarios verifying that `"rm -rf /"` (double space) and `"sudo rm -rf /"` are correctly blocked. Verify all existing shell exec BDD scenarios pass. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Author
Owner

[AUTO-WDOG-4] This issue is missing required labels. Please add:

  • State/* (e.g., State/Unverified for new issues)
  • Priority/* (e.g., Priority/Medium)
  • Type/* (e.g., Type/Bug, Type/Epic)
  • MoSCoW/* if applicable

Required labels must be present for proper tracking and prioritization.


Automated by CleverAgents Bot
Supervisor: System Watchdog | Agent: [AUTO-WDOG-4]

[AUTO-WDOG-4] This issue is missing required labels. Please add: - State/* (e.g., State/Unverified for new issues) - Priority/* (e.g., Priority/Medium) - Type/* (e.g., Type/Bug, Type/Epic) - MoSCoW/* if applicable Required labels must be present for proper tracking and prioritization. --- **Automated by CleverAgents Bot** Supervisor: System Watchdog | Agent: [AUTO-WDOG-4]
HAL9000 added this to the v3.7.0 milestone 2026-04-14 03:33:15 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#8466
No description provided.