Backend API¶
SandboxBackend (Abstract Base Class)¶
- class noxrunner.backend.base.SandboxBackend[source]¶
Bases:
ABCAbstract base class for sandbox execution backends.
All backends (local, HTTP, k8s, docker, etc.) must implement this interface. This provides a unified API regardless of the underlying implementation.
- abstractmethod health_check()[source]¶
Check if the backend is healthy.
- Returns:
True if healthy, False otherwise
- Return type:
- abstractmethod create_sandbox(session_id, ttl_seconds=900, image=None, cpu_limit=None, memory_limit=None, ephemeral_storage_limit=None)[source]¶
Create or ensure a sandbox exists.
- Parameters:
session_id (str) – Unique session identifier
ttl_seconds (int) – Time to live in seconds (default: 900)
image (str | None) – Container image (optional)
cpu_limit (str | None) – CPU limit (optional, e.g., “1”)
memory_limit (str | None) – Memory limit (optional, e.g., “1Gi”)
ephemeral_storage_limit (str | None) – Ephemeral storage limit (optional, e.g., “2Gi”)
- Returns:
Dict with ‘podName’ (or equivalent) and ‘expiresAt’
- Return type:
- abstractmethod exec(session_id, cmd, workdir='/workspace', env=None, timeout_seconds=30)[source]¶
Execute a command in the sandbox.
- Parameters:
- Returns:
Dict with ‘exitCode’, ‘stdout’, ‘stderr’, ‘durationMs’
- Return type:
- abstractmethod upload_files(session_id, files, dest='/workspace')[source]¶
Upload files to the sandbox.
- abstractmethod download_files(session_id, src='/workspace')[source]¶
Download files from the sandbox as a tar archive.
HTTPSandboxBackend¶
HTTP client backend for remote NoxRunner services.
- class noxrunner.backend.http.HTTPSandboxBackend(base_url, timeout=30)[source]¶
Bases:
SandboxBackendHTTP client backend for NoxRunner sandbox execution.
This backend communicates with a remote NoxRunner-compatible API via HTTP. The remote service may be implemented using Kubernetes, Docker, or other technologies.
This backend acts as an HTTP client and does not implement the sandbox itself. It connects to a remote service that provides the actual sandbox implementation.
- __init__(base_url, timeout=30)[source]¶
Initialize the HTTP backend.
- Parameters:
base_url (str) – Base URL of the NoxRunner backend (e.g., “http://127.0.0.1:8080”)
timeout (int) – Request timeout in seconds (default: 30)
- create_sandbox(session_id, ttl_seconds=900, image=None, cpu_limit=None, memory_limit=None, ephemeral_storage_limit=None)[source]¶
Create or ensure a sandbox exists.
- exec(session_id, cmd, workdir='/workspace', env=None, timeout_seconds=30)[source]¶
Execute a command in the sandbox.
LocalBackend¶
Local filesystem backend for development and testing.
- class noxrunner.backend.local.LocalBackend(base_dir='/tmp')[source]¶
Bases:
SandboxBackendLocal filesystem backend for offline testing.
WARNING: This backend executes commands in the local environment using temporary directories. It should ONLY be used for testing purposes. Using this in production can cause severe data loss or security risks.
- __init__(base_dir='/tmp')[source]¶
Initialize local sandbox backend.
- Parameters:
base_dir (str) – Base directory for sandbox storage (default: /tmp)
- create_sandbox(session_id, ttl_seconds=900, image=None, cpu_limit=None, memory_limit=None, ephemeral_storage_limit=None)[source]¶
Create or ensure a sandbox exists.
- Parameters:
session_id (str) – Unique session identifier
ttl_seconds (int) – Time to live in seconds
image (str | None) – Container image (ignored in local mode)
cpu_limit (str | None) – CPU limit (ignored in local mode)
memory_limit (str | None) – Memory limit (ignored in local mode)
ephemeral_storage_limit (str | None) – Storage limit (ignored in local mode)
- Returns:
Dict with ‘podName’ and ‘expiresAt’
- Return type:
- exec(session_id, cmd, workdir='/workspace', env=None, timeout_seconds=30)[source]¶
Execute a command in the sandbox.
WARNING: This executes commands in the local environment!
- download_files(session_id, src='/workspace')[source]¶
Download files from the sandbox as a tar archive.
Internal Modules¶
Security Module¶
Command validation for sandbox security.
This module provides command validation to prevent dangerous operations in sandbox environments.
- class noxrunner.security.command_validator.CommandValidator[source]¶
Bases:
objectValidates commands for safety in sandbox environments.
This validator checks commands against allowlists and blocklists to prevent dangerous operations.
- ALLOWED_COMMANDS = {'[', 'awk', 'bash', 'cat', 'cmp', 'cp', 'cut', 'diff', 'echo', 'env', 'false', 'file', 'find', 'grep', 'gunzip', 'gzip', 'head', 'ln', 'ls', 'mkdir', 'mv', 'node', 'printenv', 'pwd', 'python', 'python2', 'python3', 'readlink', 'sed', 'sh', 'sort', 'stat', 'tail', 'tar', 'test', 'touch', 'tr', 'true', 'type', 'uniq', 'unzip', 'wc', 'which', 'xargs', 'zip', 'zsh'}¶
- BLOCKED_COMMANDS = {'chgrp', 'chmod', 'chown', 'dd', 'del', 'fdisk', 'format', 'halt', 'init', 'killall', 'mkfs', 'mount', 'poweroff', 'reboot', 'rm', 'rmdir', 'shutdown', 'su', 'sudo', 'umount', 'unlink'}¶
Path sanitization for sandbox security.
This module provides path sanitization to prevent path traversal attacks in sandbox environments.
- class noxrunner.security.path_sanitizer.PathSanitizer[source]¶
Bases:
objectSanitizes paths to ensure they’re within the sandbox.
Security: Prevents path traversal attacks by ensuring all paths are within the sandbox directory.
- sanitize(path, sandbox_path, workspace_name='workspace')[source]¶
Sanitize a path to ensure it’s within the sandbox.
- Parameters:
- Returns:
Sanitized Path object that is guaranteed to be within sandbox
- Return type:
- Security:
Prevents path traversal attacks (../)
Redirects paths outside sandbox to workspace root
Handles both absolute and relative paths
File Operations Module¶
Tar archive handling utilities.
This module provides utilities for creating and extracting tar archives used in file synchronization between local and sandbox environments.
- class noxrunner.fileops.tar_handler.TarHandler[source]¶
Bases:
objectHandles tar archive creation and extraction.
This class provides methods to create tar archives from file dictionaries and extract tar archives to directories, with security checks.