Skip to main content

Sandbox Execution

Qwen Code supports sandboxed execution environments to isolate command execution and enhance security. This is particularly useful when running untrusted code or working in sensitive environments.

Supported Sandbox Runtimes

Qwen Code supports three sandbox backends:

Docker

Container-based isolation using Docker Engine.

Podman

Rootless container runtime, ideal for environments without Docker daemon privileges.

macOS Seatbelt (sandbox-exec)

Native macOS sandboxing without containers. Only available on macOS.

Configuration

Environment Variables

Enable Sandbox

Custom Sandbox Image

Additional Configuration

Command Line Options

Settings File Configuration

In .qwen/settings.json:

Docker/Podman Sandbox Details

How It Works

When using Docker or Podman:
  1. Container Creation: Qwen Code spawns a container with:
    • Current working directory mounted
    • User settings directory (~/.qwen) mounted
    • Temporary directory mounted
    • Network access configured
  2. Environment Inheritance: The following are passed through:
    • API keys (GEMINI_API_KEY, OPENAI_API_KEY, etc.)
    • Terminal settings (TERM, COLORTERM)
    • Virtual environment paths (VIRTUAL_ENV)
    • Google Cloud credentials
  3. Lifecycle: Container is automatically removed on exit (--rm flag)

Volume Mounts

By default, these are mounted:

UID/GID Handling

On Linux, Qwen Code automatically matches the host user’s UID/GID to prevent permission issues:
This creates a user inside the container with matching UID/GID:

macOS Seatbelt Sandbox

Built-in Profiles

Qwen Code includes six built-in seatbelt profiles:

Profile Locations

  • Built-in: packages/cli/src/utils/sandbox-macos-{profile}.sb
  • Custom: .qwen/sandbox-macos-{profile}.sb (project-specific)

Custom Profile Example

Create .qwen/sandbox-macos-custom.sb:
Then use it:

Seatbelt Variables

Available in .sb files:
  • TARGET_DIR: Current working directory
  • TMP_DIR: System temporary directory
  • HOME_DIR: User home directory
  • CACHE_DIR: macOS cache directory
  • INCLUDE_DIR_0 through INCLUDE_DIR_4: Additional workspace directories

Proxy Support

For sandboxed environments with restricted network access:

Container-Based Proxy

The proxy container:
  • Runs on port 8877 by default
  • Connected to both host network and internal sandbox network
  • Automatically started/stopped with sandbox

macOS Seatbelt Proxy

Custom Sandbox Images

Building Custom Image

Create .qwen/sandbox.Dockerfile:

Build and Use

Startup Scripts

Container Bashrc

Create .qwen/sandbox.bashrc to customize the container environment:
This file is automatically sourced on container startup.

Troubleshooting

Permission Issues (Linux)

Problem: Files created in sandbox have wrong ownership. Solution: Enable UID/GID matching:

Image Not Found

Problem: Sandbox image 'qwen-code-sandbox' is missing Solution: Build the sandbox image:

Network Access Issues

Problem: Container cannot access network. Solution: Check Docker/Podman network configuration:

Virtual Environment Issues

Problem: Python venv not working in sandbox. Solution: The sandbox creates a separate venv directory:

Port Binding Issues

Problem: Cannot access exposed ports. Solution: Ensure ports are properly exposed:

macOS Seatbelt Issues

Problem: Seatbelt denies file access. Solution: Use a more permissive profile or create custom profile:

Security Considerations

Container Isolation

  • Containers run with --init flag for proper signal handling
  • Containers auto-removed on exit (--rm)
  • Host Docker socket NOT mounted by default
  • Network can be restricted using internal networks + proxy

Seatbelt Isolation

  • File system access restricted to defined paths
  • Network access can be blocked in restrictive profiles
  • Process execution can be limited

Best Practices

  1. Use restrictive profiles in production
  2. Mount only necessary paths with appropriate permissions (ro/rw)
  3. Review custom Dockerfiles before building
  4. Use proxy mode for network filtering
  5. Keep sandbox images updated with security patches

Integration Tests

For testing with sandbox enabled:
Integration test mode:
  • Uses unique container names with random suffixes
  • Supports checkpoint/resume functionality
  • Handles temporary file cleanup

Advanced Topics

Multiple Workspaces

Mount additional workspace directories:

Custom Entrypoint

Override container entrypoint:

Resource Limits

Debug Mode

Enable debugging:

Source Code References

  • Sandbox implementation: packages/cli/src/utils/sandbox.ts
  • Sandbox config: packages/cli/src/config/sandboxConfig.ts
  • Seatbelt profiles: packages/cli/src/utils/sandbox-macos-*.sb