Buckets:

download
raw
26.7 kB
"""
Skills sandboxing and isolation for GRID.
Provides secure execution environments for skills with resource limits,
network isolation, and audit logging. Implements containerized execution
and performance monitoring for skill isolation.
"""
from __future__ import annotations
import asyncio
import json
import logging
import os
import shutil
import sys
import tempfile
import time
import uuid
from dataclasses import dataclass
from datetime import datetime
from enum import StrEnum
from pathlib import Path
from typing import Any
# resource module is Unix-only
try:
import resource
HAS_RESOURCE = True
except ImportError:
HAS_RESOURCE = False
# psutil is optional - used for process monitoring
try:
import psutil # type: ignore[import-untyped]
HAS_PSUTIL = True
except ImportError:
psutil = None # type: ignore
HAS_PSUTIL = False
logger = logging.getLogger(__name__)
class SandboxStatus(StrEnum):
"""Sandbox execution status."""
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
FAILED = "failed"
TIMEOUT = "timeout"
KILLED = "killed"
class ResourceLimit(StrEnum):
"""Resource limit types."""
CPU_TIME = "cpu_time"
MEMORY = "memory"
DISK_SPACE = "disk_space"
NETWORK = "network"
PROCESSES = "processes"
@dataclass
class SandboxConfig:
"""Sandbox configuration."""
max_cpu_time: float = 30.0 # seconds
max_memory: int = 512 * 1024 * 1024 # 512MB
max_disk_space: int = 100 * 1024 * 1024 # 100MB
allow_network: bool = False
allow_filesystem: bool = True
allowed_paths: set[Path] | None = None
environment_variables: dict[str, str] | None = None
timeout: float = 60.0 # seconds
def __post_init__(self):
if self.allowed_paths is None:
self.allowed_paths = set()
if self.environment_variables is None:
self.environment_variables = {}
@dataclass
class SandboxResult:
"""Sandbox execution result."""
execution_id: str
status: SandboxStatus
start_time: datetime
end_time: datetime | None
exit_code: int | None
stdout: str
stderr: str
resource_usage: dict[str, Any]
error_message: str | None
security_violations: list[str]
def to_dict(self) -> dict[str, Any]:
"""Convert to dictionary for serialization."""
return {
"execution_id": self.execution_id,
"status": self.status.value,
"start_time": self.start_time.isoformat(),
"end_time": self.end_time.isoformat() if self.end_time else None,
"exit_code": self.exit_code,
"stdout": self.stdout,
"stderr": self.stderr,
"resource_usage": self.resource_usage,
"error_message": self.error_message,
"security_violations": self.security_violations,
}
class SkillsSandbox:
"""
Secure sandbox for skill execution with resource limits and monitoring.
Features:
- Resource limits (CPU, memory, disk, network)
- Filesystem isolation
- Network isolation
- Process isolation
- Security monitoring
- Performance metrics
- Audit logging
"""
def __init__(self, config: SandboxConfig | None = None):
"""
Initialize skills sandbox.
Args:
config: Sandbox configuration
"""
self.config = config or SandboxConfig()
self._active_executions: dict[str, asyncio.Task] = {}
self._resource_monitors: dict[str, asyncio.Task] = {}
self._execution_history: list[SandboxResult] = []
self._security_violations: list[dict[str, Any]] = []
logger.info("SkillsSandbox initialized")
async def execute_skill(
self,
skill_code: str,
skill_args: dict[str, Any] | None = None,
execution_id: str | None = None,
) -> SandboxResult:
"""
Execute a skill in the sandbox.
Args:
skill_code: Skill code to execute
skill_args: Arguments for the skill
execution_id: Unique execution identifier
Returns:
Sandbox execution result
"""
if not execution_id:
execution_id = f"skill_exec_{int(time.time())}"
start_time = datetime.now()
logger.info(f"Starting skill execution: {execution_id}")
temp_path: Path | None = None
try:
# Use deterministic temp root to avoid ACL issues in constrained runtimes.
temp_path = self._create_execution_directory(execution_id)
# Prepare skill execution
exec_script = self._prepare_execution_script(skill_code, skill_args or {})
script_path = temp_path / "skill.py"
await asyncio.to_thread(script_path.write_text, exec_script)
# Set up resource limits
self._apply_resource_limits()
# Execute with monitoring
try:
result = await self._execute_with_monitoring(
execution_id,
script_path,
temp_path,
start_time,
)
except PermissionError as e:
logger.warning(
"Subprocess execution unavailable for %s; falling back to in-process mode: %s",
execution_id,
e,
)
result = await self._execute_in_process_fallback(
execution_id=execution_id,
skill_code=skill_code,
skill_args=skill_args or {},
work_dir=temp_path,
start_time=start_time,
)
# Store result
self._execution_history.append(result)
logger.info(f"Skill execution completed: {execution_id}")
return result
except Exception as e:
logger.error(f"Skill execution failed: {execution_id} - {e}")
result = SandboxResult(
execution_id=execution_id,
status=SandboxStatus.FAILED,
start_time=start_time,
end_time=datetime.now(),
exit_code=None,
stdout="",
stderr=str(e),
resource_usage={},
error_message=str(e),
security_violations=[],
)
self._execution_history.append(result)
return result
finally:
if temp_path is not None:
shutil.rmtree(temp_path, ignore_errors=True)
def _create_execution_directory(self, execution_id: str) -> Path:
"""Create a per-run execution directory with predictable permissions."""
configured_root = (
os.environ.get("GRID_SANDBOX_TMPDIR") or os.environ.get("GRID_TEST_TMPDIR") or tempfile.gettempdir()
)
temp_root = Path(configured_root) / "grid_skills_sandbox"
temp_root.mkdir(parents=True, exist_ok=True)
execution_dir = temp_root / f"{execution_id}_{uuid.uuid4().hex}"
execution_dir.mkdir(parents=False, exist_ok=False)
return execution_dir
def _prepare_execution_script(self, skill_code: str, skill_args: dict[str, Any]) -> str:
"""Prepare the skill execution script."""
# Wrap skill code in execution harness
harness = f"""
import sys
import json
import traceback
from datetime import datetime
# Skill arguments
SKILL_ARGS = {json.dumps(skill_args)}
# Execution context
execution_context = {{
'start_time': datetime.now().isoformat(),
'args': SKILL_ARGS,
}}
# Skill code
{skill_code}
# Execute main function if exists
if 'main' in locals():
try:
result = main(SKILL_ARGS)
print(f"RESULT: {{json.dumps(result)}}")
except Exception as e:
print(f"ERROR: {{str(e)}}")
traceback.print_exc()
sys.exit(1)
else:
print("ERROR: No main function found in skill")
sys.exit(1)
"""
return harness
def _apply_resource_limits(self) -> None:
"""Apply resource limits to current process."""
if not HAS_RESOURCE:
logger.debug("Resource limits not available on this platform (Windows lacks resource module)")
return
try:
# Set memory limit
resource.setrlimit(resource.RLIMIT_AS, (self.config.max_memory, self.config.max_memory)) # type: ignore[attr-defined]
# Set CPU time limit
resource.setrlimit(resource.RLIMIT_CPU, (self.config.max_cpu_time, self.config.max_cpu_time)) # type: ignore[attr-defined]
# Set process limit
resource.setrlimit( # type: ignore[attr-defined]
resource.RLIMIT_NPROC, # type: ignore[attr-defined]
(10, 10), # Max 10 processes
) # type: ignore[attr-defined]
logger.debug("Resource limits applied")
except (OSError, ValueError) as e:
logger.warning(f"Failed to apply resource limits: {e}")
async def _execute_with_monitoring(
self,
execution_id: str,
script_path: Path,
work_dir: Path,
start_time: datetime,
) -> SandboxResult:
"""Execute skill with resource monitoring."""
process = None
monitor_task = None
try:
# Start the process
process = await asyncio.create_subprocess_exec(
sys.executable,
str(script_path),
cwd=str(work_dir),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=self._get_execution_environment(),
)
# Start resource monitoring
monitor_task = asyncio.create_task(self._monitor_resources(execution_id, process.pid))
# Wait for completion with timeout
try:
async with asyncio.timeout(self.config.timeout):
stdout, stderr = await process.communicate()
status = SandboxStatus.COMPLETED if process.returncode == 0 else SandboxStatus.FAILED
except TimeoutError:
# Kill the process on timeout
process.kill()
await process.wait()
stdout, stderr = await process.communicate()
status = SandboxStatus.TIMEOUT
# Stop monitoring
if monitor_task:
monitor_task.cancel()
try:
await monitor_task
except asyncio.CancelledError:
pass
# Get resource usage
resource_usage = await self._get_resource_usage(execution_id)
# Decode output
stdout_text = stdout.decode("utf-8") if stdout else ""
stderr_text = stderr.decode("utf-8") if stderr else ""
# Check for security violations (read executed script for pattern check)
script_code = await asyncio.to_thread(script_path.read_text, encoding="utf-8", errors="replace")
violations = self._check_security_violations(execution_id, work_dir, skill_code=script_code)
return SandboxResult(
execution_id=execution_id,
status=status,
start_time=start_time,
end_time=datetime.now(),
exit_code=process.returncode,
stdout=stdout_text,
stderr=stderr_text,
resource_usage=resource_usage,
error_message=None if status == SandboxStatus.COMPLETED else stderr_text,
security_violations=violations,
)
except Exception:
# Cleanup on error
if monitor_task:
monitor_task.cancel()
try:
await monitor_task
except asyncio.CancelledError:
pass
if process:
process.kill()
await process.wait()
raise
async def _execute_in_process_fallback(
self,
execution_id: str,
skill_code: str,
skill_args: dict[str, Any],
work_dir: Path,
start_time: datetime,
) -> SandboxResult:
"""Fallback execution path when subprocess creation is blocked."""
# CRIT-4: Check security violations BEFORE execution, not after.
violations = self._check_security_violations(execution_id, work_dir, skill_code=skill_code)
if violations:
logger.warning(
"Sandbox fallback BLOCKED for %s: security violations detected before exec: %s",
execution_id,
violations,
)
return SandboxResult(
execution_id=execution_id,
status=SandboxStatus.FAILED,
start_time=start_time,
end_time=datetime.now(),
exit_code=1,
stdout="",
stderr="Execution blocked: security violations detected before execution",
resource_usage=await self._get_resource_usage(execution_id),
error_message="Security violations detected — execution refused",
security_violations=violations,
)
# CRIT-5: Gate fallback exec() behind explicit environment flag.
allow_inprocess_exec = os.getenv("GRID_ALLOW_INPROCESS_EXEC", "").strip().lower() in ("1", "true", "yes")
if not allow_inprocess_exec:
logger.error(
"Sandbox fallback BLOCKED for %s: GRID_ALLOW_INPROCESS_EXEC not set. "
"Set to '1' to enable fallback execution (NOT RECOMMENDED FOR PRODUCTION).",
execution_id,
)
return SandboxResult(
execution_id=execution_id,
status=SandboxStatus.FAILED,
start_time=start_time,
end_time=datetime.now(),
exit_code=1,
stdout="",
stderr="Execution blocked: GRID_ALLOW_INPROCESS_EXEC not enabled",
resource_usage=await self._get_resource_usage(execution_id),
error_message="Fallback exec() disabled - set GRID_ALLOW_INPROCESS_EXEC=1 to enable (production unsafe)",
security_violations=[],
)
def _run_in_process() -> Any:
# Build restricted builtins — deny dangerous primitives.
import builtins as _builtins
_DENIED_BUILTINS = frozenset(
{
"eval",
"exec",
"compile",
"open",
"globals",
"locals",
"vars",
"getattr",
"setattr",
"delattr",
"breakpoint",
"exit",
"quit",
}
)
# Filtered __import__ — blocks dangerous modules at runtime.
_BLOCKED_MODULES = frozenset(
{
"os",
"subprocess",
"socket",
"shutil",
"ctypes",
"pickle",
"shelve",
"marshal",
"importlib",
"signal",
"multiprocessing",
"_thread",
"threading",
}
)
_real_import = _builtins.__import__
def _safe_import(name, *args, **kwargs):
root = name.split(".")[0]
if root in _BLOCKED_MODULES:
raise ImportError(f"Module '{name}' is blocked in sandbox")
return _real_import(name, *args, **kwargs)
safe_builtins = {
k: getattr(_builtins, k) for k in dir(_builtins) if not k.startswith("_") and k not in _DENIED_BUILTINS
}
# Preserve __name__ and __build_class__ so class defs work
safe_builtins["__name__"] = "__sandbox__"
safe_builtins["__build_class__"] = _builtins.__build_class__
safe_builtins["__import__"] = _safe_import
namespace: dict[str, Any] = {"__builtins__": safe_builtins}
exec(skill_code, namespace) # noqa: S102 - fallback execution only when GRID_ALLOW_INPROCESS_EXEC=1
main = namespace.get("main")
if not callable(main):
raise RuntimeError("No main function found in skill")
return main(skill_args)
try:
async with asyncio.timeout(self.config.timeout):
value = await asyncio.to_thread(_run_in_process)
stdout_text = f"RESULT: {json.dumps(value)}"
stderr_text = ""
status = SandboxStatus.COMPLETED
exit_code = 0
error_message = None
except TimeoutError:
stdout_text = ""
stderr_text = f"Execution timed out after {self.config.timeout}s"
status = SandboxStatus.TIMEOUT
exit_code = 124
error_message = stderr_text
except Exception as e:
stdout_text = ""
stderr_text = str(e)
status = SandboxStatus.FAILED
exit_code = 1
error_message = str(e)
return SandboxResult(
execution_id=execution_id,
status=status,
start_time=start_time,
end_time=datetime.now(),
exit_code=exit_code,
stdout=stdout_text,
stderr=stderr_text,
resource_usage=await self._get_resource_usage(execution_id),
error_message=error_message,
security_violations=violations,
)
def _get_execution_environment(self) -> dict[str, str]:
"""Get execution environment variables."""
import os
env = os.environ.copy()
# Add sandbox-specific variables
if self.config.environment_variables:
env.update(self.config.environment_variables)
env["SANDBOX_EXECUTION"] = "true"
env["SANDBOX_START_TIME"] = datetime.now().isoformat()
# Remove potentially dangerous variables
dangerous_vars = [
"PYTHONPATH",
"PYTHONHOME",
"LD_LIBRARY_PATH",
"DYLD_LIBRARY_PATH",
]
for var in dangerous_vars:
env.pop(var, None)
return env
async def _monitor_resources(self, execution_id: str, pid: int) -> None:
"""Monitor resource usage during execution."""
if not HAS_PSUTIL or psutil is None:
# psutil not available - skip resource monitoring
logger.debug(f"Resource monitoring skipped for {execution_id}: psutil not installed")
return
try:
process = psutil.Process(pid)
monitor_task = asyncio.current_task()
if monitor_task:
self._resource_monitors[execution_id] = monitor_task
while process.is_running() and process.status() != psutil.STATUS_ZOMBIE:
try:
# Collect metrics
num_fds_func = getattr(process, "num_fds", None)
metrics = {
"cpu_percent": process.cpu_percent(),
"memory_mb": process.memory_info().rss / 1024 / 1024,
"memory_percent": process.memory_percent(),
"num_threads": process.num_threads(),
"num_fds": num_fds_func() if callable(num_fds_func) else 0,
"timestamp": datetime.now().isoformat(),
}
# Store metrics
if execution_id not in self._active_executions:
active_task = asyncio.current_task()
if active_task:
self._active_executions[execution_id] = active_task
# Check limits
if metrics["memory_mb"] > self.config.max_memory / 1024 / 1024:
logger.warning(f"Memory limit exceeded for {execution_id}")
process.kill()
break
await asyncio.sleep(0.1) # Monitor every 100ms
except (psutil.NoSuchProcess, psutil.AccessDenied):
break
except Exception as e:
logger.error(f"Resource monitoring failed for {execution_id}: {e}")
async def _get_resource_usage(self, execution_id: str) -> dict[str, Any]:
"""Get resource usage statistics."""
# This is a simplified version - in production, you'd aggregate
# the metrics collected during monitoring
return {
"peak_memory_mb": 0,
"peak_cpu_percent": 0,
"execution_time_seconds": 0,
"disk_usage_mb": 0,
}
def _check_security_violations(self, execution_id: str, work_dir: Path, skill_code: str | None = None) -> list[str]:
"""Check for security violations using both substring and AST-based analysis.
If skill_code is provided, check for suspicious patterns before execution.
"""
import ast as _ast
violations: list[str] = []
try:
# Check for unauthorized file access
if not self.config.allow_filesystem:
violations.extend(
f"Unauthorized file access: {file_path}" for file_path in work_dir.rglob("*") if file_path.is_file()
)
if skill_code is not None:
# Layer 1: Fast substring pre-screen
suspicious_patterns = [
"import os",
"import subprocess",
"import socket",
"eval(",
"exec(",
"__import__",
]
violations.extend(
f"Suspicious pattern detected: {pattern}"
for pattern in suspicious_patterns
if pattern in skill_code
)
# Layer 2: AST-based deep analysis (catches obfuscation)
try:
tree = _ast.parse(skill_code)
_blocked_imports = frozenset(
{
"os",
"subprocess",
"socket",
"shutil",
"ctypes",
"pickle",
"shelve",
"marshal",
}
)
_dangerous_calls = frozenset(
{
"eval",
"exec",
"compile",
"__import__",
}
)
_dangerous_attrs = frozenset({"system", "popen", "Popen"})
for node in _ast.walk(tree):
if isinstance(node, _ast.Import):
for alias in node.names:
root = alias.name.split(".")[0]
if root in _blocked_imports:
violations.append(f"AST: blocked import '{alias.name}' at line {node.lineno}")
elif isinstance(node, _ast.ImportFrom):
if node.module:
root = node.module.split(".")[0]
if root in _blocked_imports:
violations.append(f"AST: blocked import from '{node.module}' at line {node.lineno}")
elif isinstance(node, _ast.Call):
func = node.func
if isinstance(func, _ast.Name) and func.id in _dangerous_calls:
violations.append(f"AST: dangerous call '{func.id}()' at line {node.lineno}")
elif isinstance(func, _ast.Attribute) and func.attr in _dangerous_attrs:
violations.append(f"AST: dangerous method '.{func.attr}()' at line {node.lineno}")
except SyntaxError as parse_err:
violations.append(f"Skill code has syntax error: {parse_err}")
except Exception as e:
logger.error("Security violation check failed: %s", e)
return violations
def get_execution_history(self, limit: int = 100) -> list[SandboxResult]:
"""Get execution history."""
return self._execution_history[-limit:]
def get_active_executions(self) -> list[str]:
"""Get list of active executions."""
return list(self._active_executions.keys())
async def kill_execution(self, execution_id: str) -> bool:
"""Kill an active execution."""
if execution_id in self._active_executions:
task = self._active_executions[execution_id]
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
del self._active_executions[execution_id]
return True
return False
def get_security_violations(self, limit: int = 100) -> list[dict[str, Any]]:
"""Get security violations."""
return self._security_violations[-limit:]
def cleanup(self) -> None:
"""Clean up resources."""
# Cancel all active executions
for execution_id in list(self._active_executions.keys()):
asyncio.create_task(self.kill_execution(execution_id))
# Clear monitors
self._resource_monitors.clear()
logger.info("Sandbox cleanup completed")
# Global sandbox instance
_global_sandbox: SkillsSandbox | None = None
def get_skills_sandbox() -> SkillsSandbox:
"""Get or create global skills sandbox instance."""
global _global_sandbox
if _global_sandbox is None:
_global_sandbox = SkillsSandbox()
return _global_sandbox
def set_skills_sandbox(sandbox: SkillsSandbox) -> None:
"""Set global skills sandbox instance."""
global _global_sandbox
_global_sandbox = sandbox
# Backward compatibility alias
SkillSandbox = SkillsSandbox

Xet Storage Details

Size:
26.7 kB
·
Xet hash:
5a62a3600a1175df1282ea686e6da4755a38c49ac8c9ff85be077ac73beba43d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.