Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ PYTHONPATH=.
# MOBILE_PORT=5555
# ADB_HOST="127.0.0.1"
# ADB_PORT=5037

# Command Execution Safety Settings
COMMAND_SAFETY_CHECK_ENABLED=true
COMMAND_TIMEOUT=30
COMMAND_MAX_OUTPUT_CHARS=2000
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ LLM_PROVIDER="groq"
LLM_TEMPERATURE=0.8
```

### Command Execution Safety

Commands passed to `run_command` are checked against built-in dangerous-command patterns, including patterns for `rm -rf`, `mkfs`, `dd if=`, `shutdown`, `sudo`, `reboot`, recursive `chmod`, and fork bombs. Chained commands are checked as well.

When a command matches a dangerous pattern and safety checks are enabled, the overlay displays the command and waits for confirmation. Approve with the **Yes** button or say/type `yes` or `confirm`; deny with **No** or say/type `no` or `cancel`. The command runs only after approval.

Execution is limited by a configurable timeout. If it expires, the process is terminated and a timeout message is returned. Output longer than the configured character limit is truncated and suffixed with `[Output truncated...]`.

Set these values in `.env`:

| Environment variable | Default | Description |
| --- | --- | --- |
| `COMMAND_SAFETY_CHECK_ENABLED` | `true` | Enable pattern checks and confirmation prompts for dangerous commands. |
| `COMMAND_TIMEOUT` | `30` | Maximum execution time in seconds. |
| `COMMAND_MAX_OUTPUT_CHARS` | `2000` | Maximum command output characters before truncation. |

The safety setting is named `COMMAND_SAFETY_CHECK_ENABLED` in the current implementation (not `COMMAND_SAFETY_CHECK`). Dangerous patterns are defined in `src/config.py` as `DEFAULT_DANGEROUS_PATTERNS`; they are built-in defaults, not an environment variable named `DANGEROUS_PATTERNS`.

### UI Configuration
```python
# Interface settings in src/config.py
Expand Down
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def process_audio(self, audio):
def process_query(self, query: str):
"""Process the text query."""
overlay.put_message("query", query)
confirmation = overlay.resolve_voice_confirmation(query)
if confirmation is not None:
response = "Execution confirmed. Running the command." if confirmation else "Execution cancelled by user."
overlay.put_message("response", response)
overlay.put_message("status", "Active", "green")
self.speech.speak(response)
return

logger.debug(f"Invoking agent with: {query}")
overlay.put_message("status", "Processing...", "gold")
try:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ indent-style = "space"
line-ending = "auto"
docstring-code-format = true

[tool.pytest.ini_options]
pythonpath = ["."]

[dependency-groups]
dev = [
"ruff>=0.16.9",
Expand Down
16 changes: 16 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Centralized configuration management for Desktop assistant."""

import os
from typing import ClassVar

from dotenv import load_dotenv

Expand All @@ -14,6 +15,21 @@
class Config:
"""Configuration settings for the Desktop assistant."""

# Command Execution Safety
COMMAND_SAFETY_CHECK_ENABLED = os.environ.get("COMMAND_SAFETY_CHECK_ENABLED", "true").lower() == "true"
COMMAND_TIMEOUT = int(os.environ.get("COMMAND_TIMEOUT", "30"))
COMMAND_MAX_OUTPUT_CHARS = int(os.environ.get("COMMAND_MAX_OUTPUT_CHARS", "2000"))
DEFAULT_DANGEROUS_PATTERNS: ClassVar[list[str]] = [
r"\brm\s+-(?:[^\s]*r[^\s]*f|[^\s]*f[^\s]*r)\b",
r"\bmkfs(?:\.[\w-]+)?\b",
r"\bdd\b(?:\s+\S+)*\s+\bif\s*=",
r"\bshutdown\b",
r"\bsudo\b",
r"\breboot\b",
r"\bchmod\s+-R\b",
r":\s*\(\s*\)\s*\{\s*:\s*\|\s*:\s*&?\s*\}\s*;\s*:",
]

# API Keys
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
Expand Down
51 changes: 51 additions & 0 deletions src/core/command_safety.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Validation helpers for potentially dangerous shell commands."""

import re

from src.config import config


def _split_chained_commands(command: str) -> list[str]:
"""Split shell command chains without splitting operators inside quotes."""
segments = []
start = 0
quote = None
escaped = False
index = 0

while index < len(command):
character = command[index]

if escaped:
escaped = False
elif character == "\\" and quote != "'":
escaped = True
elif quote:
if character == quote:
quote = None
elif character in {"'", '"'}:
quote = character
elif command.startswith(("&&", "||"), index):
segments.append(command[start:index])
index += 1
start = index + 1
elif character in {";", "|"}:
segments.append(command[start:index])
start = index + 1

index += 1

segments.append(command[start:])
return segments


def is_dangerous_command(command: str, patterns: list[str] | None = None) -> bool:
"""Return whether a command or any command in its shell chain matches a pattern."""
if patterns is None:
patterns = config.DEFAULT_DANGEROUS_PATTERNS

compiled_patterns = [re.compile(pattern, re.IGNORECASE) for pattern in patterns]
if any(pattern.search(command) for pattern in compiled_patterns):
return True

return any(pattern.search(segment) for segment in _split_chained_commands(command) for pattern in compiled_patterns)
42 changes: 40 additions & 2 deletions src/core/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import contextlib
import functools
import os
import signal
import subprocess
from typing import Literal

Expand All @@ -10,6 +13,7 @@
from PIL import ImageGrab

from src.config import config
from src.core.command_safety import is_dangerous_command
from src.ui.overlay import overlay
from src.utils.logger import get_logger

Expand Down Expand Up @@ -75,7 +79,39 @@ def wrapper(*args, **kwargs):
@tool
def run_command(command: str) -> str:
"""Run a shell command on the local Linux machine."""
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
if (
config.COMMAND_SAFETY_CHECK_ENABLED
and is_dangerous_command(command)
and not overlay.request_command_confirmation(command)
):
return "Execution cancelled by user"

process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
start_new_session=os.name == "posix",
)
try:
output, _ = process.communicate(timeout=config.COMMAND_TIMEOUT)
except subprocess.TimeoutExpired:
if os.name == "posix":
with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGKILL)
else:
process.kill()
process.communicate()
return f"Command timed out after {config.COMMAND_TIMEOUT} seconds."

if process.returncode:
raise subprocess.CalledProcessError(process.returncode, command, output=output)

max_output_chars = max(0, config.COMMAND_MAX_OUTPUT_CHARS)
if len(output) > max_output_chars:
return output[:max_output_chars] + "[Output truncated...]"
return output


@tool
Expand Down Expand Up @@ -196,11 +232,13 @@ def mirror_mobile(
if source == "screen":
subprocess.Popen("scrcpy", shell=True)
return "Starting mobile screen mirroring using scrcpy."

if source == "camera":
if not camera_facing or camera_facing not in ["front", "back"]:
if camera_facing not in ("front", "back"):
camera_facing = "back"
subprocess.Popen(["scrcpy", "--video-source=camera", f"--camera-facing={camera_facing}"])
return f"Starting mobile camera {camera_facing} mirroring using scrcpy."

return "Invalid source. Use 'screen' or 'camera'."


Expand Down
101 changes: 100 additions & 1 deletion src/ui/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"""

import sys
import threading
from queue import Queue

from PyQt5.QtCore import QSize, Qt, QTimer
from PyQt5.QtCore import QSize, Qt, QThread, QTimer, pyqtSignal
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import (
QApplication,
Expand All @@ -15,6 +16,7 @@
QLabel,
QLineEdit,
QMainWindow,
QMessageBox,
QPushButton,
QScrollArea,
QToolButton,
Expand All @@ -32,6 +34,9 @@
class TransparentOverlayQt(QMainWindow):
"""A transparent overlay that displays transcriptions and responses using PyQt."""

command_confirmation_requested = pyqtSignal(str, object, object)
command_confirmation_finished = pyqtSignal(object, bool)

def __init__(self, message_timeout=5, exit_callback=None):
"""Initialize the overlay.

Expand All @@ -46,6 +51,13 @@ def __init__(self, message_timeout=5, exit_callback=None):
self.message_queue = Queue()
self.running = True
self.on_new_message = None
self._confirmation_request_lock = threading.Lock()
self._confirmation_state_lock = threading.Lock()
self._pending_confirmation = None
self._confirmation_dialog = None
self._confirmation_dialog_request = None
self.command_confirmation_requested.connect(self._show_command_confirmation, Qt.QueuedConnection)
self.command_confirmation_finished.connect(self._close_command_confirmation, Qt.QueuedConnection)

# Set up the UI
self.setup_ui()
Expand Down Expand Up @@ -193,6 +205,93 @@ def put_message(self, msg_type, *args):
self.message_queue.put((msg_type, *args))
QTimer.singleShot(0, self.process_messages) # Schedule async-safe execution

def request_command_confirmation(self, command: str) -> bool:
"""Ask the user to approve a command, dispatching the dialog on the UI thread."""
if QThread.currentThread() == self.thread():
return self._confirm_command(command)

with self._confirmation_request_lock:
request = {"completed": threading.Event(), "confirmed": False}
with self._confirmation_state_lock:
self._pending_confirmation = request
self.command_confirmation_requested.emit(command, request, None)
request["completed"].wait()
with self._confirmation_state_lock:
if self._pending_confirmation is request:
self._pending_confirmation = None
return request["confirmed"]

def _show_command_confirmation(self, command: str, request: dict, _unused: object) -> None:
try:
self.show()
self.raise_()
self.activateWindow()
dialog = QMessageBox(self)
dialog.setWindowTitle("Confirm Command Execution")
dialog.setText(
f"This command may be dangerous. Execute it?\n\n{command}\n\nClick Yes or say “yes” or “confirm”."
)
dialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
dialog.setDefaultButton(QMessageBox.No)
dialog.setEscapeButton(QMessageBox.No)
dialog.setWindowModality(Qt.NonModal)
dialog.buttonClicked.connect(
lambda button: self._resolve_command_confirmation(request, button == dialog.button(QMessageBox.Yes))
)
dialog.finished.connect(lambda _result: self._resolve_command_confirmation(request, False))
self._confirmation_dialog = dialog
self._confirmation_dialog_request = request
dialog.open()
except Exception as error:
logger.error(f"Error requesting command confirmation: {error}")
self._resolve_command_confirmation(request, False)

def _confirm_command(self, command: str) -> bool:
answer = QMessageBox.question(
self,
"Confirm Command Execution",
f"This command may be dangerous. Execute it?\n\n{command}",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No,
)
return answer == QMessageBox.Yes

def _resolve_command_confirmation(self, request: dict, confirmed: bool) -> None:
with self._confirmation_state_lock:
if self._pending_confirmation is not request or request["completed"].is_set():
return
request["confirmed"] = confirmed
request["completed"].set()
self.command_confirmation_finished.emit(request, confirmed)

def _close_command_confirmation(self, request: dict, confirmed: bool) -> None:
if self._confirmation_dialog_request is not request:
return
dialog = self._confirmation_dialog
self._confirmation_dialog = None
self._confirmation_dialog_request = None
if dialog is not None:
dialog.done(QMessageBox.Yes if confirmed else QMessageBox.No)

def resolve_voice_confirmation(self, response: str) -> bool | None:
"""Resolve a pending command approval from a concise voice or text response."""
normalized = response.strip().lower().rstrip(" .,!?")
if normalized in {"yes", "confirm"}:
decision = True
elif normalized in {"no", "cancel"}:
decision = False
else:
return None

with self._confirmation_state_lock:
request = self._pending_confirmation
if request is None or request["completed"].is_set():
return None
request["confirmed"] = decision
request["completed"].set()
self.command_confirmation_finished.emit(request, decision)
return decision

def process_messages(self):
"""Process messages from the queue."""
while not self.message_queue.empty():
Expand Down
44 changes: 44 additions & 0 deletions tests/test_command_safety.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

from src.core.command_safety import is_dangerous_command


@pytest.mark.parametrize(
"command",
[
"ls -la",
"echo hello",
"git status",
"python app.py",
],
)
def test_safe_commands_are_not_flagged(command: str) -> None:
assert not is_dangerous_command(command)


@pytest.mark.parametrize(
"command",
[
"rm -rf /",
"sudo apt update",
"mkfs.ext4 /dev/sda",
"dd if=/dev/zero of=/dev/sda",
"chmod -R 777 /",
],
)
def test_dangerous_commands_are_flagged(command: str) -> None:
assert is_dangerous_command(command)


@pytest.mark.parametrize(
"command",
[
"rm -rf /",
"echo hello && sudo apt update",
"git status; rm -rf /tmp/data",
"echo hello && mkfs.ext4 /dev/sda",
"echo hello; chmod -R 777 /tmp",
],
)
def test_dangerous_syntax_is_detected_with_spacing_and_chains(command: str) -> None:
assert is_dangerous_command(command)
Loading