Skip to main content

Sandbox Tool

The sandbox provides an isolated execution environment for running untrusted code and commands securely.

Overview

Sandboxing protects your system by:
  • Isolating execution: Commands run in separate container
  • Limiting access: Restricted file system and network access
  • Resource control: CPU and memory limits
  • Clean environment: Fresh state for each session

Supported Providers

Docker

Status: Recommended for most users Requirements:
  • Docker Desktop or Docker Engine
  • Sufficient disk space for container image
Setup:

Podman

Status: Alternative to Docker Requirements:
  • Podman installed and configured
  • Rootless mode supported
Setup:

macOS Seatbelt

Status: macOS native sandboxing Requirements:
  • macOS operating system
  • System Integrity Protection enabled
Setup:

No Sandbox

Status: Not recommended (development only) Setup:
⚠️ Warning: Only use when necessary. Commands execute with full system access.

Configuration

Environment Variable

The primary configuration method:

Settings File

Configure in settings.json:

Build Configuration

Default Image:
Custom Image: See “Customizing the Sandbox” section below.

How It Works

Execution Flow

Container Lifecycle

  1. Session Start:
    • Pull/build sandbox image
    • Start container
    • Mount project directory
    • Set up environment
  2. Command Execution:
    • Send command to container
    • Execute in isolated environment
    • Stream output back to CLI
    • Handle errors and timeouts
  3. Session End:
    • Stop container
    • Clean up resources
    • Preserve project changes

Customizing the Sandbox

Custom Dockerfile

Note: This requires working from the source code repository, not an npm-installed package.

Requirements

  1. Clone the Qwen Code repository:
  2. Install dependencies:
  3. Build the project:
  4. Link the CLI globally:

Create Custom Dockerfile

Create .qwen/sandbox.Dockerfile in your project:

Build Custom Image

From your project root:
This builds a project-specific image based on your Dockerfile.

Verify Custom Image

Restore Official CLI

After testing custom sandbox:

Sandbox Limitations

File System

Accessible:
  • Project directory (mounted read-write)
  • Temp directory within container
  • Standard Unix paths
Not Accessible:
  • Home directory outside project
  • System directories
  • Other projects

Network

Default:
  • Outbound connections allowed
  • No inbound connections
  • DNS resolution available
Can be restricted further with custom configuration.

Performance

Overhead:
  • Container startup: ~1-2 seconds
  • Command execution: Minimal (less than 100ms)
  • File I/O: Slightly slower than native
Optimization:
  • Keep container running between commands
  • Use .dockerignore to exclude large files
  • Cache package installations in image

Security Benefits

Isolation

Protected:
  • System files
  • Other projects
  • User data
  • Network resources
Contained:
  • Malicious scripts
  • Unexpected side effects
  • Resource consumption
  • File system modifications

Use Cases

When to use sandbox:
  1. Untrusted code:
  2. Experimental commands:
  3. Package installation:
  4. Build processes:
When sandbox isn’t needed:
  1. Trusted, well-known commands (git status, ls)
  2. Simple file operations
  3. Built-in tools (read_file, write_file)
  4. Development on personal projects

Troubleshooting

Image Build Fails

Error: Failed to build sandbox image Solutions:
  1. Check Docker/Podman is running
  2. Verify internet connectivity
  3. Check disk space: df -h
  4. Clear Docker cache: docker system prune
  5. Retry build: npm run build:sandbox

Container Won’t Start

Error: Container failed to start Solutions:
  1. Check Docker daemon: docker ps
  2. Review logs: docker logs <container-id>
  3. Restart Docker Desktop
  4. Check resource limits

Permission Errors

Error: Permission denied in sandbox Solutions:
  1. Check file permissions in project
  2. Verify mount points
  3. Use rootless container if possible
  4. Check SELinux/AppArmor policies

Performance Issues

Symptoms: Slow command execution Solutions:
  1. Check Docker resource allocation
  2. Reduce mounted directory size
  3. Use .dockerignore:
  4. Increase Docker Desktop resources

Network Issues

Error: Network request failed in sandbox Solutions:
  1. Check Docker network mode
  2. Verify DNS resolution: docker run alpine nslookup google.com
  3. Check firewall rules
  4. Test with --network host (temporary)

Best Practices

1. Keep Images Updated

2. Optimize Image Size

3. Cache Dependencies

4. Use .dockerignore

5. Monitor Resources

Configuration Reference

Full Settings

Environment Variables

Implementation

Key Files:
  • scripts/build_sandbox.js - Build script
  • Dockerfile - Sandbox image definition
  • packages/core/src/services/shellExecutionService.ts - Execution routing

Shell Execution Service

Next Steps