diff --git a/.env.example b/.env.example index 76b68c7..e905252 100644 --- a/.env.example +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/DesktopAI b/DesktopAI new file mode 160000 index 0000000..30a09ce --- /dev/null +++ b/DesktopAI @@ -0,0 +1 @@ +Subproject commit 30a09ce57f20ccbe8fb8f3223ddc4b7d8a623d72 diff --git a/README.md b/README.md index 1d425e1..506b400 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.py b/main.py index ddf22f9..6dfa6f0 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ This is the main entry point for the DesktopAI assistant with a clean modular architecture. """ +import argparse import os import sys @@ -12,9 +13,7 @@ import threading -from src.audio.audio_processor import AudioProcessor -from src.audio.listener import Listener -from src.audio.ttsplayer import TTSPlayer +from src import config from src.core.assistant import call_agent from src.core.tools import register_stop_assistant from src.ui.overlay import app, overlay @@ -27,72 +26,124 @@ class DesktopAssistant: """Main DesktopAI Assistant class with clean modular architecture.""" - def __init__(self): + def __init__(self, text_only: bool = False, no_tts: bool = False): """Initialize the assistant with all required components.""" + self.text_only = text_only + self.no_tts = no_tts or text_only + config.TEXT_ONLY_MODE = text_only + config.NO_TTS = self.no_tts self.lock = threading.Lock() self._setup_components() self._setup_ui() def _setup_components(self): """Initialize core components.""" - # Text-to-Speech - self.speech = TTSPlayer() - self.speech.start() + self.speech = None + if not self.no_tts: + from src.audio.ttsplayer import TTSPlayer - # Audio processing - self.audio_processor = AudioProcessor() - self.listener = Listener(tts_player=self.speech, overlay=overlay) + self.speech = TTSPlayer() + self.speech.start() + + self.audio_processor = None + self.listener = None + if not self.text_only: + from src.audio.audio_processor import AudioProcessor + from src.audio.listener import Listener + + self.audio_processor = AudioProcessor() + self.listener = Listener(tts_player=self.speech, overlay=overlay) def _setup_ui(self): """Setup user interface.""" - overlay.on_new_message = self.process_query - overlay.start() + self.overlay = overlay + self.overlay.on_new_message = self.process_query + self.overlay.start() + + def _speak(self, text): + if self.speech is not None: + self.speech.speak(text) def process_audio(self, audio): """Process incoming audio data.""" + if self.audio_processor is None: + logger.error("Audio processing is unavailable in text-only mode.") + return + with self.lock: logger.info("Processing audio...") - overlay.put_message("status", "Analyzing voice...", "gold") + self.overlay.put_message("status", "Analyzing voice...", "gold") transcription = self.audio_processor.process_audio(audio) self.process_query(transcription) def process_query(self, query: str): """Process the text query.""" - overlay.put_message("query", query) + self.overlay.put_message("query", query) + confirmation = self.overlay.resolve_voice_confirmation(query) + if confirmation is not None: + response = "Execution confirmed. Running the command." if confirmation else "Execution cancelled by user." + self.overlay.put_message("response", response) + self.overlay.put_message("status", "Active", "green") + self._speak(response) + return + logger.debug(f"Invoking agent with: {query}") - overlay.put_message("status", "Processing...", "gold") + self.overlay.put_message("status", "Processing...", "gold") try: response = call_agent(query) logger.info(f"Agent response: {response}") - overlay.put_message("response", response) - overlay.put_message("status", "Active", "green") - self.speech.speak(response) + self.overlay.put_message("response", response) + self.overlay.put_message("status", "Active", "green") + self._speak(response) except Exception as e: logger.error(f"Error processing query: {e}") - overlay.put_message("status", "Error occurred", "red") - self.speech.speak("Sorry, an error occurred while processing your request.") + self.overlay.put_message("status", "Error occurred", "red") + self._speak("Sorry, an error occurred while processing your request.") def start(self): """Start the assistant.""" - logger.info("🔊 Listening in the background... Say something!") - overlay.put_message("status", "Active", "green") - executor.submit(self.listener.listen, self.process_audio) + if self.listener is not None: + logger.info("🔊 Listening in the background... Say something!") + executor.submit(self.listener.listen, self.process_audio) + else: + logger.info("Text-only mode active. Enter a message in the overlay.") + self.overlay.put_message("status", "Active", "green") def shutdown(self): """Clean shutdown of the assistant.""" - self.speech.speak("Shutting down!") - overlay.put_message("status", "Shutting down...", "red") + self._speak("Shutting down!") + self.overlay.put_message("status", "Shutting down...", "red") logger.info("Shutting down...") - self.listener.stop_listening() - self.speech.shutdown() + if self.listener is not None: + self.listener.stop_listening() + if self.speech is not None: + self.speech.shutdown() executor.shutdown(wait=False, cancel_futures=True) - overlay.close() + self.overlay.close() def main(): """Main function to run Desktop Assistant.""" - assistant = DesktopAssistant() + parser = argparse.ArgumentParser() + parser.add_argument( + "--text-only", + action="store_true", + default=False, + help="Run in text-only mode without microphone or audio dependencies", + ) + parser.add_argument( + "--no-tts", + action="store_true", + default=False, + help="Disable Text-To-Speech output", + ) + args = parser.parse_args() + + config.TEXT_ONLY_MODE = args.text_only + config.NO_TTS = args.no_tts + + assistant = DesktopAssistant(text_only=args.text_only, no_tts=args.no_tts) register_stop_assistant(assistant.shutdown) assistant.start() app.exec() diff --git a/pyproject.toml b/pyproject.toml index f3b82dd..7b117f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/src/config.py b/src/config.py index b81e5d9..527f632 100644 --- a/src/config.py +++ b/src/config.py @@ -2,6 +2,7 @@ """Centralized configuration management for Desktop assistant.""" import os +from typing import ClassVar from dotenv import load_dotenv @@ -10,10 +11,28 @@ # Base directory for resolving relative paths BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +TEXT_ONLY_MODE = False +NO_TTS = False + 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") diff --git a/src/core/command_safety.py b/src/core/command_safety.py new file mode 100644 index 0000000..450689e --- /dev/null +++ b/src/core/command_safety.py @@ -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) diff --git a/src/core/tools.py b/src/core/tools.py index 6bf917d..6c45be3 100644 --- a/src/core/tools.py +++ b/src/core/tools.py @@ -1,4 +1,7 @@ +import contextlib import functools +import os +import signal import subprocess from typing import Literal @@ -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 @@ -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 @@ -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'." diff --git a/src/ui/overlay.py b/src/ui/overlay.py index 9689124..d0a7761 100644 --- a/src/ui/overlay.py +++ b/src/ui/overlay.py @@ -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, @@ -15,6 +16,7 @@ QLabel, QLineEdit, QMainWindow, + QMessageBox, QPushButton, QScrollArea, QToolButton, @@ -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. @@ -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() @@ -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(): @@ -217,6 +316,8 @@ def process_messages(self): def start(self): """Start the overlay.""" + self.update_status("Jasper Active", "lightgreen") + self.show() self.update_status(f"{config.ASSISTANT_NAME} Active", "lightgreen") def shutdown(self): diff --git a/tests/test_command_safety.py b/tests/test_command_safety.py new file mode 100644 index 0000000..d198202 --- /dev/null +++ b/tests/test_command_safety.py @@ -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)