Skip to content

security: isolate agent execution in a per-task sandbox worker - #716

Merged
BaaRai merged 3 commits into
mainfrom
sec/sandbox-mvp
Nov 30, 2026
Merged

BaaRai merged 3 commits into
mainfrom
sec/sandbox-mvp

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Summary

Isolate model-controlled tool execution from the panel. Stacked on #715 (P0 hardening); merge after it.

The panel keeps the API, PostgreSQL, the keys and the LLM credentials. Every agent command runs in a disposable per-task worker container with no secrets, no panel or database route, and a per-task egress proxy. The worker can reach only what the operator approved.

What changes

  • Runner. artex-runner owns the Docker socket, behind bearer-token auth. It creates one worker and one egress proxy per task. The panel never mounts the Docker socket and the worker cannot reach the runner.
  • Worker. uid/gid 10001, read-only rootfs, cap-drop ALL, no-new-privileges, pids/memory/CPU limits, tmpfs for /tmp and the executor home, only /app/data/tasks/<id>:/workspace:rw. No keys, config, DSN or LLM credentials.
  • Egress. Per-task internal network. The proxy enforces a deny-by-default allowlist with DNS pinning, blocks IMDS, RFC1918, loopback, link-local and IPv6 unless explicitly approved, and supports HTTP/HTTPS CONNECT and SOCKS5.
  • Scope drift closed. task_scope is coverage scope and is agent-writable. Migration 0023_task_scope_operator_approval.sql adds approved_by_operator. Only operator-approved rows (task scope UI, operator-selected companies, operator retarget) grant sandbox egress. Agent add_task_scope, auto insert_assets, agent-spawned company scope and expanded rows are coverage-only. WebFetch and the shared browser use the same policy.
  • Kill switch. artex-exec-wrap --kill-uid scans /proc for every uid-10001 process and does not trust any agent-writable state. The runner force-removes the worker if a process survives, preserving the workspace.
  • Audit. The egress log lives on a proxy-only mount and stdout, outside the worker workspace, so the worker cannot truncate or forge it.
  • Smaller fixes. Company scope expansion for operator-selected companies, internal-network verification for pre-existing task networks, dead-worker recreation, and script staging through a confined workspace file instead of argv.

Breaking changes

  • ARTEX_SANDBOX defaults to required. Without artex-runner, tasks refuse to execute and pause with a clear error. ARTEX_SANDBOX=off is the explicit, loud degraded mode.
  • New compose overlay docker-compose.sandbox.yml and worker/runner/proxy images are required for the sandbox.
  • Existing tasks: migration 0023 backfills operator-entered source='manual' scopes. Goal-derived (source='agent') and auto (source='auto') scopes must be re-approved once in Task Scope before sandbox egress is granted. This is fail-closed and documented in docs/SANDBOX.md.
  • Target-gate approval only clears the tool-call approval gate. Worker, WebFetch and shared-browser egress still require an operator-approved Task Scope entry or a task allow rule. The denial message and the approval response state this explicitly.

Tests

  • go build ./..., go vet ./...
  • Full go test -p 1 ./... on PostgreSQL 16, 24/24 packages
  • go test -race ./sandbox ./agent ./server
  • Live Docker end-to-end: containment, IMDS/arbitrary egress denial, tamper-proof audit, adversarial kill switch (setsid, nohup, double fork trap '' TERM, egress loop), dead-worker recreation
  • Migrations 1..23, 0022 and 0023 backfills, archive restore
  • TestSandboxScopeDriftBlockedAcrossProxyWebFetchAndBrowser: agent/auto/manual-spoof/company rows denied across proxy, WebFetch and shared browser; operator rows and operator companies allowed; domain suffix enforced

References and credits

Implements the execution-scope design tracked in #421 and #423 by @youssefmedhatl, and #554, #555 and #553 by @renaesop, with the two-sources-of-truth analysis by @Dukotah in #421. The public writeup of 28 November 2026 confirmed the gap. The audit surface will continue in the follow-up tracked by #553.

…ate backfill, and kernel kill-switch

- Startup fail-closed: LockDownKeyDir explicitly chmods 0700 an existing key
  dir (counteracting host bind mounts) and 0600 existing key files; adds keys/
  itself alongside jwt.key, secret.key, and config.json to IsolationSelfTest;
  makes IsolationSelfTest fatal unless ARTEX_ALLOW_UNISOLATED=1 is set.
- Secret rotation: adds db.RotateSecretKey (single atomic transaction
  re-encrypting all sealed columns/settings and bumping auth.password_version),
  cmd/artex -rotate-secrets, scripts/rotate-secrets.sh, and OPS/SECURITY docs.
- Scoped defaults, backfill & posture warnings: TargetGate defaults ON for
  tasks with CompanyIDs or InterceptRules (explicit opt-out persisted in
  tasks.target_gate_opt_out); migration 0022_target_gate_backfill.sql backfills
  existing active scoped tasks that did not explicitly opt out; egress-guard
  defaults to EGRESS_MODE=enforce when scope lists exist; loud startup warning
  when target gate or egress enforcement is off.
- Kernel-backed local kill-switch: enables PR_SET_CHILD_SUBREAPER and scans
  /proc (without trusting agent-writable state) on Engine.Pause/BeginDelete to
  kill escaped setsid, nohup, and nested double-fork daemons (plus all
  execPriv.UID processes in isolated mode) within <= 5s with zero post-pause
  egress.
…s proxy

Introduce the sandbox MVP architecture separating panel credentials and
PostgreSQL access from model-controlled tool execution:

- Runner service (cmd/artex-runner, sandbox/) owning /var/run/docker.sock
  behind bearer-token auth (ARTEX_RUNNER_TOKEN) with POST /v1/tasks/{id}/exec,
  POST /v1/tasks/{id}/kill, DELETE /v1/tasks/{id}, and GET /v1/health.
- Strict input and env validation (400 on forbidden secret/DB/cloud env
  patterns or values > 8192 bytes, strict task ID and /workspace workdir).
- Hardened per-task worker containers (Dockerfile.worker, artex-worker:local,
  cmd/artex-exec-wrap): uid/gid 10001, artex-exec-wrap --init PID 1 zombie
  reaper, read-only rootfs, cap-drop ALL, no-new-privileges, pids/mem/cpu
  limits, tmpfs /tmp and /home/exec, only /app/data/tasks/<id>:/workspace:rw,
  only verified internal=true runner-labeled network artex-task-<id>.
- Kernel-backed kill-switch (zero agent-writable state): Kill executes
  artex-exec-wrap --kill-uid 10001 as uid 10001 scanning /proc directly
  (defeating setsid, nohup, nested double-forked trap-TERM daemons, and /tmp
  tampering), verifies zero non-PID-1 uid-10001 processes remain, and falls
  back to force-removing the worker container if any check fails. Dead
  workers/proxies are recreated automatically when startContainer fails.
- Operator-only scope approval (migration 0023_task_scope_operator_approval.sql):
  adds task_scope.approved_by_operator BOOLEAN NOT NULL DEFAULT FALSE, set
  true exclusively by operator API/UI (AddOperatorScope) and operator-selected
  TaskCreateOptions.CompanyIDs (insertTaskCompanies). AddAgentScope and
  AddAutoScope leave it false (coverage-only). taskSandboxRunInfo reads only
  approved, non-expanded rows (and expands company scope only for approved
  company rows); tasks with no approved scope deny all egress.
- Per-task egress proxy (cmd/artex-egress-proxy, Dockerfile.egress-proxy,
  artex-egress-proxy:local): HTTP/HTTPS CONNECT (:3128) and SOCKS5 (:1080),
  deny-by-default allowlist with strict domain suffix enforcement, blocks
  RFC1918/loopback/link-local/IMDS and IPv6 unless in scope CIDRs, DNS
  pinning, and tamper-proof JSONL audit logging to a proxy-only mount
  (/app/data/egress-audit/<id>:/audit:rw) never mounted into the worker.
- Panel integration (agent/sandbox.go, agent/webfetch.go, server/sandbox.go,
  server/sharedbrowser_guard.go, server/assembly.go, server/customtool.go,
  docker-compose.sandbox.yml): ARTEX_SANDBOX defaults to required (fail closed,
  no local fallback), WebFetch and opt-in shared browser inherit the
  operator-approved sandbox.EgressPolicy (with target_gate or explicit proxy
  required for shared browser), custom script tools staged via symlink-confined
  workspace files (avoiding argv E2BIG), Pause/Stop/Delete wired to runner
  Kill/Delete.
- Documented breaking change and hardening details in docs/SANDBOX.md,
  README.md, and CHANGELOG.md.
@BaaRai
BaaRai merged commit 02b6452 into main Nov 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant