Sandboxed environment for running AI models with strict security boundaries
Project description
AI Sandbox Template
A GitHub template for creating sandboxed environments to safely run AI models and execute AI-generated code with strict security boundaries.
๐ก๏ธ Security Features
- Restricted file system access (limited to project directory)
- Controlled code execution using RestrictedPython
- Path validation and sanitization
- Process isolation capabilities
๐ Getting Started
Use this template
- Click the "Use this template" button on GitHub
- Clone your new repository
- Set up the environment with uv:
# Install uv if you don't have it already
# pip install uv
# Create virtual environment with Python 3.13
uv venv --python 3.13 .venv
# Activate virtual environment
# On Windows:
.venv\Scripts\activate
# On Unix/MacOS:
source .venv/bin/activate
# Install dependencies using uv
uv pip install -e ".[dev]"
# For Linux-specific security features (requires libcap-dev)
# uv pip install -e ".[linux-security]"
๐ Repository Structure
/
โโโ src/
โ โโโ ai_sandbox/ # Main package code
โ โโโ __init__.py
โ โโโ security.py # Security utilities
โ โโโ executor.py # Safe code execution utilities
โโโ examples/ # Example usage
โ โโโ safe_execution.py # Example of running AI code safely
โโโ tests/ # Test cases
โ โโโ test_executor.py
โ โโโ test_security.py
โโโ pyproject.toml # Package configuration
โโโ README.md
โโโ LICENSE
๐งฐ Usage Example
from ai_sandbox.security import safe_path_access, SecurityError
from ai_sandbox.executor import execute_ai_code
# Safely access files only within project directory
try:
file_path = safe_path_access("data/training.json")
with open(file_path, 'r') as f:
data = f.read()
except SecurityError as e:
print(f"Security violation: {e}")
# Safely execute AI-generated code with restrictions
ai_code = """
def analyze_data(input_data):
return {"summary": "Data analysis complete", "count": len(input_data)}
result = analyze_data(input_data)
"""
result = execute_ai_code(ai_code, {"input_data": [1, 2, 3]})
print(result) # Output: {"summary": "Data analysis complete", "count": 3}
๐ Best Practices
- Never grant AI access to sensitive directories outside the project
- Use multiple layers of security - no single mechanism is foolproof
- Implement logging of all AI actions for audit purposes
- Regularly update dependencies to patch security vulnerabilities
- Run security scanning tools like Bandit regularly
โ ๏ธ Limitations
- This sandbox provides basic isolation but is not equivalent to container/VM isolation
- Different security mechanisms are available on different operating systems
- Python's dynamic nature makes 100% secure sandboxing difficult
๐ Security Implementation
Path Security
The AI Sandbox uses multiple layers to ensure file system security:
- Path Boundary Enforcement: Ensures all file operations are restricted to the current directory and its subdirectories
- Path Validation: Validates paths against platform-specific security rules using
pathvalidate - Absolute Path Resolution: Converts all paths to absolute and resolved form before validation
Code Execution Security
The sandbox uses RestrictedPython to enforce several security measures:
- Limited Builtins: Only safe Python builtins are accessible
- Import Restrictions: Prevents importing dangerous modules
- Execution Isolation: Code executes in a restricted environment with limited access to the global namespace
Example Security Flow
When AI-generated code attempts to access a file:
- The path is validated using
safe_path_access() - If the path points outside the allowed directory, access is denied
- If the path contains potentially dangerous patterns, access is denied
- Only after passing all security checks is file access granted
๐ Testing
Run the security tests to verify sandbox functionality:
# Run all tests
uv run pytest tests/
# Run with coverage
uv run pytest --cov=ai_sandbox tests/
# Run specific test file
uv run pytest tests/test_executor.py
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ง Development
# Install development dependencies
uv pip install -e ".[dev]"
# Run linting
uv run ruff check .
# Run code formatting
uv run black .
# Run security checks
uv run bandit -r src/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file uv_ai_sandbox-0.1.0.tar.gz.
File metadata
- Download URL: uv_ai_sandbox-0.1.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b37adea5078a48fd31e376c05ea0b82fa7323b7e620f3f80a25a614755b8f2e9
|
|
| MD5 |
1af48a4240352c6f1e9f1bbd54f6a339
|
|
| BLAKE2b-256 |
7714e40e12f99deb409061295571bc5e69ae8ff5e3f3878e98ba43f742c62a89
|
File details
Details for the file uv_ai_sandbox-0.1.0-py3-none-any.whl.
File metadata
- Download URL: uv_ai_sandbox-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8886c084ca0e8c67a92c34b37309e241f0360f0e7e98de2d1f182411f80ea91
|
|
| MD5 |
a4cd0abfbc8ad2ad44f046ae3c16708b
|
|
| BLAKE2b-256 |
824e17a0f7aa8d3e57bd99fda5b992c91f10f4ca7e192e7eb52e9d0c6139395a
|