Skip to main content

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

  1. Click the "Use this template" button on GitHub
  2. Clone your new repository
  3. 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

  1. Never grant AI access to sensitive directories outside the project
  2. Use multiple layers of security - no single mechanism is foolproof
  3. Implement logging of all AI actions for audit purposes
  4. Regularly update dependencies to patch security vulnerabilities
  5. 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:

  1. Path Boundary Enforcement: Ensures all file operations are restricted to the current directory and its subdirectories
  2. Path Validation: Validates paths against platform-specific security rules using pathvalidate
  3. 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:

  1. Limited Builtins: Only safe Python builtins are accessible
  2. Import Restrictions: Prevents importing dangerous modules
  3. 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:

  1. The path is validated using safe_path_access()
  2. If the path points outside the allowed directory, access is denied
  3. If the path contains potentially dangerous patterns, access is denied
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uv_ai_sandbox-0.1.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uv_ai_sandbox-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

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

Hashes for uv_ai_sandbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b37adea5078a48fd31e376c05ea0b82fa7323b7e620f3f80a25a614755b8f2e9
MD5 1af48a4240352c6f1e9f1bbd54f6a339
BLAKE2b-256 7714e40e12f99deb409061295571bc5e69ae8ff5e3f3878e98ba43f742c62a89

See more details on using hashes here.

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

Hashes for uv_ai_sandbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8886c084ca0e8c67a92c34b37309e241f0360f0e7e98de2d1f182411f80ea91
MD5 a4cd0abfbc8ad2ad44f046ae3c16708b
BLAKE2b-256 824e17a0f7aa8d3e57bd99fda5b992c91f10f4ca7e192e7eb52e9d0c6139395a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page