src — sandbox

Module: src-sandbox Cohesion: 0.80 Members: 0

src — sandbox

The src/sandbox module provides a robust and flexible framework for executing commands and code in isolated, secure environments. It is designed to protect the host system from potentially malicious or resource-intensive operations, offering multiple backend implementations and a granular policy engine for command authorization.

Purpose and Overview

The primary goal of the sandbox module is to enhance the security and stability of the application by:

This module acts as a critical security layer, ensuring that agent-generated or user-provided commands do not compromise the system.

Core Concepts

Sandbox Backend Interface (SandboxBackendInterface)

All concrete sandbox implementations adhere to the SandboxBackendInterface defined in src/sandbox/sandbox-backend.ts. This interface standardizes how different sandboxing technologies are interacted with, allowing the system to swap backends seamlessly.

Key methods include:

Sandbox Registry (SandboxRegistry)

The SandboxRegistry (in src/sandbox/sandbox-registry.ts) implements a Strategy pattern to select the most appropriate sandbox backend at runtime. Backends are registered with a priority, and the registry automatically picks the highest-priority available backend. This ensures the system always attempts to use the strongest available isolation.

Policy Actions (PolicyAction)

The ExecPolicy framework uses PolicyAction to define the outcome of a command evaluation:

Architecture and Execution Flow

The overall flow for executing a command involves several layers:

graph TD
    A[Command Execution Request] --> B{ExecPolicy.evaluate};
    B -- Action: 'sandbox' --> C{AutoSandboxRouter.route};
    B -- Action: 'allow'/'ask'/'deny' --> D{SandboxRegistry.sandboxExecute};
    C -- Mode: 'sandbox' --> D;
    C -- Mode: 'direct' --> E[Direct OS Execution];

    D -- Selects best backend --> F(SandboxBackendInterface);
    F <|-- G[DockerSandbox];
    F <|-- H[OSSandbox];
    F <|-- I[E2BSandbox];
    F <|-- J[OpenShellBackend];

    subgraph Security & Routing
        B
        C
    end

    subgraph Sandbox Backends
        G
        H
        I
        J
    end

    subgraph Core Abstractions
        D
        F
    end

  1. Policy Evaluation: Any command execution request first passes through ExecPolicy.evaluate(). This checks the command against a set of predefined and custom rules.
  2. Auto-Sandboxing: If ExecPolicy determines the command should be sandboxed (or if it's a dangerous command), AutoSandboxRouter.route() is consulted. This router makes a decision based on command patterns and Docker availability.
  3. Backend Selection: The SandboxRegistry.sandboxExecute() function is the entry point for actual sandboxed execution. It queries registered backends (e.g., DockerSandbox, OSSandbox, E2BSandbox, OpenShellBackend) in order of priority to find the first available one.
  4. Command Execution: The selected backend's execute() method is called, running the command in its specific isolated environment. If no sandbox backend is available or required, the command might fall back to direct OS execution (with appropriate warnings).

Key Components

src/sandbox/execpolicy.ts

The ExecPolicy class provides a granular command authorization system.

src/sandbox/auto-sandbox.ts

The AutoSandboxRouter automatically decides whether a command should be routed to a Docker sandbox.

src/sandbox/docker-sandbox.ts

The DockerSandbox class executes commands within isolated Docker containers.

src/sandbox/e2b-sandbox.ts

The E2BSandbox class provides cloud-based sandboxed execution using E2B Firecracker microVMs.

src/sandbox/os-sandbox.ts

The OSSandbox class provides native OS-level sandboxing.

src/sandbox/openshell-backend.ts

The OpenShellBackend provides compatibility with NVIDIA OpenShell, supporting two modes:

src/sandbox/safe-eval.ts

The safe-eval module offers a secure way to evaluate JavaScript expressions.

Integration Points and Usage

Contribution Guidelines

Adding a New Sandbox Backend

  1. Create a new file src/sandbox/your-backend.ts.
  2. Implement the SandboxBackendInterface.
  3. Register your backend in src/sandbox/sandbox-registry.ts (or at application startup) with an appropriate priority.
  4. Consider how AutoSandboxRouter might interact with your new backend if it offers specific advantages for "dangerous" commands.

Extending Execution Policies

  1. Custom Rules: Developers can add new PolicyRule or PrefixRule instances via ExecPolicy.addRule() or ExecPolicy.addPrefixRule(). These can be persisted to a custom rules file.
  2. Dangerous Patterns: The DANGEROUS_PATTERNS list in execpolicy.ts can be extended to detect new types of malicious commands.
  3. Sandbox Modes: The createSandboxConfigForMode() function in os-sandbox.ts can be extended to define new tiers of filesystem access if more nuanced control is required.