Sandbox Isolation
Monty is designed to safely execute untrusted code by providing complete isolation from the host environment. The interpreter implements a strict sandbox that prevents code from accessing or modifying the host system.What’s Blocked
Monty blocks all direct access to:- Filesystem: No direct file I/O operations
- Environment Variables: Cannot read or modify environment variables
- Network Access: No sockets or HTTP requests
- Subprocess Execution: Cannot spawn processes or run shell commands
- Import System: Cannot import modules with side effects or use
__import__ - Third-party Libraries: No external Python packages
- Most Standard Library: Only a minimal subset is available
What’s Allowed
Monty provides controlled access through:- External Functions: Only functions explicitly provided by the host
- OS Operations: Filesystem and network operations handled via host callbacks
- Limited Standard Library:
sys,os,typing,asyncio,remodules - Safe Operations: Pure computation, data structures, control flow
Security Guarantees
Complete Host Isolation
Monty never uses CPython’sexec() or any FFI to Python. It’s a completely independent interpreter written in Rust that provides:
- No filesystem access without explicit host callbacks
- No environment variable access
- No network access without explicit host callbacks
- No subprocess execution
- No access to system resources
Memory Safety
Monty is written in Rust withoutunsafe code (except in carefully reviewed FFI boundaries for Python/JavaScript bindings). This provides:
- No buffer overflows
- No use-after-free errors
- No null pointer dereferences
- Safe memory management with reference counting
Resource Limits
Monty enforces configurable limits to prevent denial-of-service attacks:- Memory limits: Cap maximum heap usage
- Execution time: Timeout after specified duration
- Allocation count: Limit number of heap allocations
- Recursion depth: Prevent stack overflow (default: 1000)
See the Resource Limits page for detailed configuration options.
Controlled External Access
External Functions
All host interaction happens through external functions that you explicitly provide:OS Operations
Filesystem and network operations are never performed directly. Instead, they trigger callbacks that you handle:Deserialization Safety
Always validate the source of serialized data before calling:Monty.load(bytes)FunctionSnapshot.load(bytes)RunProgress.load(bytes)
Threat Model
Monty is designed to protect against:Protected Against
- Code Injection: Untrusted Python code execution
- Resource Exhaustion: Memory bombs, infinite loops
- Information Disclosure: No access to host environment
- Path Traversal: No filesystem access without your control
- Regex DoS: Controlled through execution limits
- Import Abuse: Only safe minimal standard library available
Out of Scope
- Timing Attacks: Execution timing is observable
- Deserialization Attacks: Don’t load snapshots from untrusted sources
- Host Function Vulnerabilities: Security of external functions is your responsibility
Why Monty?
Traditional sandboxing approaches have significant limitations:| Solution | Start Latency | Security | Complexity |
|---|---|---|---|
| Monty | 0.06ms | Strict | Easy |
| Docker | 195ms | Good | Intermediate |
| Pyodide/WASM | 2800ms | Poor | Intermediate |
| Subprocess | 30ms | None | Easy |
| Direct exec() | 0.1ms | None | Easy |
Best Practices
Whitelist External Functions
Only provide the minimum set of external functions needed. Apply strict validation within each function.
Handle OS Callbacks Safely
When using
start()/resume(), apply security checks in your OS operation handlers.