A secure and efficient API that executes user-submitted Python scripts inside a tightly controlled environment. The code is expected to define a main() function which returns a JSON-serializable response.
🔒 Isolated Python execution using NsJail sandbox 🔄 Flask API with a single /execute endpoint 📤 Captures stdout and return values as part of output 🐳 Optimized Docker setup for local and cloud use 🛡️ Cloud-safe build with chroot and seccomp hardening 🧰 Supports numpy, pandas, and standard Python modules 🌐 Includes basic web IDE built in React (optional)
To run the API locally using Docker:
docker build -t python-sandbox-api --build-arg BUILD=local .
docker run -p 8080:8080 python-sandbox-apiThis project is also compatible with Cloud Run and accounts for gVisor’s syscall restrictions.
docker build -t python-api --build-arg BUILD=cloud .Cloud Run uses gVisor under the hood, which blocks certain system operations:
❌ clone(CLONE_NEWUSER), CLONE_NEWPID, etc. → Not allowed
✅ chroot() → Permitted and used for filesystem sandboxing
Instead of relying on namespace-based isolation, this implementation switches to chroot mode with a pre-bundled Python runtime and minimal libraries.
Precompiled Python 3.11 binary and core libraries
Controlled /tmp execution directory
Read-only mounts and environment whitelisting
Seccomp filtering to block risky operations
KILL {
chmod, fchmod, fchmodat,
chown, fchown, lchown,
setuid, setgid, setreuid, setregid,
setresuid, setresgid, setfsuid, setfsgid,
setgroups,
mount, umount
} DEFAULT ALLOW
This policy ensures no privilege escalation or filesystem tampering.
Scripts are required to contain a main() function
All code is wrapped in a controlled executor before running
Output includes:
- result: return value from main()
- stdout: printed output
- error and trace: if execution fails
A frontend playground is also available to test the API in-browser. Built with:
React + Vite
CodeMirror for code editing
Lucide icons
Axios for backend integration
This provides a convenient way to test the /execute endpoint interactively.
Built using React, CodeMirror, axios, and Tailwind-style UI with Lucide icons.
cd client
npm install
npm run devVisit http://localhost:5173 in your browser.
curl -X POST https://<your-api>/execute \
-H "Content-Type: application/json" \
-d '{"script": "def main():\n print(\"Hi!\")\n return {\"done\": True}"}'curl -X POST https://<your-api>/execute \
-H "Content-Type: application/json" \
-d '{"script": "def main():\n import numpy as np\n return {\"avg\": float(np.mean([1,2,3]))}"}'curl -X POST https://<your-api>/execute \
-H "Content-Type: application/json" \
-d '{"script": "def main():\n import os\n os.mkdir(\"/root\")\n return {}"}'
This project was built over the course of 15–20 focused hours, covering:
-
🔧 Designing and optimizing Docker builds for both local and cloud execution
-
🛠️ Adapting NsJail to function seamlessly in Cloud Run by accounting for gVisor limitations
-
🔒 Implementing secure Python sandboxing using chroot and restrictive seccomp rules
-
🧪 Developing a robust Flask API with layered validation and structured error reporting
-
💻 Crafting a React-based IDE to test and visualize script execution in real-time
-
🧾 Writing clear documentation and validating against various edge-case scenarios
Local Image: 301MB Production Image: 509MB