Files
nanobot/nanobot/webui/settings_runtime.py
T
Xubin Ren 90d69e7a11 refactor(exec): share the sandbox launch path with settings checks (#6645)
* refactor(exec): share the sandbox launch path with settings checks

* refactor(sandbox): remove duplicate platform and path bookkeeping
2026-11-13 15:29:01 +08:00

193 lines
8.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
import ipaddress
import math
import re
from pathlib import Path, PurePosixPath, PureWindowsPath
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.webui.settings_contracts import WebUISettingsError
# Only these leaves may be read or patched through this surface. Model/provider,
# channel, and secret configuration keep their dedicated mutation contracts.
RUNTIME_CONFIG_PATHS = (
"agents.defaults.timezone",
"agents.defaults.timezone_mode",
"agents.defaults.session_ttl_minutes",
"agents.defaults.max_tool_iterations",
"agents.defaults.max_concurrent_subagents",
"agents.defaults.max_tool_result_chars",
"agents.defaults.provider_retry_mode",
"agents.defaults.tool_hint_max_length",
"agents.defaults.dream.enabled",
"gateway.host",
"gateway.port",
"gateway.restart_mode",
"tools.exec.enable",
"tools.exec.timeout",
"tools.exec.path_prepend",
"tools.exec.path_append",
"tools.exec.sandbox",
"tools.exec.sandbox_launcher",
"tools.exec.sandbox_ro_binds",
"tools.exec.sandbox_rw_binds",
"tools.exec.allowed_env_keys",
"tools.exec.allow_patterns",
"tools.exec.deny_patterns",
"tools.file.enable",
"tools.my.enable",
"tools.my.allow_set",
"tools.cli_apps.enable",
"tools.cli_apps.install_timeout",
"tools.cli_apps.run_timeout",
"tools.cli_apps.catalog_ttl_seconds",
"tools.restrict_to_workspace",
"tools.ssrf_whitelist",
"tools.max_session_messages_per_minute",
"tools.webui_allow_remote_package_install",
"tools.web.enable",
"network.proxy",
"tools.web.use_proxy",
"tools.web.user_agent",
"tools.image_generation.save_dir",
)
def _parent(config: Config, path: str) -> tuple[BaseModel, str]:
# Keep the existing WebUI editor on the canonical hard-runtime field.
if path == "tools.exec.timeout":
path = "tools.exec.max_runtime"
parent: BaseModel = config
*parts, leaf = path.split(".")
for part in parts:
value: object = getattr(parent, part)
assert isinstance(value, BaseModel)
parent = value
return parent, leaf
def runtime_config_payload(config: Config) -> dict[str, Any]:
return {
path: getattr(parent, leaf)
for path in RUNTIME_CONFIG_PATHS
for parent, leaf in [_parent(config, path)]
}
def update_runtime_config(
config: Config, values: dict[str, Any], *, local_browser: bool,
) -> bool:
"""Validate the complete patch before changing any persisted configuration."""
unknown = values.keys() - set(RUNTIME_CONFIG_PATHS)
if unknown:
raise WebUISettingsError("Unknown runtime setting: " + sorted(unknown)[0])
remote_install = "tools.webui_allow_remote_package_install"
if remote_install in values and not local_browser:
if values[remote_install] != config.tools.webui_allow_remote_package_install:
raise WebUISettingsError(
"Change remote installation access from a browser on the gateway host.",
status=403,
)
candidate = config.model_copy(deep=True)
for path, value in values.items():
parent, leaf = _parent(candidate, path)
if isinstance(value, float) and not math.isfinite(value):
raise WebUISettingsError(f"{path}: enter a finite number")
try:
field = type(parent).model_fields[leaf]
parsed = TypeAdapter(field.rebuild_annotation()).validate_python(value, strict=True)
except ValidationError as exc:
message = exc.errors(include_input=False, include_url=False)[0]["msg"]
raise WebUISettingsError(f"{path}: {message}") from None
setattr(parent, leaf, parsed)
try:
# Dump aliases so legacy field names and timezone/model validators are
# handled by the same schema as a normal configuration-file load.
validated = Config.model_validate(candidate.model_dump(by_alias=True), strict=True)
except ValidationError as exc:
errors = exc.errors(include_input=False, include_url=False)
error = errors[0]
location = ".".join(str(part) for part in error["loc"])
raise WebUISettingsError(f"{location}: {error['msg']}") from None
numeric_minima = {
"agents.defaults.max_tool_iterations": 1,
"agents.defaults.max_tool_result_chars": 1,
"gateway.port": 1,
}
for path, minimum in numeric_minima.items():
if path in values:
parent, leaf = _parent(validated, path)
if getattr(parent, leaf) < minimum:
raise WebUISettingsError(f"{path}: must be at least {minimum}")
if "gateway.port" in values and validated.gateway.port > 65535:
raise WebUISettingsError("gateway.port: must be at most 65535")
if "tools.exec.timeout" in values:
limit = validated.tools.exec.max_runtime_limit
if validated.tools.exec.max_runtime > limit:
raise WebUISettingsError(
f"tools.exec.timeout: enter 1–{limit} seconds, or 0 for no time limit"
)
for path in ("gateway.host", "tools.image_generation.save_dir"):
if path in values:
parent, leaf = _parent(validated, path)
if not str(getattr(parent, leaf)).strip():
raise WebUISettingsError(f"{path}: must not be empty")
if "tools.image_generation.save_dir" in values:
directory = validated.tools.image_generation.save_dir
if (PureWindowsPath(directory).drive or directory.startswith(("/", "\\"))
or ".." in PurePosixPath(directory.replace("\\", "/")).parts):
raise WebUISettingsError("tools.image_generation.save_dir: use a relative path inside the media directory")
if "tools.exec.sandbox" in values and validated.tools.exec.sandbox not in {"", "bwrap", "seatbelt"}:
raise WebUISettingsError("tools.exec.sandbox: choose no sandbox, bwrap, or seatbelt")
if {"tools.exec.sandbox_launcher", "tools.exec.sandbox_rw_binds"} & values.keys() and validated.tools.exec.sandbox_launcher:
from nanobot.security.sandbox_launcher import (
SandboxLauncherError,
resolve_sandbox_launcher,
)
try:
exec_config = resolve_config_env_vars(validated.tools.exec)
workspace = Path(resolve_config_env_vars(str(validated.workspace_path))).expanduser()
resolve_sandbox_launcher(
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:
for cidr in validated.tools.ssrf_whitelist:
try:
ipaddress.ip_network(cidr, strict=False)
except ValueError:
raise WebUISettingsError("tools.ssrf_whitelist: enter valid IP addresses or CIDR ranges") from None
for path in ("tools.exec.allow_patterns", "tools.exec.deny_patterns"):
if path in values:
patterns = cast(list[str], values[path])
for pattern in patterns:
try:
re.compile(pattern)
except re.error:
raise WebUISettingsError(f"{path}: invalid regular expression") from None
if values.get("tools.web.use_proxy") is True:
try:
validated.network.resolve_proxy(True)
except ValueError as exc:
raise WebUISettingsError(str(exc)) from None
before = runtime_config_payload(config)
after = runtime_config_payload(validated)
if before == after:
return False
config.agents = validated.agents
config.tools = validated.tools
config.network = validated.network
config.gateway = validated.gateway
return True