fix(sandbox): resolve saved environment paths during validation
This commit is contained in:
1 parent
fb159257cb
commit
16250540d3
5 files changed
+97
-12
No files matched your search
@@ -19,6 +19,8 @@ from nanobot.agent.tools.sandbox_launcher import (
|
||||
sandbox_platform,
|
||||
)
|
||||
from nanobot.agent.tools.shell_config import ExecToolConfig
|
||||
from nanobot.config.errors import ConfigLoadError
|
||||
from nanobot.config.loader import resolve_config_env_vars
|
||||
|
||||
_PROBE_TIMEOUT_S = 5
|
||||
|
||||
@@ -32,9 +34,19 @@ class SandboxCheck(TypedDict):
|
||||
|
||||
|
||||
def sandbox_settings_status(config: ExecToolConfig, workspace: Path) -> SandboxCheck:
|
||||
return _prepare_settings(config, workspace)[2]
|
||||
|
||||
|
||||
def _prepare_settings(config: ExecToolConfig, workspace: Path) -> tuple[ExecToolConfig, Path, SandboxCheck]:
|
||||
import sys
|
||||
|
||||
backend = "bwrap" if sys.platform.startswith("linux") else "seatbelt" if sys.platform == "darwin" else None
|
||||
missing_env = False
|
||||
try:
|
||||
config = resolve_config_env_vars(config)
|
||||
workspace = Path(resolve_config_env_vars(str(workspace))).expanduser()
|
||||
except ConfigLoadError:
|
||||
missing_env = True
|
||||
revision = hashlib.sha256(json.dumps(
|
||||
{"config": config.model_dump(), "workspace": str(workspace)}, sort_keys=True,
|
||||
).encode()).hexdigest()
|
||||
@@ -48,6 +60,8 @@ def sandbox_settings_status(config: ExecToolConfig, workspace: Path) -> SandboxC
|
||||
result["state"] = "off"
|
||||
elif sandbox_platform(config.sandbox) == "unsupported":
|
||||
result.update(state="unsupported", reason="platform_unsupported")
|
||||
elif missing_env:
|
||||
result.update(state="unavailable", reason="path_unavailable")
|
||||
else:
|
||||
try:
|
||||
result["launcher"] = resolve_sandbox_launcher(
|
||||
@@ -56,11 +70,11 @@ def sandbox_settings_status(config: ExecToolConfig, workspace: Path) -> SandboxC
|
||||
)
|
||||
except SandboxLauncherError as exc:
|
||||
result.update(state="unavailable", reason=exc.reason)
|
||||
return result
|
||||
return config, workspace, result
|
||||
|
||||
|
||||
async def check_sandbox(config: ExecToolConfig, workspace: Path) -> SandboxCheck:
|
||||
result = await asyncio.to_thread(sandbox_settings_status, config, workspace)
|
||||
config, workspace, result = await asyncio.to_thread(_prepare_settings, config, workspace)
|
||||
if result["state"] != "unchecked":
|
||||
return result
|
||||
# Use synthetic files only. Never run startup/login profiles or an arbitrary
|
||||
|
||||
@@ -2,7 +2,7 @@ import json
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any, cast, overload
|
||||
from typing import Any, TypeVar, cast, overload
|
||||
|
||||
from pydantic import BaseModel, ValidationError
|
||||
from pydantic_settings import SettingsError
|
||||
@@ -180,14 +180,15 @@ def merge_missing_defaults(existing: object, defaults: object) -> object:
|
||||
|
||||
|
||||
_ENV_REF_PATTERN = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}")
|
||||
_ConfigValue = TypeVar("_ConfigValue")
|
||||
|
||||
|
||||
def resolve_config_env_vars(
|
||||
config: Config,
|
||||
config: _ConfigValue,
|
||||
*,
|
||||
config_path: Path | None = None,
|
||||
) -> Config:
|
||||
"""Return *config* with ``${VAR}`` env-var references resolved.
|
||||
) -> _ConfigValue:
|
||||
"""Return a config or config subtree with ``${VAR}`` references resolved.
|
||||
|
||||
Walks in place so fields declared with ``exclude=True`` survive;
|
||||
returns the same instance when no references are present.
|
||||
|
||||
@@ -8,6 +8,8 @@ from typing import Any, cast
|
||||
|
||||
from pydantic import BaseModel, TypeAdapter, ValidationError
|
||||
|
||||
from nanobot.config.errors import ConfigLoadError
|
||||
from nanobot.config.loader import resolve_config_env_vars
|
||||
from nanobot.config.schema import Config
|
||||
from nanobot.security.network import is_loopback_host
|
||||
from nanobot.webui.settings_contracts import WebUISettingsError
|
||||
@@ -160,10 +162,14 @@ def update_runtime_config(
|
||||
)
|
||||
|
||||
try:
|
||||
exec_config = resolve_config_env_vars(validated.tools.exec)
|
||||
workspace = Path(resolve_config_env_vars(str(validated.workspace_path))).expanduser()
|
||||
resolve_sandbox_launcher(
|
||||
validated.tools.exec.sandbox, validated.tools.exec.sandbox_launcher,
|
||||
writable_roots=(validated.workspace_path, *map(Path, validated.tools.exec.sandbox_rw_binds)),
|
||||
exec_config.sandbox, exec_config.sandbox_launcher,
|
||||
writable_roots=(workspace, *map(Path, exec_config.sandbox_rw_binds)),
|
||||
)
|
||||
except ConfigLoadError:
|
||||
raise WebUISettingsError("sandbox_launcher_path_unavailable") from None
|
||||
except SandboxLauncherError as exc:
|
||||
raise WebUISettingsError(f"sandbox_launcher_{exc.reason}") from None
|
||||
if "tools.ssrf_whitelist" in values:
|
||||
|
||||
Reference in new issue
Block a user