RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(security): authenticate sandbox executor API and isolate container network (#18703)
陈志谦fdf937177d14
diff --git a/agent/sandbox/.env.example b/agent/sandbox/.env.exampleindex e88ed561ce..98a357fc82 100644--- a/agent/sandbox/.env.example+++ b/agent/sandbox/.env.example@@ -7,3 +7,21 @@ SANDBOX_EXECUTOR_MANAGER_PORT=9385SANDBOX_ENABLE_SECCOMP=falseSANDBOX_MAX_MEMORY=256m # b, k, m, gSANDBOX_TIMEOUT=10s # s, m, 1m30s+# Shared secret required by the executor manager /run endpoint (Authorization:+# Bearer or X-Sandbox-Token). Authentication is fail-closed: with no token set,+# /run answers 503 instead of staying open. Generate one with, for example:+# python -c "import secrets; print(secrets.token_urlsafe(32))"+SANDBOX_EXECUTOR_MANAGER_API_TOKEN=+# Explicit, risk-accepting opt-in for deployments that cannot provide a token.+# Leave unset (or false) in normal deployments.+SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=false+# Docker network used for sandbox runner containers. "none" (default) gives+# sandboxed code no external network access. Set to "bridge" only if your+# sandboxed code genuinely needs outbound network (e.g. pip/npm installs at+# runtime); prefer baking dependencies into the base images instead.+SANDBOX_CONTAINER_NETWORK=none+# Rate limits for POST /run, keyed by client address: a small pre-auth+# throttle applied to ALL /run traffic (including invalid-token requests) and+# the larger quota for authenticated execution.+SANDBOX_RUN_PREAUTH_RATE_LIMIT=30/minute+SANDBOX_RUN_RATE_LIMIT=120/minutediff --git a/agent/sandbox/README.md b/agent/sandbox/README.mdindex 409341d990..78375907dc 100644--- a/agent/sandbox/README.md+++ b/agent/sandbox/README.md@@ -174,6 +174,29 @@ For users who need **zero-trust-level syscall control**, we support an additionaIn addition to sandboxing, Python code is **statically analyzed via AST (Abstract Syntax Tree)** before execution. Potentially malicious code (e.g. file operations, subprocess calls, etc.) is rejected early, providing an extra layer of protection.+### 🌐 Network Isolation (Default: No External Access)++Sandbox runner containers are started with `--network none`: sandboxed code has **no external network access** by default. `docker exec` (used to feed code in and collect results) is unaffected.++If your sandboxed code genuinely needs outbound network access (e.g. runtime `pip install` / `npm install`), opt in explicitly via `.env`:++```dotenv+SANDBOX_CONTAINER_NETWORK=bridge+```++Prefer baking dependencies into the base images (`sandbox_base_image/python/requirements.txt`, `sandbox_base_image/nodejs/package.json`) instead — that keeps the default hermetic profile.++### 🔑 Executor API Authentication (Shared Secret)++The `/run` endpoint can require a shared secret. Set the same value on both sides:++```dotenv+# executor manager (and RAGFlow when using the compose profile)+SANDBOX_EXECUTOR_MANAGER_API_TOKEN=<openssl rand -hex 32>+```++Requests must then carry `Authorization: Bearer <token>` (or `X-Sandbox-Token: <token>`); the RAGFlow self-managed provider sends this automatically. When the variable is unset the endpoint accepts unauthenticated requests (the pre-token behavior), and the executor manager logs a prominent security warning at startup and on first request. `/run` is additionally rate limited per client address (`SANDBOX_RUN_RATE_LIMIT`, default `120/minute`).+---This security model strikes a balance between **robust isolation** and **developer usability**. While `seccomp` can be highly restrictive, our default setup aims to keep things usable for most developers — no obscure crashes or cryptic setup required.@@ -253,6 +276,8 @@ function main({arg1, arg2}) {Async funcion with aioxs+> Note: outbound HTTP requires opting into network access (`SANDBOX_CONTAINER_NETWORK=bridge`); by default sandbox containers have no external network.+```javascriptconst axios = require('axios');async function main() {diff --git a/agent/sandbox/docker-compose.yml b/agent/sandbox/docker-compose.ymlindex d0e5777818..7bb39de55f 100644--- a/agent/sandbox/docker-compose.yml+++ b/agent/sandbox/docker-compose.yml@@ -6,8 +6,9 @@ services:image: sandbox-executor-manager:latestruntime: runcprivileged: true+ # Bind to loopback only: the /run endpoint executes arbitrary sandboxed code.ports:- - "${SANDBOX_EXECUTOR_MANAGER_PORT:-9385}:9385"+ - "127.0.0.1:${SANDBOX_EXECUTOR_MANAGER_PORT:-9385}:9385"volumes:- /var/run/docker.sock:/var/run/docker.socknetworks:@@ -22,6 +23,19 @@ services:- SANDBOX_ENABLE_SECCOMP=${SANDBOX_ENABLE_SECCOMP:-false}- SANDBOX_MAX_MEMORY=${SANDBOX_MAX_MEMORY:-256m} # b, k, m, g- SANDBOX_TIMEOUT=${SANDBOX_TIMEOUT:-10s} # s, m, 1m30s+ # Shared secret for the /run API. Authentication is fail-closed: with+ # no token the API refuses /run (503) unless the explicit opt-in flag+ # below is set.+ - SANDBOX_EXECUTOR_MANAGER_API_TOKEN=${SANDBOX_EXECUTOR_MANAGER_API_TOKEN:-}+ # Explicit, risk-accepting opt-in for deployments that cannot provide a+ # token. Leave unset/false in normal deployments.+ - SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=${SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED:-false}+ # Sandbox runner containers get no external network by default.+ - SANDBOX_CONTAINER_NETWORK=${SANDBOX_CONTAINER_NETWORK:-none}+ # Rate limits: a small pre-auth throttle applied to all /run traffic,+ # and the larger per-address quota for authenticated execution.+ - SANDBOX_RUN_PREAUTH_RATE_LIMIT=${SANDBOX_RUN_PREAUTH_RATE_LIMIT:-30/minute}+ - SANDBOX_RUN_RATE_LIMIT=${SANDBOX_RUN_RATE_LIMIT:-120/minute}healthcheck:test: ["CMD-SHELL", "curl --fail http://localhost:9385/healthz || exit 1"]interval: 10sdiff --git a/agent/sandbox/executor_manager/api/routes.py b/agent/sandbox/executor_manager/api/routes.pyindex b0ffe908b2..0c426c049e 100644--- a/agent/sandbox/executor_manager/api/routes.py+++ b/agent/sandbox/executor_manager/api/routes.py@@ -13,7 +13,10 @@# See the License for the specific language governing permissions and# limitations under the License.#-from fastapi import APIRouter+from fastapi import APIRouter, Depends+from services.auth import require_api_token+from services.limiter import RUN_RATE_LIMIT, limiter+from services.preauth import preauth_rate_limitfrom api.handlers import healthz_handler, run_code_handler@@ -21,4 +24,8 @@ router = APIRouter()router.get("/")(healthz_handler)router.get("/healthz")(healthz_handler)-router.post("/run")(run_code_handler)+# Execution endpoints throttle ALL traffic per client address before+# authentication (so invalid-token floods eventually receive 429), then+# require the shared-secret token, then apply the larger authenticated+# execution quota. Health probes stay unauthenticated.+router.post("/run", dependencies=[Depends(preauth_rate_limit), Depends(require_api_token)])(limiter.limit(RUN_RATE_LIMIT)(run_code_handler))diff --git a/agent/sandbox/executor_manager/core/config.py b/agent/sandbox/executor_manager/core/config.pyindex 332fdefbeb..6e9989dc08 100644--- a/agent/sandbox/executor_manager/core/config.py+++ b/agent/sandbox/executor_manager/core/config.py@@ -17,6 +17,7 @@ import osfrom contextlib import asynccontextmanagerfrom fastapi import FastAPI+from services.auth import log_authentication_startup_statefrom util import format_timeout_duration, parse_timeout_durationfrom core.container import init_containers, teardown_containers@@ -40,4 +41,5 @@ async def _lifespan(app: FastAPI):def init():logger.info(f"Global timeout: {format_timeout_duration(TIMEOUT)}")+ log_authentication_startup_state()return _lifespandiff --git a/agent/sandbox/executor_manager/core/container.py b/agent/sandbox/executor_manager/core/container.pyindex 36cdded28f..4093b262b6 100644--- a/agent/sandbox/executor_manager/core/container.py+++ b/agent/sandbox/executor_manager/core/container.py@@ -98,6 +98,11 @@ async def create_container(name: str, language: SupportLanguage) -> bool:"--workdir","/workspace",]+ # Network isolation by default: sandboxed code gets no external network+ # access unless a deployment explicitly opts in (e.g. needing pip/npm+ # installs or outbound requests) via SANDBOX_CONTAINER_NETWORK.+ container_network = (os.getenv("SANDBOX_CONTAINER_NETWORK") or "").strip() or "none"+ create_args.extend(["--network", container_network])if os.getenv("SANDBOX_MAX_MEMORY"):memory_limit = os.getenv("SANDBOX_MAX_MEMORY") or "256m"if is_valid_memory_limit(memory_limit):@@ -136,7 +141,7 @@ async def create_container(name: str, language: SupportLanguage) -> bool:return await container_is_running(name)except Exception as e:- logger.error(f"❌ Container creation exception {name}: {str(e)}")+ logger.error(f"❌ Container creation exception {name}: {e!s}")return False@@ -148,7 +153,7 @@ async def recreate_container(name: str, language: SupportLanguage) -> bool:return await create_container(name, language)except Exception as e:- logger.error(f"❌ Container {name} recreation failed: {str(e)}")+ logger.error(f"❌ Container {name} recreation failed: {e!s}")return Falsediff --git a/agent/sandbox/executor_manager/pytest.ini b/agent/sandbox/executor_manager/pytest.ininew file mode 100644index 0000000000..b93bddf2d6--- /dev/null+++ b/agent/sandbox/executor_manager/pytest.ini@@ -0,0 +1,6 @@+[pytest]+# Unit tests for the executor manager. They use top-level imports resolved+# against this directory (mirroring WORKDIR /app in the Docker image), so the+# rootdir must stay here and not pick up the repository-level pytest config.+python_files = test_*.py+testpaths = testsdiff --git a/agent/sandbox/executor_manager/services/auth.py b/agent/sandbox/executor_manager/services/auth.pynew file mode 100644index 0000000000..64ff506624--- /dev/null+++ b/agent/sandbox/executor_manager/services/auth.py@@ -0,0 +1,128 @@+#+# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#+import os+import secrets++from core.logger import logger+from fastapi import HTTPException, Request, status++API_TOKEN_ENV_VAR = "SANDBOX_EXECUTOR_MANAGER_API_TOKEN"+ALLOW_UNAUTHENTICATED_ENV_VAR = "SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED"++_TRUTHY_FLAG_VALUES = {"1", "true", "yes", "on"}++_UNAUTH_OPT_IN_HINT = (+ "Set SANDBOX_EXECUTOR_MANAGER_API_TOKEN to a strong shared secret "+ "(and configure RAGFlow with the same value), or explicitly accept the risk by "+ "setting SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=true."+)++_UNAUTHENTICATED_WARNING = (+ "********************************************************************************\n"+ "\u26a0\ufe0f SECURITY WARNING: SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=true and\n"+ " no API token is configured. The executor manager accepts unauthenticated\n"+ " requests on /run: anyone who can reach this endpoint can execute code in\n"+ " the sandbox pool. Remove the flag and set SANDBOX_EXECUTOR_MANAGER_API_TOKEN\n"+ " to a strong shared secret.\n"+ "********************************************************************************"+)++_no_token_or_opt_in_warned = False+++def get_configured_api_token() -> str:+ """Return the configured shared-secret API token, or an empty string."""+ return (os.getenv(API_TOKEN_ENV_VAR) or "").strip()+++def unauthenticated_access_explicitly_allowed() -> bool:+ """Whether the operator explicitly opted into unauthenticated /run.++ Authentication is fail-closed by default: when no API token is configured,+ /run refuses to serve instead of silently staying open. Deployments that+ genuinely cannot provide a token must set+ SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=true, making the insecure+ state an explicit operator decision rather than the default.+ """+ return (os.getenv(ALLOW_UNAUTHENTICATED_ENV_VAR) or "").strip().lower() in _TRUTHY_FLAG_VALUES+++def warn_if_unauthenticated_opt_in() -> None:+ """Log a prominent warning (once) while the explicit insecure opt-in is active."""+ global _no_token_or_opt_in_warned+ if _no_token_or_opt_in_warned:+ return+ _no_token_or_opt_in_warned = True+ logger.warning(_UNAUTHENTICATED_WARNING)+++def log_authentication_startup_state() -> None:+ """Log the effective /run authentication posture once at startup.++ Fail-closed with no token (503 for every /run request), the explicit+ insecure opt-in, or token-enforced authentication.+ """+ if get_configured_api_token():+ logger.info("Sandbox executor /run authentication: shared-secret token configured")+ return+ if unauthenticated_access_explicitly_allowed():+ warn_if_unauthenticated_opt_in()+ return+ logger.error(+ "SANDBOX_EXECUTOR_MANAGER_API_TOKEN is not set: /run will refuse requests with 503. %s",+ _UNAUTH_OPT_IN_HINT,+ )+++def _extract_request_token(request: Request) -> str:+ """Extract the caller-supplied token from supported headers.++ Supported formats:+ - Authorization: Bearer <token>+ - X-Sandbox-Token: <token>+ """+ authorization = request.headers.get("Authorization", "")+ if authorization[:7].lower() == "bearer ":+ return authorization[7:].strip()+ return (request.headers.get("X-Sandbox-Token") or "").strip()+++async def require_api_token(request: Request) -> None:+ """FastAPI dependency enforcing shared-secret authentication.++ Fail-closed by default (CWE-306): when SANDBOX_EXECUTOR_MANAGER_API_TOKEN+ is unset or blank, /run refuses requests with 503 instead of staying open.+ Legacy open behaviour survives only through the explicit operator opt-in+ SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=true.++ When a token is configured, every request must present the same secret via+ `Authorization: Bearer <token>` or `X-Sandbox-Token`. Comparison uses+ secrets.compare_digest to avoid timing attacks.+ """+ configured_token = get_configured_api_token()+ if not configured_token:+ if unauthenticated_access_explicitly_allowed():+ warn_if_unauthenticated_opt_in()+ return+ raise HTTPException(+ status_code=status.HTTP_503_SERVICE_UNAVAILABLE,+ detail=f"Sandbox executor authentication is not configured; refusing unauthenticated /run. {_UNAUTH_OPT_IN_HINT}",+ )++ provided_token = _extract_request_token(request)+ if not provided_token or not secrets.compare_digest(provided_token, configured_token):+ logger.warning("\U0001f6ab Rejected unauthenticated %s request from %s", request.url.path, request.client.host if request.client else "unknown")+ raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing or invalid sandbox executor API token")diff --git a/agent/sandbox/executor_manager/services/limiter.py b/agent/sandbox/executor_manager/services/limiter.pyindex cdaffbd43f..8b380abf1f 100644--- a/agent/sandbox/executor_manager/services/limiter.py+++ b/agent/sandbox/executor_manager/services/limiter.py@@ -13,6 +13,8 @@# See the License for the specific language governing permissions and# limitations under the License.#+import os+from fastapi import Requestfrom fastapi.responses import JSONResponsefrom models.enums import ResultStatus@@ -21,12 +23,18 @@ from slowapi import Limiterfrom slowapi.errors import RateLimitExceededfrom slowapi.util import get_remote_address+# Rate limit applied to POST /run, keyed by remote address (see key_func above).+# Callers behind a Docker bridge network share a single address, so this acts as+# a coarse per-deployment throttle; tune via SANDBOX_RUN_RATE_LIMIT if needed.+RUN_RATE_LIMIT = os.getenv("SANDBOX_RUN_RATE_LIMIT", "120/minute")+limiter = Limiter(key_func=get_remote_address)async def rate_limit_exceeded_handler(request: Request, exc: Exception) -> JSONResponse:if isinstance(exc, RateLimitExceeded):return JSONResponse(+ status_code=429,content=CodeExecutionResult(status=ResultStatus.PROGRAM_RUNNER_ERROR,stdout="",diff --git a/agent/sandbox/executor_manager/services/preauth.py b/agent/sandbox/executor_manager/services/preauth.pynew file mode 100644index 0000000000..0cf2988d70--- /dev/null+++ b/agent/sandbox/executor_manager/services/preauth.py@@ -0,0 +1,110 @@+#+# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#+"""Pre-authentication rate limiting for /run.++Authentication dependencies run before the SlowAPI-decorated handler, so+without this layer invalid-token traffic never touches the execution rate+limit: a reachable executor manager could be made to perform unbounded+authentication work (compare_digest plus a warning log per attempt) without+ever receiving a 429. This module applies a small per-address sliding-window+limit to ALL /run traffic, before authentication, so repeated unauthenticated+requests are eventually throttled. Authenticated execution keeps the larger+SlowAPI quota as a second layer.+"""++import os+import threading+import time+from collections import deque++from core.logger import logger+from fastapi import Request+from limits import parse_many+from slowapi.errors import RateLimitExceeded++PRE_AUTH_RATE_LIMIT = os.getenv("SANDBOX_RUN_PREAUTH_RATE_LIMIT", "30/minute")+++class _LimitDescriptor:+ """Minimal stand-in for slowapi's Limit that RateLimitExceeded accepts."""++ def __init__(self, limit):+ self.limit = limit+ self.error_message = "Too many requests, please try again later"+++def _parse_rate_limit(value: str):+ items = list(parse_many(value))+ if len(items) != 1:+ raise ValueError(f"SANDBOX_RUN_PREAUTH_RATE_LIMIT must be a single rate like '30/minute', got {value!r}")+ return items[0]+++class PreAuthRateLimiter:+ """Thread-safe sliding-window limiter keyed by client address."""++ def __init__(self, limit):+ self._limit = limit+ self._window_seconds = limit.get_expiry()+ self._max_amount = limit.amount+ self._hits: dict[str, deque] = {}+ # Bounded rejection logging: at most one warning per address per+ # window, so throttling is observable without unbounded log writes.+ self._last_logged: dict[str, float] = {}+ self._lock = threading.Lock()++ def hit(self, address: str, now: float | None = None) -> None:+ """Record one hit for `address`; raise RateLimitExceeded when over budget."""+ timestamp = time.monotonic() if now is None else now+ with self._lock:+ window = self._hits.get(address)+ if window is None:+ window = deque()+ self._hits[address] = window+ cutoff = timestamp - self._window_seconds+ while window and window[0] <= cutoff:+ window.popleft()+ if len(window) >= self._max_amount:+ if timestamp - self._last_logged.get(address, float("-inf")) >= self._window_seconds:+ self._last_logged[address] = timestamp+ logger.warning(+ "Pre-auth rate limit exceeded for %s on /run (%s); further rejections from this address are throttled silently for this window",+ address,+ self._limit,+ )+ raise RateLimitExceeded(_LimitDescriptor(self._limit))+ window.append(timestamp)+ # Opportunistic cleanup keeps the address maps bounded under scans.+ if len(self._hits) > 10_000:+ stale = [key for key, hits in self._hits.items() if not hits or hits[-1] <= cutoff]+ for key in stale:+ del self._hits[key]+ self._last_logged.pop(key, None)++ def reset(self) -> None:+ with self._lock:+ self._hits.clear()+ self._last_logged.clear()+++_preauth_limit_item = _parse_rate_limit(PRE_AUTH_RATE_LIMIT)+preauth_limiter = PreAuthRateLimiter(_preauth_limit_item)+++def preauth_rate_limit(request: Request) -> None:+ """FastAPI dependency: throttle /run traffic before authentication."""+ address = request.client.host if request.client else "unknown"+ preauth_limiter.hit(address)diff --git a/agent/sandbox/executor_manager/tests/conftest.py b/agent/sandbox/executor_manager/tests/conftest.pynew file mode 100644index 0000000000..9858e3a1b7--- /dev/null+++ b/agent/sandbox/executor_manager/tests/conftest.py@@ -0,0 +1,51 @@+#+# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#+import os+import sys+from pathlib import Path++# executor_manager modules use top-level imports (e.g. `from api.handlers+# import ...`) resolved against the executor_manager directory (WORKDIR /app+# in Docker). Make that directory importable regardless of the pytest rootdir.+EXECUTOR_MANAGER_ROOT = Path(__file__).resolve().parent.parent+sys.path.insert(0, str(EXECUTOR_MANAGER_ROOT))++# Keep tests hermetic: a small window makes the 429 case cheap to exercise.+# Must be set before `services.limiter` / `services.preauth` are first imported+# (module-level env reads). The pre-auth budget stays generous by default so+# ordinary auth tests never trip it; the dedicated pre-auth tests shrink the+# limiter explicitly.+os.environ["SANDBOX_RUN_RATE_LIMIT"] = os.environ.get("SANDBOX_TEST_RATE_LIMIT_OVERRIDE", "3/minute")+os.environ["SANDBOX_RUN_PREAUTH_RATE_LIMIT"] = os.environ.get("SANDBOX_TEST_PREAUTH_RATE_LIMIT_OVERRIDE", "1000/minute")++import pytest # noqa: E402 (env vars above must be set before these imports)+from services.limiter import limiter # noqa: E402+from services.preauth import preauth_limiter # noqa: E402++[email protected](autouse=True)+def _reset_rate_limiters():+ limiter.reset()+ preauth_limiter.reset()+ yield+ preauth_limiter.reset()+ limiter.reset()++[email protected](autouse=True)+def _clean_token_env(monkeypatch):+ monkeypatch.delenv("SANDBOX_EXECUTOR_MANAGER_API_TOKEN", raising=False)+ yielddiff --git a/agent/sandbox/executor_manager/tests/test_run_endpoint_security.py b/agent/sandbox/executor_manager/tests/test_run_endpoint_security.pynew file mode 100644index 0000000000..d924f7ca84--- /dev/null+++ b/agent/sandbox/executor_manager/tests/test_run_endpoint_security.py@@ -0,0 +1,241 @@+#+# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#+"""Security tests for the executor manager /run endpoint.++Run from the executor_manager directory:++ cd agent/sandbox/executor_manager+ python -m pytest tests/++Covers: fail-closed shared-secret authentication (503 when no token is+configured unless the explicit unauthenticated opt-in flag is set, 401 with a+wrong token, success via Authorization: Bearer and X-Sandbox-Token), the+pre-auth rate limit applied to all /run traffic, the authenticated execution+quota, the standalone Compose environment contract, and the docker network+flag used for sandbox runner containers.+"""++import asyncio+import base64+from pathlib import Path++import pytest+from fastapi.testclient import TestClient+from models.enums import ResultStatus, SupportLanguage+from models.schemas import CodeExecutionResult++API_TOKEN_ENV = "SANDBOX_EXECUTOR_MANAGER_API_TOKEN"+ALLOW_UNAUTH_ENV = "SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED"+TEST_TOKEN = "unit-test-shared-secret"+++def _payload() -> dict:+ code = "def main():\n return 42\n"+ return {"code_b64": base64.b64encode(code.encode("utf-8")).decode("utf-8"), "language": "python", "arguments": {}}+++def client(monkeypatch):+ async def fake_execute_code(req):+ return CodeExecutionResult(status=ResultStatus.SUCCESS, stdout="42", stderr="", exit_code=0)++ monkeypatch.setattr("api.handlers.execute_code", fake_execute_code)++ import core.container as container_module++ # Normally populated by the lifespan hook (which provisions real docker+ # containers); handlers uses the same dict object by reference.+ container_module._CONTAINER_EXECUTION_SEMAPHORES[SupportLanguage.PYTHON] = asyncio.Semaphore(1)+ container_module._CONTAINER_EXECUTION_SEMAPHORES[SupportLanguage.NODEJS] = asyncio.Semaphore(1)++ import main++ # Instantiated without a context manager on purpose: the lifespan hook+ # provisions real docker containers, which these unit tests must avoid.+ return TestClient(main.app)+++class TestRunEndpointAuth:+ def test_missing_token_returns_401_when_configured(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ response = client.post("/run", json=_payload())+ assert response.status_code == 401++ def test_wrong_bearer_token_returns_401(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ response = client.post("/run", json=_payload(), headers={"Authorization": "Bearer not-the-token"})+ assert response.status_code == 401++ def test_wrong_x_sandbox_token_returns_401(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ response = client.post("/run", json=_payload(), headers={"X-Sandbox-Token": "also-wrong"})+ assert response.status_code == 401++ def test_correct_bearer_token_is_accepted(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ response = client.post("/run", json=_payload(), headers={"Authorization": f"Bearer {TEST_TOKEN}"})+ assert response.status_code == 200+ assert response.json()["status"] == ResultStatus.SUCCESS++ def test_correct_x_sandbox_token_is_accepted(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ response = client.post("/run", json=_payload(), headers={"X-Sandbox-Token": TEST_TOKEN})+ assert response.status_code == 200++ def test_unset_token_fails_closed_with_503(self, client, monkeypatch):+ monkeypatch.delenv(API_TOKEN_ENV, raising=False)+ monkeypatch.delenv(ALLOW_UNAUTH_ENV, raising=False)+ response = client.post("/run", json=_payload())+ assert response.status_code == 503+ assert "SANDBOX_EXECUTOR_MANAGER_API_TOKEN" in response.json()["detail"]++ def test_blank_token_fails_closed_with_503(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, " ")+ monkeypatch.delenv(ALLOW_UNAUTH_ENV, raising=False)+ assert client.post("/run", json=_payload()).status_code == 503++ def test_explicit_opt_in_restores_open_endpoint(self, client, monkeypatch):+ monkeypatch.delenv(API_TOKEN_ENV, raising=False)+ monkeypatch.setenv(ALLOW_UNAUTH_ENV, "true")+ response = client.post("/run", json=_payload())+ assert response.status_code == 200++ @pytest.mark.parametrize("flag", ["1", "TRUE", "Yes", "on "])+ def test_opt_in_accepts_truthy_flag_spellings(self, client, monkeypatch, flag):+ monkeypatch.delenv(API_TOKEN_ENV, raising=False)+ monkeypatch.setenv(ALLOW_UNAUTH_ENV, flag)+ assert client.post("/run", json=_payload()).status_code == 200++ @pytest.mark.parametrize("flag", ["false", "0", "no", "unexpected"])+ def test_opt_in_rejects_non_truthy_flag_spellings(self, client, monkeypatch, flag):+ monkeypatch.delenv(API_TOKEN_ENV, raising=False)+ monkeypatch.setenv(ALLOW_UNAUTH_ENV, flag)+ assert client.post("/run", json=_payload()).status_code == 503++ def test_opt_in_is_ignored_when_a_token_is_configured(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ monkeypatch.setenv(ALLOW_UNAUTH_ENV, "true")+ # The flag never weakens a configured token: bad credentials still 401.+ assert client.post("/run", json=_payload(), headers={"Authorization": "Bearer wrong"}).status_code == 401++ def test_healthz_does_not_require_token(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ assert client.get("/healthz").status_code == 200+ assert client.get("/").status_code == 200+++class TestRunEndpointRateLimit:+ def test_requests_beyond_limit_get_429(self, client, monkeypatch):+ # conftest pins SANDBOX_RUN_RATE_LIMIT to 3/minute.+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ headers = {"Authorization": f"Bearer {TEST_TOKEN}"}+ for _ in range(3):+ assert client.post("/run", json=_payload(), headers=headers).status_code == 200+ response = client.post("/run", json=_payload(), headers=headers)+ assert response.status_code == 429+ body = response.json()+ assert body["exit_code"] == -429++ def test_repeated_unauthenticated_requests_eventually_get_429(self, client, monkeypatch):+ # The pre-auth limiter throttles ALL /run traffic before authentication,+ # so an invalid-token flood cannot generate unbounded authentication+ # work. The limiter is shrunk to 3/minute for this test (conftest+ # resets it afterwards).+ import services.preauth as preauth_module++ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ monkeypatch.setattr(+ preauth_module,+ "preauth_limiter",+ preauth_module.PreAuthRateLimiter(preauth_module._parse_rate_limit("3/minute")),+ )+ for _ in range(3):+ assert client.post("/run", json=_payload()).status_code == 401+ response = client.post("/run", json=_payload())+ assert response.status_code == 429+ body = response.json()+ assert body["exit_code"] == -429++ def test_single_auth_failures_still_return_401_under_generous_preauth(self, client, monkeypatch):+ monkeypatch.setenv(API_TOKEN_ENV, TEST_TOKEN)+ assert client.post("/run", json=_payload()).status_code == 401+ assert client.post("/run", json=_payload(), headers={"X-Sandbox-Token": "wrong"}).status_code == 401+++class TestContainerNetworkIsolation:+ @staticmethod+ def _captured_docker_run_argv(monkeypatch) -> list[list[str]]:+ calls: list[list[str]] = []++ async def fake_run_command(*args, timeout=5):+ calls.append(list(args))+ return 0, "true", ""++ import core.container as container_module++ monkeypatch.setattr(container_module, "async_run_command", fake_run_command)+ assert asyncio.run(container_module.create_container("sandbox_python_0", SupportLanguage.PYTHON)) is True+ return next(argv for argv in calls if argv[:3] == ["docker", "run", "-d"])++ def test_container_defaults_to_network_none(self, monkeypatch):+ monkeypatch.delenv("SANDBOX_CONTAINER_NETWORK", raising=False)+ argv = self._captured_docker_run_argv(monkeypatch)+ assert argv[argv.index("--network") + 1] == "none"++ def test_blank_network_env_falls_back_to_none(self, monkeypatch):+ monkeypatch.setenv("SANDBOX_CONTAINER_NETWORK", " ")+ argv = self._captured_docker_run_argv(monkeypatch)+ assert argv[argv.index("--network") + 1] == "none"++ def test_network_env_override_is_passed_through(self, monkeypatch):+ monkeypatch.setenv("SANDBOX_CONTAINER_NETWORK", "bridge")+ argv = self._captured_docker_run_argv(monkeypatch)+ assert argv[argv.index("--network") + 1] == "bridge"++ def test_other_hardening_flags_still_present(self, monkeypatch):+ monkeypatch.delenv("SANDBOX_CONTAINER_NETWORK", raising=False)+ argv = self._captured_docker_run_argv(monkeypatch)+ assert "--runtime=runsc" in argv+ assert "--read-only" in argv+ assert argv[argv.index("--user") + 1] == "nobody"+++class TestStandaloneComposeEnvironment:+ """Regression: the standalone Compose file must actually deliver the+ documented environment contract (SANDBOX_RUN_RATE_LIMIT, the pre-auth+ limit, and the explicit unauthenticated opt-in flag) into the container."""++ COMPOSE_PATH = Path(__file__).resolve().parents[2] / "docker-compose.yml"+ ENV_EXAMPLE_PATH = Path(__file__).resolve().parents[2] / ".env.example"++ def test_documented_rate_limit_reaches_the_container(self):+ compose = self.COMPOSE_PATH.read_text(encoding="utf-8")+ assert "- SANDBOX_RUN_RATE_LIMIT=${SANDBOX_RUN_RATE_LIMIT:-120/minute}" in compose+ assert "- SANDBOX_RUN_PREAUTH_RATE_LIMIT=${SANDBOX_RUN_PREAUTH_RATE_LIMIT:-30/minute}" in compose++ def test_unauthenticated_opt_in_flag_is_forwarded(self):+ compose = self.COMPOSE_PATH.read_text(encoding="utf-8")+ assert "- SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=${SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED:-false}" in compose++ def test_env_example_matches_compose_defaults(self):+ example = self.ENV_EXAMPLE_PATH.read_text(encoding="utf-8")+ for name in (+ "SANDBOX_RUN_RATE_LIMIT",+ "SANDBOX_RUN_PREAUTH_RATE_LIMIT",+ "SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED",+ "SANDBOX_EXECUTOR_MANAGER_API_TOKEN",+ ):+ assert name in example, f"{name} missing from .env.example"diff --git a/agent/sandbox/providers/self_managed.py b/agent/sandbox/providers/self_managed.pyindex ceb16cca93..296e21757f 100644--- a/agent/sandbox/providers/self_managed.py+++ b/agent/sandbox/providers/self_managed.py@@ -25,11 +25,11 @@ import base64import osimport timeimport uuid-from typing import Dict, Any, List, Optional+from typing import Anyimport requests-from .base import SandboxProvider, SandboxInstance, ExecutionResult+from .base import ExecutionResult, SandboxInstance, SandboxProviderclass SelfManagedProvider(SandboxProvider):@@ -45,9 +45,10 @@ class SelfManagedProvider(SandboxProvider):self.timeout: int = 30self.max_retries: int = 3self.pool_size: int = 3+ self.api_token: str = ""self._initialized: bool = False- def initialize(self, config: Dict[str, Any]) -> bool:+ def initialize(self, config: dict[str, Any]) -> bool:"""Initialize the provider with configuration.@@ -57,6 +58,8 @@ class SelfManagedProvider(SandboxProvider):- timeout: Request timeout in seconds (default: 30)- max_retries: Maximum retry attempts (default: 3)- pool_size: Container pool size for info (default: 3)+ - api_token: Shared secret for the executor manager API+ (falls back to SANDBOX_EXECUTOR_MANAGER_API_TOKEN env var)Returns:True if initialization successful, False otherwise@@ -65,6 +68,9 @@ class SelfManagedProvider(SandboxProvider):self.timeout = config.get("timeout", 30)self.max_retries = config.get("max_retries", 3)self.pool_size = config.get("executor_manager_pool_size", config.get("pool_size", 3))+ # Shared-secret token authenticating RAGFlow towards the executor+ # manager. Explicit config wins over the environment variable.+ self.api_token = str(config.get("api_token") or os.getenv("SANDBOX_EXECUTOR_MANAGER_API_TOKEN", "") or "").strip()# Validate endpoint is accessibleif not self.health_check():@@ -111,7 +117,7 @@ class SelfManagedProvider(SandboxProvider):},)- def execute_code(self, instance_id: str, code: str, language: str, timeout: int = 10, arguments: Optional[Dict[str, Any]] = None) -> ExecutionResult:+ def execute_code(self, instance_id: str, code: str, language: str, timeout: int = 10, arguments: dict[str, Any] | None = None) -> ExecutionResult:"""Execute code in the sandbox.@@ -142,10 +148,14 @@ class SelfManagedProvider(SandboxProvider):url = f"{self.endpoint}/run"exec_timeout = timeout or self.timeout+ headers = {"Content-Type": "application/json"}+ if self.api_token:+ headers["Authorization"] = f"Bearer {self.api_token}"+start_time = time.time()try:- response = requests.post(url, json=payload, timeout=exec_timeout, headers={"Content-Type": "application/json"})+ response = requests.post(url, json=payload, timeout=exec_timeout, headers=headers)execution_time = time.time() - start_time@@ -178,7 +188,7 @@ class SelfManagedProvider(SandboxProvider):raise TimeoutError(f"Execution timed out after {exec_timeout} seconds")except requests.RequestException as e:- raise RuntimeError(f"HTTP request failed: {str(e)}")+ raise RuntimeError(f"HTTP request failed: {e!s}")def destroy_instance(self, instance_id: str) -> bool:"""@@ -212,7 +222,7 @@ class SelfManagedProvider(SandboxProvider):except Exception:return False- def get_supported_languages(self) -> List[str]:+ def get_supported_languages(self) -> list[str]:"""Get list of supported programming languages.@@ -222,7 +232,7 @@ class SelfManagedProvider(SandboxProvider):return ["python", "nodejs", "javascript"]@staticmethod- def get_config_schema() -> Dict[str, Dict]:+ def get_config_schema() -> dict[str, dict]:"""Return configuration schema for self-managed provider.@@ -240,6 +250,17 @@ class SelfManagedProvider(SandboxProvider):"scope": "runtime","readonly": False,},+ "api_token": {+ "type": "string",+ "required": False,+ "label": "Executor Manager API Token",+ "secret": True,+ "placeholder": "Optional; defaults to SANDBOX_EXECUTOR_MANAGER_API_TOKEN",+ "default": "",+ "description": "Shared secret authenticating RAGFlow to sandbox-executor-manager. Must match SANDBOX_EXECUTOR_MANAGER_API_TOKEN on the executor-manager side.",+ "scope": "runtime",+ "readonly": False,+ },"timeout": {"type": "integer","required": False,@@ -318,6 +339,15 @@ class SelfManagedProvider(SandboxProvider):"scope": "deployment","readonly": True,},+ "container_network": {+ "type": "string",+ "required": False,+ "label": "Container Network",+ "default": os.getenv("SANDBOX_CONTAINER_NETWORK", "none"),+ "description": "Docker network attached to sandbox containers. Defaults to 'none' (no external network access); set to 'bridge' only if sandboxed code needs outbound network.",+ "scope": "deployment",+ "readonly": True,+ },"sandbox_timeout": {"type": "string","required": False,@@ -350,7 +380,7 @@ class SelfManagedProvider(SandboxProvider):else:return language- def validate_config(self, config: dict) -> tuple[bool, Optional[str]]:+ def validate_config(self, config: dict) -> tuple[bool, str | None]:"""Validate self-managed provider configuration.diff --git a/agent/sandbox/sandbox_spec.md b/agent/sandbox/sandbox_spec.mdindex afb559a9c9..9b35ac252e 100644--- a/agent/sandbox/sandbox_spec.md+++ b/agent/sandbox/sandbox_spec.md@@ -138,6 +138,7 @@ Wraps the existing executor_manager implementation. The implementation file is l- `endpoint`: HTTP endpoint (default: "http://sandbox-executor-manager:9385")- `timeout`: Request timeout in seconds (default: 30)- `max_retries`: Maximum retry attempts (default: 3)+- `api_token`: Shared secret for the executor manager API (falls back to the `SANDBOX_EXECUTOR_MANAGER_API_TOKEN` environment variable)The container pool is **not** sized from this configuration. sandbox-executor-manager reads`SANDBOX_EXECUTOR_MANAGER_POOL_SIZE` from its environment once at startup@@ -146,6 +147,12 @@ change that requires restarting that service. A `pool_size` key passed to the prfor reporting only — `get_info()` echoes it back — and reloading `sandbox.self_managed` recreatesthe RAGFlow-side client without resizing anything.+**Executor manager environment variables**:++- `SANDBOX_EXECUTOR_MANAGER_API_TOKEN`: Shared secret required on the `/run` endpoint (sent as `Authorization: Bearer <token>` or `X-Sandbox-Token: <token>`, compared with `secrets.compare_digest`). When unset, `/run` refuses requests with HTTP 503 instead of staying open; the legacy open behaviour survives only through the explicit operator opt-in `SANDBOX_EXECUTOR_MANAGER_ALLOW_UNAUTHENTICATED=true`. The RAGFlow side must be configured with the same value.+- `SANDBOX_CONTAINER_NETWORK`: Docker network attached to sandbox runner containers (default: `none`, i.e. no external network access). Set to `bridge` (or a custom network) only when sandboxed code genuinely needs outbound network access; prefer baking dependencies into the base images instead.+- `SANDBOX_RUN_RATE_LIMIT`: Rate limit for `POST /run`, keyed by client address (default: `120/minute`).+**Languages**:- Python- Node.js@@ -156,6 +163,8 @@ the RAGFlow-side client without resizing anything.- seccomp- read-only filesystem- memory limits+- network isolation (`--network none` by default)+- shared-secret authentication on the executor API**Advantages**:- Low latency (<90ms), data privacy, full control@@ -1163,6 +1172,9 @@ class CodeExecutorComponent:### Network security- Self-managed: Network isolation by default, no external access+ - Sandbox runner containers are started with `--network none` (opt out via `SANDBOX_CONTAINER_NETWORK`, e.g. `bridge`, when sandboxed code needs outbound network such as runtime `pip`/`npm` installs; prefer baking dependencies into the base images instead)+ - The executor manager API itself requires a shared secret (`SANDBOX_EXECUTOR_MANAGER_API_TOKEN`) on execution endpoints when configured; requests are authenticated with a constant-time comparison of the `Authorization: Bearer` or `X-Sandbox-Token` header+ - The executor manager port is published to loopback (`127.0.0.1`) only; RAGFlow reaches it over the internal compose network- SaaS: HTTPS only, certificate pinning- IP whitelisting for self-managed endpoint accessdiff --git a/agent/sandbox/tests/test_providers.py b/agent/sandbox/tests/test_providers.pyindex 9cde83d206..9ed0117386 100644--- a/agent/sandbox/tests/test_providers.py+++ b/agent/sandbox/tests/test_providers.py@@ -18,11 +18,11 @@Unit tests for sandbox provider abstraction layer."""-import pytestfrom unittest.mock import Mock, patch-import requests-from agent.sandbox.providers.base import SandboxProvider, SandboxInstance, ExecutionResult+import pytest+import requests+from agent.sandbox.providers.base import ExecutionResult, SandboxInstance, SandboxProviderfrom agent.sandbox.providers.manager import ProviderManagerfrom agent.sandbox.providers.self_managed import SelfManagedProvider@@ -245,6 +245,40 @@ class TestSelfManagedProvider:assert result.metadata["result_value"] == {"items": ["a", "b"]}assert result.metadata["result_type"] == "json"+ @patch("requests.post")+ def test_execute_code_sends_bearer_token_when_configured(self, mock_post):+ """Test that the shared-secret token is sent to the executor manager."""+ mock_response = Mock()+ mock_response.status_code = 200+ mock_response.json.return_value = {"status": "success", "stdout": "", "stderr": "", "exit_code": 0}+ mock_post.return_value = mock_response++ provider = SelfManagedProvider()+ provider._initialized = True+ provider.api_token = "shared-secret"++ provider.execute_code(instance_id="test-123", code="def main(): return 1", language="python", timeout=10)++ _, kwargs = mock_post.call_args+ assert kwargs["headers"]["Authorization"] == "Bearer shared-secret"++ @patch("requests.post")+ def test_execute_code_omits_auth_header_without_token(self, mock_post):+ """Test that no Authorization header is sent when no token is set."""+ mock_response = Mock()+ mock_response.status_code = 200+ mock_response.json.return_value = {"status": "success", "stdout": "", "stderr": "", "exit_code": 0}+ mock_post.return_value = mock_response++ provider = SelfManagedProvider()+ provider._initialized = True+ provider.api_token = ""++ provider.execute_code(instance_id="test-123", code="def main(): return 1", language="python", timeout=10)++ _, kwargs = mock_post.call_args+ assert "Authorization" not in kwargs["headers"]+@patch("requests.post")def test_execute_code_timeout(self, mock_post):"""Test code execution timeout."""diff --git a/agent/tools/code_exec.py b/agent/tools/code_exec.pyindex 2cb58c23ac..336bec002f 100644--- a/agent/tools/code_exec.py+++ b/agent/tools/code_exec.py@@ -21,10 +21,9 @@ import osimport uuidfrom abc import ABCfrom collections.abc import Mapping-from typing import Optional+from enum import StrEnumfrom pydantic import BaseModel, Field, field_validator-from enum import StrEnumfrom agent.tools.base import ToolBase, ToolMeta, ToolParamBasefrom api.db.services.file_service import FileService@@ -32,7 +31,6 @@ from common import settingsfrom common.connection_utils import timeoutfrom common.constants import SANDBOX_ARTIFACT_BUCKET, SANDBOX_ARTIFACT_EXPIRE_DAYS-SYSTEM_OUTPUT_KEYS = frozenset({"content",@@ -210,7 +208,7 @@ class Language(StrEnum):class CodeExecutionRequest(BaseModel):code_b64: str = Field(..., description="Base64 encoded code string")language: str = Field(default=Language.PYTHON.value, description="Programming language")- arguments: Optional[dict] = Field(default={}, description="Arguments")+ arguments: dict | None = Field(default={}, description="Arguments")@field_validator("code_b64")@classmethod@@ -219,7 +217,7 @@ class CodeExecutionRequest(BaseModel):base64.b64decode(v, validate=True)return vexcept Exception as e:- raise ValueError(f"Invalid base64 encoding: {str(e)}")+ raise ValueError(f"Invalid base64 encoding: {e!s}")@field_validator("language", mode="before")@classmethod@@ -279,16 +277,17 @@ Supported artifact file types: .png, .jpg, .jpeg, .svg, .pdf, .csv, .json, .htmlCollected artifacts are also parsed automatically and appended to the stable text output `content`. The content includes sections like `attachment1 (image): ...`, `attachment2 (pdf): ...`, so downstream nodes can consume a single text output without depending on unstable attachment-specific variables.Here's a code example for Javascript(`main` function MUST be included and exported):-const axios = require('axios');-async function main(args) {- try {- const response = await axios.get('https://github.com/infiniflow/ragflow');- console.log('Body:', response.data);- } catch (error) {- console.error('Error:', error.message);- }+const fs = require('fs');+function main(args) {+ const numbers = args && args.numbers ? args.numbers : [1, 2, 3];+ const total = numbers.reduce((sum, value) => sum + value, 0);+ fs.writeFileSync('artifacts/summary.json', JSON.stringify({ count: numbers.length, total: total }));+ console.log('Sum:', total);+ return { count: numbers.length, total: total };}module.exports = { main };++Note: the sandbox has NO outbound network access by default (SANDBOX_CONTAINER_NETWORK=none), so code making HTTP requests (axios, fetch, or any external URL) fails with a connection error unless the deployment explicitly sets SANDBOX_CONTAINER_NETWORK=bridge. Prefer offline computation, or have the caller fetch remote data and pass it in via arguments.""","parameters": {"lang": {@@ -352,8 +351,7 @@ class CodeExec(ToolBase, ABC):# Try using the new sandbox provider system firsttry:from agent.sandbox.client import execute_code as sandbox_execute_code- from agent.sandbox.client import get_provider_info- from agent.sandbox.client import reload_provider+ from agent.sandbox.client import get_provider_info, reload_providerfrom agent.sandbox.providers.base import SandboxProviderConfigErrorif self.check_if_canceled("CodeExec execution"):@@ -404,7 +402,14 @@ class CodeExec(ToolBase, ABC):self.set_output("_ERROR", "Task has been canceled")return self.output()- resp = requests.post(url=f"http://{settings.SANDBOX_HOST}:9385/run", json=code_req, timeout=timeout_seconds)+ # Shared-secret authentication towards the executor manager;+ # no header when no token is configured (backwards compatibility).+ headers = {"Content-Type": "application/json"}+ api_token = (os.getenv("SANDBOX_EXECUTOR_MANAGER_API_TOKEN") or "").strip()+ if api_token:+ headers["Authorization"] = f"Bearer {api_token}"++ resp = requests.post(url=f"http://{settings.SANDBOX_HOST}:9385/run", json=code_req, headers=headers, timeout=timeout_seconds)logging.info(f"http://{settings.SANDBOX_HOST}:9385/run, code_req: {code_req}, resp.status_code {resp.status_code}:")if self.check_if_canceled("CodeExec execution"):diff --git a/docker/.env b/docker/.envindex a15a04d739..c6acce530a 100644--- a/docker/.env+++ b/docker/.env@@ -360,6 +360,17 @@ ENABLE_REGISTER=1# SANDBOX_ENABLE_SECCOMP=false# SANDBOX_MAX_MEMORY=256m # b, k, m, g# SANDBOX_TIMEOUT=10s # s, m, 1m30s+# Shared secret required by sandbox-executor-manager's /run endpoint+# (Authorization: Bearer or X-Sandbox-Token). Strongly recommended; the same+# value is injected into the ragflow service. Leave unset for backwards+# compatibility (the executor manager then logs a security warning).+# Generate one with: openssl rand -hex 32+# SANDBOX_EXECUTOR_MANAGER_API_TOKEN=+# Docker network used for sandbox runner containers. "none" (default) means+# sandboxed code has no external network access. Set to "bridge" only if your+# sandboxed code genuinely needs outbound network (e.g. runtime pip/npm+# installs); prefer baking dependencies into the base images instead.+# SANDBOX_CONTAINER_NETWORK=none# -----------------------------------------------------------------------------# Sandbox Enddiff --git a/docker/docker-compose-base.yml b/docker/docker-compose-base.ymlindex 863b05b61b..751451035b 100644--- a/docker/docker-compose-base.yml+++ b/docker/docker-compose-base.yml@@ -181,8 +181,10 @@ services:- sandboximage: ${SANDBOX_EXECUTOR_MANAGER_IMAGE-infiniflow/sandbox-executor-manager:latest}privileged: true+ # Bind to loopback only: the /run endpoint executes arbitrary sandboxed+ # code. RAGFlow reaches this service over the shared `ragflow` network.ports:- - ${SANDBOX_EXECUTOR_MANAGER_PORT-9385}:9385+ - 127.0.0.1:${SANDBOX_EXECUTOR_MANAGER_PORT:-9385}:9385env_file: .envvolumes:- /var/run/docker.sock:/var/run/docker.sock@@ -197,6 +199,12 @@ services:- SANDBOX_ENABLE_SECCOMP=${SANDBOX_ENABLE_SECCOMP:-false}- SANDBOX_MAX_MEMORY=${SANDBOX_MAX_MEMORY:-256m}- SANDBOX_TIMEOUT=${SANDBOX_TIMEOUT:-10s}+ # Shared secret for the /run API. Set it in .env (same value is passed+ # to the ragflow service); when empty the API stays open (backwards+ # compatible) but a warning is logged.+ - SANDBOX_EXECUTOR_MANAGER_API_TOKEN=${SANDBOX_EXECUTOR_MANAGER_API_TOKEN:-}+ # Sandbox runner containers get no external network by default.+ - SANDBOX_CONTAINER_NETWORK=${SANDBOX_CONTAINER_NETWORK:-none}healthcheck:test: ["CMD", "curl", "http://localhost:9385/healthz"]interval: 10sdiff --git a/docker/docker-compose.yml b/docker/docker-compose.ymlindex 454567e924..6321b72b32 100644--- a/docker/docker-compose.yml+++ b/docker/docker-compose.yml@@ -61,6 +61,10 @@ services:- ${SVR_MCP_PORT}:9382 # entry for MCP (host_port:docker_port). The docker_port must match the value you set for `mcp-port` above.- ${GO_HTTP_PORT}:9384- ${GO_ADMIN_PORT}:9383+ environment:+ # Shared secret used to authenticate towards sandbox-executor-manager.+ # Must match SANDBOX_EXECUTOR_MANAGER_API_TOKEN on that service.+ - SANDBOX_EXECUTOR_MANAGER_API_TOKEN=${SANDBOX_EXECUTOR_MANAGER_API_TOKEN:-}volumes:- ./ragflow-logs:/ragflow/logs# - ./nginx/ragflow.conf:/etc/nginx/conf.d/ragflow.conf@@ -124,11 +128,15 @@ services:# - ./nginx/nginx.conf:/etc/nginx/nginx.conf- ./service_conf.yaml.template:/ragflow/conf/service_conf.yaml.template- ./entrypoint.sh:/ragflow/entrypoint.sh+ environment:+ # Shared secret used to authenticate towards sandbox-executor-manager.+ # Must match SANDBOX_EXECUTOR_MANAGER_API_TOKEN on that service.+ - SANDBOX_EXECUTOR_MANAGER_API_TOKEN=${SANDBOX_EXECUTOR_MANAGER_API_TOKEN:-}env_file: .envnetworks:- ragflowrestart: unless-stopped- # https://docs.docker.com/engine/daemon/prometheus/#create-a-prometheus-configuration+ # https://docs.docker.com/engine/daemon/prometheus/#create-a-prometheus-scrape-configuration# If you use Docker Desktop, the --add-host flag is optional. This flag ensures that the host's internal IP is exposed to the Prometheus container.extra_hosts:- "host.docker.internal:host-gateway"diff --git a/internal/agent/sandbox/manager_test.go b/internal/agent/sandbox/manager_test.goindex 0910c955e9..7d8fc0de30 100644--- a/internal/agent/sandbox/manager_test.go+++ b/internal/agent/sandbox/manager_test.go@@ -212,11 +212,11 @@ func TestNewSelfManagedProviderFromConfig_MinimalConfig(t *testing.T) {func TestNewSelfManagedProviderFromConfig_FullConfig(t *testing.T) {t.Parallel()cfg := map[string]any{- "EXECUTOR_MANAGER_URL": "https://custom.example:9999/",- "EXECUTOR_MANAGER_TIMEOUT": float64(45), // JSON-decoded number- "EXECUTOR_MANAGER_POOL_SIZE": float64(10),- "BASE_PYTHON_IMAGE": "registry.example.com/py:latest",- "BASE_NODEJS_IMAGE": "registry.example.com/node:20",+ "endpoint": "https://custom.example:9999/",+ "timeout": float64(45), // JSON-decoded seconds+ "pool_size": float64(10),+ "base_python_image": "registry.example.com/py:latest",+ "base_nodejs_image": "registry.example.com/node:20",}p := newSelfManagedProviderFromConfig(cfg)if p.endpoint != "https://custom.example:9999" {@@ -241,7 +241,7 @@ func TestNewSelfManagedProviderFromConfig_FullConfig(t *testing.T) {func TestNewSelfManagedProviderFromConfig_TimeoutAsString(t *testing.T) {t.Parallel()cfg := map[string]any{- "EXECUTOR_MANAGER_TIMEOUT": "1m30s",+ "timeout": "1m30s",}p := newSelfManagedProviderFromConfig(cfg)if p.timeout != 90*time.Second {@@ -381,7 +381,7 @@ func TestBuildProviderFromConfig_UnknownType(t *testing.T) {func TestBuildProviderFromConfig_SelfManaged_HappyPath(t *testing.T) {t.Parallel()p, err := buildProviderFromConfig(ProviderSelfManaged, map[string]any{- "EXECUTOR_MANAGER_URL": "http://example.invalid:9999",+ "endpoint": "http://example.invalid:9999",})if err != nil {t.Fatalf("buildProviderFromConfig: %v", err)@@ -437,10 +437,10 @@ func TestLoadFromSettingsWithReader_HappyPath(t *testing.T) {rows: map[string][]entity.SystemSettings{"sandbox.provider_type": {{Name: "sandbox.provider_type", Value: "self_managed"}},"sandbox.self_managed": {{Name: "sandbox.self_managed", Value: `{- "EXECUTOR_MANAGER_URL": "` + srv.URL + `",- "EXECUTOR_MANAGER_TIMEOUT": "5s",- "EXECUTOR_MANAGER_POOL_SIZE": 7,- "BASE_PYTHON_IMAGE": "reg.example.com/py:1"+ "endpoint": "` + srv.URL + `",+ "timeout": "5s",+ "pool_size": 7,+ "base_python_image": "reg.example.com/py:1"}`}},},}@@ -649,8 +649,8 @@ func TestReloadFromSettingsWithReader(t *testing.T) {rows: map[string][]entity.SystemSettings{"sandbox.provider_type": {{Name: "sandbox.provider_type", Value: "self_managed"}},"sandbox.self_managed": {{Name: "sandbox.self_managed", Value: `{- "EXECUTOR_MANAGER_URL": "` + srv.URL + `",- "EXECUTOR_MANAGER_TIMEOUT": "5s"+ "endpoint": "` + srv.URL + `",+ "timeout": "5s"}`}},},}@@ -669,3 +669,63 @@ func TestReloadFromSettingsWithReader(t *testing.T) {t.Errorf("endpoint = %q, want %q (from settings after reload)", sm.endpoint, srv.URL)}}++// TestLoadFromSettingsWithReader_CanonicalSchemaTokenPropagation verifies+// the full settings-driven path against the canonical lowercase persisted+// JSON: LoadFromSettingsWithReader builds the provider from the+// sandbox.self_managed row, and the executor manager receives the+// Authorization header derived from the row's api_token on /run. This is+// the configuration-level counterpart of the request-level bearer test in+// self_managed_test.go.+func TestLoadFromSettingsWithReader_CanonicalSchemaTokenPropagation(t *testing.T) {+ var capturedAuth string+ var authSeen bool+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch r.URL.Path {+ case "/healthz":+ w.WriteHeader(http.StatusOK)+ _, _ = w.Write([]byte(`{"status":"ok"}`))+ case "/run":+ capturedAuth, authSeen = r.Header.Get("Authorization"), true+ handleRun(t, w, r, "ok", "")+ default:+ w.WriteHeader(http.StatusNotFound)+ }+ }))+ defer srv.Close()+ ctx := t.Context()++ // The exact lowercase shape the admin panel persists; only endpoint and+ // api_token are set so the env fallbacks for the other fields stay+ // exercised.+ r := &fakeSettingsReader{+ rows: map[string][]entity.SystemSettings{+ "sandbox.provider_type": {{Name: "sandbox.provider_type", Value: "self_managed"}},+ "sandbox.self_managed": {{Name: "sandbox.self_managed", Value: `{+ "endpoint": "` + srv.URL + `",+ "api_token": "canonical-settings-secret"+ }`}},+ },+ }+ m := &ProviderManager{}+ if err := m.LoadFromSettingsWithReader(ctx, dao.DB, r); err != nil {+ t.Fatalf("LoadFromSettingsWithReader: %v", err)+ }+ sm, ok := m.Provider().(*SelfManagedProvider)+ if !ok {+ t.Fatalf("provider type = %T, want *SelfManagedProvider", m.Provider())+ }+ if sm.apiToken != "canonical-settings-secret" {+ t.Errorf("apiToken = %q, want canonical-settings-secret (from lowercase settings row)", sm.apiToken)+ }+ inst, err := sm.CreateInstance(ctx, "python")+ if err != nil {+ t.Fatalf("CreateInstance: %v", err)+ }+ if _, err := sm.ExecuteCode(ctx, inst, "def main(): return 1", "python", 5, nil); err != nil {+ t.Fatalf("ExecuteCode: %v", err)+ }+ if !authSeen || capturedAuth != "Bearer canonical-settings-secret" {+ t.Errorf("Authorization header = %q (seen=%v), want Bearer canonical-settings-secret", capturedAuth, authSeen)+ }+}diff --git a/internal/agent/sandbox/self_managed.go b/internal/agent/sandbox/self_managed.goindex 3476c0d71e..8567cd8dbd 100644--- a/internal/agent/sandbox/self_managed.go+++ b/internal/agent/sandbox/self_managed.go@@ -62,6 +62,7 @@ import ("fmt""io""net/http"+ "strconv""strings""sync""time"@@ -101,6 +102,11 @@ type SelfManagedProvider struct {// language's image get a per-language override path that the// executor_manager can then route at container-create time.baseImages map[string]string+ // apiToken is the shared secret authenticating this RAGFlow+ // instance towards the executor_manager's /run endpoint+ // (Authorization: Bearer). Empty means the executor_manager runs+ // without authentication (backwards compatibility).+ apiToken string}// newSelfManagedProviderFromEnv reads SANDBOX_EXECUTOR_MANAGER_URL@@ -111,39 +117,77 @@ type SelfManagedProvider struct {// are also read; empty values mean "use executor_manager's// default image" — no override.func newSelfManagedProviderFromEnv() *SelfManagedProvider {- return newSelfManagedProviderFromConfig(selfManagedConfigFromEnv())+ return newSelfManagedProviderFromConfig(map[string]any{})}-// selfManagedConfigFromEnv builds a config map from the SANDBOX_*-// env vars, mirroring the admin-panel settings JSON shape.-func selfManagedConfigFromEnv() map[string]any {- return map[string]any{- "EXECUTOR_MANAGER_URL": common.GetEnv(common.EnvSandboxExecutorManagerURL),- "EXECUTOR_MANAGER_TIMEOUT": common.GetEnv(common.EnvSandboxExecutorManagerTimeout),- "EXECUTOR_MANAGER_POOL_SIZE": common.GetEnv(common.EnvSandboxExecutorManagerPoolSize),- "EXECUTOR_MANAGER_MAX_RETRIES": common.GetEnv(common.EnvSandboxExecutorManagerMaxRetries),- "BASE_PYTHON_IMAGE": common.GetEnv(common.EnvSandboxBasePythonImage),- "BASE_NODEJS_IMAGE": common.GetEnv(common.EnvSandboxBaseNodeJSImage),+// The provider resolves each field through one canonical chain: the+// lowercase admin-panel settings schema first (endpoint, timeout,+// max_retries, pool_size, api_token — the exact schema+// agent/sandbox/providers/self_managed.py persists and reads; pool_size+// additionally accepts its executor_manager_pool_size spelling, which the+// Python provider reads first), then the SANDBOX_* environment variable for+// fields absent from persisted settings, then the built-in default. Keeping+// a single resolution path on both runtimes means a settings row plus+// environment variables configures Python and Go identically.+func configStringEnv(cfg map[string]any, envName string, keys ...string) string {+ for _, key := range keys {+ // Blank-after-trim values count as absent so they fall through to+ // the environment, matching the Python provider's+ // `config.get(...) or env` resolution.+ if value := strings.TrimSpace(configString(cfg, key)); value != "" {+ return value+ }}+ return common.GetEnv(envName)}-// newSelfManagedProviderFromConfig builds the provider from a JSON-// config map (as stored in the system_settings table for the-// self_managed provider). The config keys mirror the env-var names-// without the SANDBOX_ prefix; see selfManagedConfigFromEnv for the-// shape. Missing / unparseable values fall back to the same defaults-// the env-driven path uses.+func configIntEnv(cfg map[string]any, fallback int, envName string, keys ...string) int {+ for _, key := range keys {+ if _, ok := cfg[key]; ok && cfg[key] != nil {+ return configInt(cfg, key, fallback)+ }+ }+ if envValue := common.GetEnv(envName); envValue != "" {+ if parsed, err := strconv.Atoi(envValue); err == nil {+ return parsed+ }+ }+ return fallback+}++func configDurationEnv(cfg map[string]any, fallback time.Duration, envName string, keys ...string) time.Duration {+ for _, key := range keys {+ if _, ok := cfg[key]; ok && cfg[key] != nil {+ return configDuration(cfg, key, fallback)+ }+ }+ if envValue := common.GetEnv(envName); envValue != "" {+ if parsed, err := time.ParseDuration(envValue); err == nil && parsed > 0 {+ return parsed+ }+ }+ return fallback+}++// newSelfManagedProviderFromConfig builds the provider from the persisted+// settings JSON (the `sandbox.self_managed` row written by the admin panel,+// lowercase keys: endpoint, timeout, max_retries, pool_size, api_token,+// optionally base_python_image / base_nodejs_image). Fields absent from the+// settings fall back to their SANDBOX_* environment variable before the+// built-in default, mirroring the Python provider's config-over-env+// resolution.func newSelfManagedProviderFromConfig(cfg map[string]any) *SelfManagedProvider {- endpoint := configString(cfg, "EXECUTOR_MANAGER_URL")+ endpoint := configStringEnv(cfg, common.EnvSandboxExecutorManagerURL, "endpoint")if endpoint == "" {endpoint = selfManagedDefaultEndpoint}endpoint = strings.TrimRight(endpoint, "/")- timeout := configDuration(cfg, "EXECUTOR_MANAGER_TIMEOUT", 30*time.Second)- poolSize := configInt(cfg, "EXECUTOR_MANAGER_POOL_SIZE", 3)- maxRetries := configInt(cfg, "EXECUTOR_MANAGER_MAX_RETRIES", 3)- _ = maxRetries // retained for future use; retry is in HTTPClient+ timeout := configDurationEnv(cfg, 30*time.Second, common.EnvSandboxExecutorManagerTimeout, "timeout")+ poolSize := configIntEnv(cfg, 3, common.EnvSandboxExecutorManagerPoolSize, "executor_manager_pool_size", "pool_size")+ // max_retries maps to the HTTP client's total attempt count (first try+ // included), matching its default of 3 attempts.+ maxRetries := configIntEnv(cfg, 3, common.EnvSandboxExecutorManagerMaxRetries, "max_retries")// Per-language base image overrides. Empty = executor_manager// default; non-empty = the operator picked a custom base image@@ -151,17 +195,21 @@ func newSelfManagedProviderFromConfig(cfg map[string]any) *SelfManagedProvider {// pre-installed, or a node image with native deps for native// addons).baseImages := map[string]string{- "python": configString(cfg, "BASE_PYTHON_IMAGE"),- "nodejs": configString(cfg, "BASE_NODEJS_IMAGE"),+ "python": configStringEnv(cfg, common.EnvSandboxBasePythonImage, "base_python_image"),+ "nodejs": configStringEnv(cfg, common.EnvSandboxBaseNodeJSImage, "base_nodejs_image"),}+ apiToken := strings.TrimSpace(configStringEnv(cfg, common.EnvSandboxExecutorManagerAPIToken, "api_token"))+return &SelfManagedProvider{endpoint: endpoint,timeout: timeout,poolSize: poolSize,baseImages: baseImages,+ apiToken: apiToken,helper: NewHTTPClient(HTTPConfig{- Timeout: timeout,+ Timeout: timeout,+ MaxAttempts: maxRetries,}),healthHelper: NewHTTPClient(HTTPConfig{Timeout: 5 * time.Second,@@ -264,7 +312,13 @@ func (p *SelfManagedProvider) ExecuteCode(}start := time.Now()- resp, err := p.helper.Do(ctx, http.MethodPost, p.endpoint+"/run", string(body), "application/json", nil)+ // Shared-secret authentication towards the executor_manager; empty+ // token keeps the request unauthenticated (backwards compatibility).+ var reqHeaders map[string]string+ if p.apiToken != "" {+ reqHeaders = map[string]string{"Authorization": "Bearer " + p.apiToken}+ }+ resp, err := p.helper.Do(ctx, http.MethodPost, p.endpoint+"/run", string(body), "application/json", reqHeaders)if err != nil {return nil, fmt.Errorf("self_managed: POST /run: %w", err)}diff --git a/internal/agent/sandbox/self_managed_test.go b/internal/agent/sandbox/self_managed_test.goindex bd0bf4bf69..404901ebf3 100644--- a/internal/agent/sandbox/self_managed_test.go+++ b/internal/agent/sandbox/self_managed_test.go@@ -207,6 +207,56 @@ func TestSelfManaged_ExecuteCode(t *testing.T) {}}+func TestSelfManaged_ExecuteCode_SendsBearerToken(t *testing.T) {+ t.Parallel()+ var capturedAuth string+ var authSeen bool+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ capturedAuth, authSeen = r.Header.Get("Authorization"), true+ handleRun(t, w, r, "ok", "")+ }))+ defer srv.Close()+ ctx := t.Context()++ p := newSelfManagedForTest(srv.URL)+ p.apiToken = "unit-test-shared-secret"+ p.initialized = true+ inst, err := p.CreateInstance(ctx, "python")+ if err != nil {+ t.Fatalf("CreateInstance: %v", err)+ }+ if _, err := p.ExecuteCode(ctx, inst, "def main(): return 1", "python", 5, nil); err != nil {+ t.Fatalf("ExecuteCode: %v", err)+ }+ if !authSeen || capturedAuth != "Bearer unit-test-shared-secret" {+ t.Errorf("Authorization header = %q (seen=%v), want %q", capturedAuth, authSeen, "Bearer unit-test-shared-secret")+ }+}++func TestSelfManaged_ExecuteCode_OmitsAuthHeaderWithoutToken(t *testing.T) {+ t.Parallel()+ var authSeen bool+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ _, authSeen = r.Header["Authorization"]+ handleRun(t, w, r, "ok", "")+ }))+ defer srv.Close()+ ctx := t.Context()++ p := newSelfManagedForTest(srv.URL)+ p.initialized = true+ inst, err := p.CreateInstance(ctx, "python")+ if err != nil {+ t.Fatalf("CreateInstance: %v", err)+ }+ if _, err := p.ExecuteCode(ctx, inst, "def main(): return 1", "python", 5, nil); err != nil {+ t.Fatalf("ExecuteCode: %v", err)+ }+ if authSeen {+ t.Errorf("Authorization header unexpectedly present")+ }+}+func TestSelfManaged_ExecuteCode_JSWrapped(t *testing.T) {t.Parallel()var capturedBody []byte@@ -502,3 +552,169 @@ func handleRunWithResult(t *testing.T, w http.ResponseWriter, _ *http.Request, sw.WriteHeader(http.StatusOK)_ = json.NewEncoder(w).Encode(resp)}++// TestNewSelfManagedProviderFromConfig_CanonicalPythonSchema pins the+// canonical lowercase admin-panel settings shape that the Python provider+// persists and reads (`sandbox.self_managed`: endpoint, timeout,+// max_retries, pool_size, api_token). The Go provider must map the same+// schema so a standard settings row configures both runtimes identically.+func TestNewSelfManagedProviderFromConfig_CanonicalPythonSchema(t *testing.T) {+ t.Parallel()+ p := newSelfManagedProviderFromConfig(map[string]any{+ "endpoint": "https://manager.example:9385/",+ "timeout": float64(20), // JSON-decoded seconds+ "max_retries": float64(5),+ "pool_size": float64(9),+ "api_token": "settings-secret",+ })+ if p.helper.maxAttempts != 5 {+ t.Errorf("helper maxAttempts = %d, want 5 from settings max_retries", p.helper.maxAttempts)+ }+ if p.endpoint != "https://manager.example:9385" {+ t.Errorf("endpoint = %q, want trailing slash stripped", p.endpoint)+ }+ if p.timeout != 20*time.Second {+ t.Errorf("timeout = %v, want 20s", p.timeout)+ }+ if p.poolSize != 9 {+ t.Errorf("poolSize = %d, want 9", p.poolSize)+ }+ if p.apiToken != "settings-secret" {+ t.Errorf("apiToken = %q, want settings-secret", p.apiToken)+ }+}++// TestNewSelfManagedProviderFromConfig_ApiTokenResolution pins the token+// resolution contract shared with the Python provider: an explicit settings+// value wins, and an absent/empty settings value falls back to+// SANDBOX_EXECUTOR_MANAGER_API_TOKEN. Cannot use t.Parallel() with t.Setenv.+func TestNewSelfManagedProviderFromConfig_ApiTokenResolution(t *testing.T) {+ t.Setenv("SANDBOX_EXECUTOR_MANAGER_API_TOKEN", "env-secret")++ fromEnv := newSelfManagedProviderFromConfig(map[string]any{+ "endpoint": "https://manager.example:9385",+ })+ if fromEnv.apiToken != "env-secret" {+ t.Errorf("apiToken = %q, want env fallback value", fromEnv.apiToken)+ }++ fromSettings := newSelfManagedProviderFromConfig(map[string]any{+ "endpoint": "https://manager.example:9385",+ "api_token": "settings-secret",+ })+ if fromSettings.apiToken != "settings-secret" {+ t.Errorf("apiToken = %q, want settings value to win over env", fromSettings.apiToken)+ }++ blankSettingsWins := newSelfManagedProviderFromConfig(map[string]any{+ "endpoint": "https://manager.example:9385",+ "api_token": " ",+ })+ if blankSettingsWins.apiToken != "env-secret" {+ t.Errorf("apiToken = %q, want blank settings value to fall back to env", blankSettingsWins.apiToken)+ }+}++// TestSelfManaged_ExecuteCode_TokenFromCanonicalSettingsPropagation is the+// end-to-end version of the bearer-token test: the provider is built from+// the real lowercase persisted settings JSON (not by setting apiToken+// directly), and the fake executor manager asserts the Authorization header+// that /run actually receives.+func TestSelfManaged_ExecuteCode_TokenFromCanonicalSettingsPropagation(t *testing.T) {+ t.Parallel()+ var capturedAuth string+ var authSeen bool+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch r.URL.Path {+ case "/healthz":+ w.WriteHeader(http.StatusOK)+ _, _ = w.Write([]byte(`{"status":"ok"}`))+ case "/run":+ capturedAuth, authSeen = r.Header.Get("Authorization"), true+ handleRun(t, w, r, "ok", "")+ default:+ w.WriteHeader(http.StatusNotFound)+ }+ }))+ defer srv.Close()+ ctx := t.Context()++ // The exact JSON shape the admin panel persists for sandbox.self_managed.+ var settings map[string]any+ if err := json.Unmarshal([]byte(`{+ "endpoint": "`+srv.URL+`",+ "timeout": 5,+ "max_retries": 3,+ "pool_size": 3,+ "api_token": "settings-shared-secret"+ }`), &settings); err != nil {+ t.Fatalf("unmarshal settings: %v", err)+ }+ p := newSelfManagedProviderFromConfig(settings)+ if err := p.Initialize(ctx); err != nil {+ t.Fatalf("Initialize: %v", err)+ }+ inst, err := p.CreateInstance(ctx, "python")+ if err != nil {+ t.Fatalf("CreateInstance: %v", err)+ }+ if _, err := p.ExecuteCode(ctx, inst, "def main(): return 1", "python", 5, nil); err != nil {+ t.Fatalf("ExecuteCode: %v", err)+ }+ if !authSeen || capturedAuth != "Bearer settings-shared-secret" {+ t.Errorf("Authorization header = %q (seen=%v), want Bearer settings-shared-secret", capturedAuth, authSeen)+ }+}++// TestNewSelfManagedProvider_EnvOnlyFallbacks pins the environment-only+// configuration path: with an empty settings map, every SANDBOX_* variable+// (including the pool size, which previously lost its env fallback) reaches+// the provider. Cannot use t.Parallel() with t.Setenv.+func TestNewSelfManagedProvider_EnvOnlyFallbacks(t *testing.T) {+ t.Setenv("SANDBOX_EXECUTOR_MANAGER_URL", "https://env.example:9385")+ t.Setenv("SANDBOX_EXECUTOR_MANAGER_TIMEOUT", "15s")+ t.Setenv("SANDBOX_EXECUTOR_MANAGER_POOL_SIZE", "11")+ t.Setenv("SANDBOX_EXECUTOR_MANAGER_MAX_RETRIES", "6")+ // asserted via p.helper.maxAttempts below+ t.Setenv("SANDBOX_BASE_PYTHON_IMAGE", "reg.example.com/envpy:2")+ t.Setenv("SANDBOX_EXECUTOR_MANAGER_API_TOKEN", "env-only-secret")++ p := newSelfManagedProviderFromEnv()+ if p.endpoint != "https://env.example:9385" {+ t.Errorf("endpoint = %q, want env value", p.endpoint)+ }+ if p.timeout != 15*time.Second {+ t.Errorf("timeout = %v, want 15s from env", p.timeout)+ }+ if p.poolSize != 11 {+ t.Errorf("poolSize = %d, want 11 from env", p.poolSize)+ }+ if p.baseImages["python"] != "reg.example.com/envpy:2" {+ t.Errorf("python baseImage = %q, want env value", p.baseImages["python"])+ }+ if p.apiToken != "env-only-secret" {+ t.Errorf("apiToken = %q, want env value", p.apiToken)+ }+ if p.helper.maxAttempts != 6 {+ t.Errorf("helper maxAttempts = %d, want 6 from env max retries", p.helper.maxAttempts)+ }+}++// TestNewSelfManagedProviderFromConfig_SettingsBeatEnv pins precedence:+// persisted settings values win over the environment for the same field.+// Cannot use t.Parallel() with t.Setenv.+func TestNewSelfManagedProviderFromConfig_SettingsBeatEnv(t *testing.T) {+ t.Setenv("SANDBOX_EXECUTOR_MANAGER_POOL_SIZE", "11")+ t.Setenv("SANDBOX_BASE_PYTHON_IMAGE", "reg.example.com/envpy:2")++ p := newSelfManagedProviderFromConfig(map[string]any{+ "pool_size": float64(4),+ "base_python_image": "reg.example.com/settingspy:3",+ })+ if p.poolSize != 4 {+ t.Errorf("poolSize = %d, want settings value 4 to beat env 11", p.poolSize)+ }+ if p.baseImages["python"] != "reg.example.com/settingspy:3" {+ t.Errorf("python baseImage = %q, want settings value to beat env", p.baseImages["python"])+ }+}diff --git a/internal/common/environments.go b/internal/common/environments.goindex c1a3059845..bef2653a17 100644--- a/internal/common/environments.go+++ b/internal/common/environments.go@@ -104,6 +104,7 @@ const (EnvSandboxExecutorManagerTimeout = "SANDBOX_EXECUTOR_MANAGER_TIMEOUT"EnvSandboxExecutorManagerPoolSize = "SANDBOX_EXECUTOR_MANAGER_POOL_SIZE"EnvSandboxExecutorManagerMaxRetries = "SANDBOX_EXECUTOR_MANAGER_MAX_RETRIES"+ EnvSandboxExecutorManagerAPIToken = "SANDBOX_EXECUTOR_MANAGER_API_TOKEN"EnvSandboxBasePythonImage = "SANDBOX_BASE_PYTHON_IMAGE"EnvSandboxBaseNodeJSImage = "SANDBOX_BASE_NODEJS_IMAGE"EnvSandboxArtifactBucket = "SANDBOX_ARTIFACT_BUCKET"