Development · Tip

Sandbox Hardening Checklist for Code-Executing Agents

A three-step checklist to isolate and harden runtimes for arbitrary code execution.

This architectural tip covers three practical steps to protect agent runtimes before executing untrusted code. Adapted from Browser Use's sandbox engineering patterns, it focuses on stripping source files, dropping system privileges, and cleaning runtime environments to prevent token leakage and privilege escalation.

How it works

1. Bytecode-only execution: compile Python to .pyc in Docker build, delete every .py; framework loads into memory as root, source gone. 2. Privilege drop: start as root only to read root-owned bytecode, then setuid/setgid to a 'sandbox' user. 3. Environment stripping: read SESSION_TOKEN, CONTROL_PLANE_URL, SESSION_ID into Python variables, then delete from os.environ; VM in private VPC with only control-plane access.

What it's for

  • Harden containerized environments before running untrusted LLM-generated code
  • Prevent environment variable scraping in multi-tenant agent execution runtimes
  • Reduce attack surface by dropping root permissions and removing source code files

Key points

  • Bytecode-only execution by compiling Python files to .pyc during build and removing .py sources
  • In-memory loading as root before executing untrusted code
  • Privilege dropping via setuid and setgid to an unprivileged sandbox user
  • Environment variable stripping to remove sensitive tokens and URLs from os.environ after ingestion
  • Network confinement to private VPCs restricted to control plane communication

Pricing

Free

Details

Type
Tip
Category
Development
Topic
Application Security
Platforms
Linux, Self-hosted
For
Security engineers and AI developers building code-execution sandboxes
Tags
sandboxing, security, ai agents, docker, python

Why it was saved

  • @larsencc, 27 February 2026: Reusable security checklist for anyone running agent code in containers/VMs.

Related

  • Browser Use Agent Sandbox Architecture

    Architecture pattern for securely running code-executing agents inside isolated micro-VMs

  • awesome-llm-apps

    Open-source collection of runnable AI agent, skill, and RAG templates

  • smolagents

    Lightweight open-source Python library for building code-writing and vision AI agents

  • agent-browser Environments

    Run browser automation agents inside on-demand isolated sandbox environments

  • boat

    Persistent Ubuntu cloud sandboxes with browsers for running AI agents

  • Composio

    Tooling and authentication layer connecting AI agents to external applications

Updated