security: split internal/edge access, harden sandbox, fix follow-up matching

- server: two listeners — :4646 internal (PROXY_INTERNAL_TOKEN only,
  X-Authentik-Username no longer trusted) and :4647 edge (Authentik
  outpost only; requires X-Authentik-Username; NetworkPolicy-restricted)
- session-manager: strip PROXY_INTERNAL_TOKEN & friends from the ACP
  child env (allowlist + proxy vars), dedicated agent HOME, resolved
  tool_call tracking so findSessionForFollowUp ignores already-answered
  results (no more cross-turn log spam / stale matches)
- ask_question auto-answer picks the SAFE option (deny/reject/last),
  never options[0] — root cause of the allow-all config drift
- bootstrap: force-rewrite deny-all cli-config on every boot into BOTH
  proxy and agent HOME (PVC drift self-heals), strip agent-authored hooks
- cli-config: deny all 22 built-in tools (Shell/Read/Write/Grep/Glob/
  WebFetch/WebSearch/Task/...), allow Mcp(*) only
- deploy: edge Service + ServiceExport, NetworkPolicy (4646 open+token,
  4647 only from node2 pod CIDR), readOnlyRootFilesystem, no SA token,
  agent-home + tmp emptyDirs
- Dockerfile: pin cursor-agent 2026.10.22-27dcf5f from build cache (deny
  enforcement verified), non-root, dedicated agent home
- e2e: auth-surface matrix + built-in Read leak check + bridge egress

Closes infra-yip
This commit is contained in:
Ubuntu
2026-10-23 10:11:39 +00:00
parent 71f95e7a0c
commit c0d758249c
11 changed files with 494 additions and 65 deletions
+51 -4
View File
@@ -1,6 +1,12 @@
#!/bin/bash
# Bootstrap: seed ~/.cursor/cli-config.json and verify the cursor-agent
# Bootstrap: enforce the sandbox cli-config and verify the cursor-agent
# binary works before the HTTP server starts serving traffic.
#
# The config is FORCE-REWRITTEN on every boot. The agent CLI rewrites
# ~/.cursor/cli-config.json at runtime (model choice, permission dialogs
# auto-answered in the past, etc.); with HOME on a PVC a drifted config
# (observed 2026-10: allow-all Shell/Read/Write) would survive reboots
# forever. Rewriting on boot pins the security-relevant fields.
set -euo pipefail
CURSOR_HOME="${HOME}/.cursor"
@@ -9,8 +15,49 @@ export NO_OPEN_BROWSER=1
mkdir -p "${CURSOR_HOME}"
if [[ -f "${BOOTSTRAP}/cli-config.json" ]] && [[ ! -f "${CURSOR_HOME}/cli-config.json" ]]; then
cp -f "${BOOTSTRAP}/cli-config.json" "${CURSOR_HOME}/cli-config.json"
# Force-rewrite the security config every boot (merge model preference if
# the runtime config has one, keep everything else from the bootstrap file).
# Written to BOTH the proxy HOME and the sandboxed agent HOME — the ACP
# children read the latter.
write_config() {
local target_home="$1"
mkdir -p "${target_home}/.cursor"
if [[ -f "${BOOTSTRAP}/cli-config.json" ]]; then
local model_json="{}"
if [[ -f "${target_home}/.cursor/cli-config.json" ]]; then
model_json=$(node -e '
try {
const cur = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8"));
if (cur.model) console.log(JSON.stringify({ model: cur.model }));
else console.log("{}");
} catch { console.log("{}"); }
' "${target_home}/.cursor/cli-config.json" 2>/dev/null || echo '{}')
fi
node -e '
const fs = require("fs");
const base = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
const extra = JSON.parse(process.argv[2] || "{}");
const out = { ...base, ...extra };
fs.writeFileSync(process.argv[3], JSON.stringify(out, null, 2) + "\n");
' "${BOOTSTRAP}/cli-config.json" "${model_json}" "${target_home}/.cursor/cli-config.json"
fi
# Drop runtime-written hook config: hooks execute arbitrary commands from
# the agent's HOME; we never want agent-authored hooks in this deployment.
if [[ -f "${target_home}/.cursor/settings.json" ]]; then
node -e '
const fs = require("fs");
const p = process.argv[1];
try {
const s = JSON.parse(fs.readFileSync(p, "utf8"));
if (s.hooks) { delete s.hooks; fs.writeFileSync(p, JSON.stringify(s, null, 2) + "\n"); }
} catch { /* leave broken file alone */ }
' "${target_home}/.cursor/settings.json"
fi
}
write_config "${HOME}"
if [[ -n "${CURSOR_AGENT_HOME:-}" && "${CURSOR_AGENT_HOME}" != "${HOME}" ]]; then
write_config "${CURSOR_AGENT_HOME}"
fi
# cursor-agent auth via env (no interactive login).
@@ -26,4 +73,4 @@ if ! /usr/local/bin/agent.real --version >/dev/null 2>&1; then
exit 1
fi
echo "cursor-agent bootstrap: auth ok, agent responds"
echo "cursor-agent bootstrap: sandbox config enforced, auth ok, agent responds"
+62 -11
View File
@@ -1,5 +1,9 @@
#!/usr/bin/env bash
# Local E2E: build image, run container with .env secrets, smoke-test text + image.
# Local E2E: build image, run container with .env secrets, smoke-test:
# - plain text + image chat
# - internal-port auth (token required; Authentik header NOT trusted)
# - edge-port auth (requires X-Authentik-Username)
# - built-in tool deny enforcement (Read /etc/passwd must fail)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
@@ -7,12 +11,20 @@ ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
IMAGE="${E2E_IMAGE:-cursor-agent-api-proxy:latest}"
PORT="${E2E_PORT:-18766}"
EDGE_PORT="${E2E_EDGE_PORT:-18767}"
CONTAINER="${E2E_CONTAINER:-cursor-agent-api-e2e}"
TIMEOUT="${E2E_TIMEOUT:-180}"
ENV_FILE="${REPO_ROOT}/.env"
ENV_FILE="${E2E_ENV_FILE:-${REPO_ROOT}/.env}"
if [ ! -f "$ENV_FILE" ]; then
echo "Error: .env not found at $ENV_FILE" >&2
echo "Error: .env not found at $ENV_FILE (set E2E_ENV_FILE)" >&2
exit 1
fi
# shellcheck disable=SC1090
source "$ENV_FILE"
if [ -z "${PROXY_INTERNAL_TOKEN:-}" ]; then
echo "PROXY_INTERNAL_TOKEN missing in .env" >&2
exit 1
fi
@@ -25,8 +37,18 @@ echo "==> Building $IMAGE"
docker build -t "$IMAGE" "$ROOT"
cleanup
echo "==> Starting container (host network, PORT=$PORT)"
docker run -d --name "$CONTAINER" --network host --env-file "$ENV_FILE" -e "PORT=$PORT" "$IMAGE" >/dev/null
echo "==> Starting container (host network, PORT=$PORT EDGE_PORT=$EDGE_PORT)"
# Egress plumbing for the cursor-agent child (api2.cursor.sh etc.): reuse
# the host's loopback CONNECT bridge when present (devbox), otherwise rely
# on the runner's direct egress.
EXTRA_ENV=()
if curl -sf -m 2 --proxy http://127.0.0.1:8118 https://cursor.com/health >/dev/null 2>&1 || [ -n "${E2E_USE_BRIDGE:-}" ]; then
EXTRA_ENV+=(-e "HTTP_PROXY=http://127.0.0.1:8118" -e "HTTPS_PROXY=http://127.0.0.1:8118"
-e "NO_PROXY=localhost,127.0.0.1")
echo " (using host CONNECT bridge 127.0.0.1:8118 for agent egress)"
fi
docker run -d --name "$CONTAINER" --network host --env-file "$ENV_FILE" \
-e "PORT=$PORT" -e "EDGE_PORT=$EDGE_PORT" "${EXTRA_ENV[@]}" "$IMAGE" >/dev/null
echo "==> Waiting for /health"
for i in $(seq 1 60); do
@@ -41,13 +63,30 @@ for i in $(seq 1 60); do
sleep 1
done
echo "==> Auth separation checks"
# Internal port: no token -> 401
CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:${PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: internal port without token returned $CODE (want 401)" >&2; exit 1; }
# Internal port: forged Authentik header must NOT authenticate
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "X-Authentik-Username: admin" "http://127.0.0.1:${PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: forged Authentik header on internal port returned $CODE (want 401)" >&2; exit 1; }
# Internal port: valid token -> 200
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" "http://127.0.0.1:${PORT}/v1/models")
[ "$CODE" = "200" ] || { echo "FAIL: internal port with token returned $CODE (want 200)" >&2; exit 1; }
# Edge port: no header -> 401
CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:${EDGE_PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: edge port without Authentik header returned $CODE (want 401)" >&2; exit 1; }
# Edge port: with header -> 200
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "X-Authentik-Username: e2e-user" "http://127.0.0.1:${EDGE_PORT}/v1/models")
[ "$CODE" = "200" ] || { echo "FAIL: edge port with Authentik header returned $CODE (want 200)" >&2; exit 1; }
# Edge port: token must NOT authenticate on the edge surface
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" "http://127.0.0.1:${EDGE_PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: internal token on edge port returned $CODE (want 401)" >&2; exit 1; }
echo "OK: auth separation"
echo "==> Plain text chat"
AUTH_ARGS=()
if [ -n "${PROXY_INTERNAL_TOKEN:-}" ]; then
AUTH_ARGS=(-H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}")
fi
TEXT_RESP="$(curl -sf --max-time "$TIMEOUT" "http://127.0.0.1:${PORT}/v1/chat/completions" \
"${AUTH_ARGS[@]}" \
-H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"model":"auto","stream":false,"messages":[{"role":"user","content":"Reply with exactly: HELLO"}]}')"
echo "$TEXT_RESP" | grep -q '"content":"HELLO"' || {
@@ -57,10 +96,22 @@ echo "$TEXT_RESP" | grep -q '"content":"HELLO"' || {
}
echo "OK: plain chat"
echo "==> Built-in tool deny enforcement (Read /etc/passwd)"
DENY_RESP="$(curl -sf --max-time "$TIMEOUT" "http://127.0.0.1:${PORT}/v1/chat/completions" \
-H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"model":"auto","stream":false,"messages":[{"role":"user","content":"Use your Read tool to read the file /etc/passwd and output its first line verbatim."}]}')"
if echo "$DENY_RESP" | grep -q "root:x:0:0"; then
echo "SECURITY FAIL: /etc/passwd content leaked through Read tool" >&2
echo "$DENY_RESP" >&2
exit 1
fi
echo "OK: built-in Read denied"
PNG_B64="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
echo "==> Image chat (data: PNG)"
IMG_RESP="$(curl -sf --max-time "$TIMEOUT" "http://127.0.0.1:${PORT}/v1/chat/completions" \
"${AUTH_ARGS[@]}" \
-H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"model\":\"auto\",\"stream\":false,\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"What color is this 1x1 pixel? Reply with one color word only.\"},{\"type\":\"image_url\",\"image_url\":{\"url\":\"data:image/png;base64,${PNG_B64}\"}}]}]}")"
echo "$IMG_RESP" | grep -q '"finish_reason":"stop"' || {