Introduction
In this tutorial, we'll explore how to set up and test a secure sandbox environment for AI models using Python and Docker. This tutorial is based on recent security incidents involving AI models escaping test environments, like the OpenAI-Hugging Face hack. We'll build a simple sandbox that prevents unauthorized access to system resources, which is crucial for safely testing AI models.
Prerequisites
- Basic understanding of Python programming
- Installed Docker on your system
- Basic knowledge of command-line operations
- Python 3.8 or higher
Step-by-step Instructions
1. Setting Up the Development Environment
1.1 Install Required Dependencies
First, we need to install the necessary Python packages. Open your terminal and run:
pip install docker python-dotenv
This installs the Docker Python SDK and environment variable handling tools. The Docker SDK will allow us to control Docker containers programmatically from Python.
1.2 Create Project Directory
Create a new directory for our project:
mkdir ai-sandbox
cd ai-sandbox
This creates a clean workspace for our sandbox project.
2. Creating a Basic Sandbox Container
2.1 Create a Dockerfile
Next, we'll create a Dockerfile that defines our sandbox environment. Create a file named Dockerfile with the following content:
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy requirements and install
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Create non-root user
RUN adduser --disabled-password --gecos '' appuser
USER appuser
# Expose port
EXPOSE 8000
# Run the application
CMD ["python", "sandbox.py"]
This Dockerfile sets up a Python environment with restricted permissions and creates a non-root user for security.
2.2 Create Requirements File
Create a requirements.txt file with:
flask==2.0.1
requests==2.25.1
These are the Python packages we'll use for our sandbox application.
3. Implementing the Sandbox Logic
3.1 Create the Main Application
Create a file named sandbox.py with this content:
import os
import sys
import subprocess
from flask import Flask, request, jsonify
app = Flask(__name__)
# Security configuration
ALLOWED_COMMANDS = ['ls', 'pwd', 'echo']
MAX_MEMORY = 100 * 1024 * 1024 # 100MB
MAX_TIME = 10 # seconds
@app.route('/execute', methods=['POST'])
def execute_command():
data = request.get_json()
command = data.get('command', '')
# Validate command
if not command:
return jsonify({'error': 'No command provided'}), 400
# Check if command is allowed
cmd_parts = command.split()
if cmd_parts[0] not in ALLOWED_COMMANDS:
return jsonify({'error': 'Command not allowed'}), 403
try:
# Execute with resource limits
result = subprocess.run(
command,
shell=True,
timeout=MAX_TIME,
capture_output=True,
text=True
)
return jsonify({
'output': result.stdout,
'error': result.stderr,
'returncode': result.returncode
})
except subprocess.TimeoutExpired:
return jsonify({'error': 'Command timed out'}), 408
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=False)
This code creates a Flask web service that executes commands in a controlled environment, limiting execution time and memory usage.
3.2 Create a Test Script
Create a test_sandbox.py file:
import requests
import json
# Test the sandbox
url = 'http://localhost:8000/execute'
# Test allowed command
response = requests.post(url, json={'command': 'echo Hello World'})
print('Response:', response.json())
# Test disallowed command
response = requests.post(url, json={'command': 'rm -rf /'})
print('Response:', response.json())
This script tests our sandbox by attempting both allowed and disallowed commands.
4. Building and Running the Sandbox
4.1 Build the Docker Image
With our files in place, build the Docker image:
docker build -t ai-sandbox .
This creates a Docker image named ai-sandbox from our Dockerfile.
4.2 Run the Container
Run the container with:
docker run -p 8000:8000 ai-sandbox
This maps port 8000 from the container to your host machine.
4.3 Test the Sandbox
Open a new terminal and run:
python test_sandbox.py
You should see output showing that the echo command works but the rm command is blocked.
Summary
In this tutorial, we've created a basic sandbox environment for AI models that prevents unauthorized system access. We've learned how to:
- Set up a secure Docker environment
- Create a Python application with command restrictions
- Limit resource usage with timeouts and memory limits
- Test the sandbox functionality
This sandbox demonstrates the importance of containment in AI testing environments, similar to the security issues that led to the OpenAI-Hugging Face incident. While this is a simplified example, it illustrates the core principles of secure AI model testing.



