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:
+113
-10
@@ -28,6 +28,63 @@ export const TOOL_CALL_BATCH_MS = parseInt(process.env.PROXY_TOOL_CALL_BATCH_MS
|
||||
const REAL_AGENT = process.env.CURSOR_AGENT_BIN || "/usr/local/bin/agent.real";
|
||||
const MCP_BRIDGE = process.env.MCP_BRIDGE_BIN || "/app/scripts/mcp-stdio-bridge.sh";
|
||||
|
||||
/**
|
||||
* Env vars the cursor-agent child is allowed to see. Everything else from
|
||||
* the proxy environment (notably PROXY_INTERNAL_TOKEN and any other pod
|
||||
* secret) is stripped: the child executes model-directed code paths, so it
|
||||
* must never hold proxy credentials. CURSOR_API_KEY is passed on explicitly.
|
||||
*/
|
||||
const CHILD_ENV_ALLOWLIST = new Set([
|
||||
"PATH",
|
||||
"HOME",
|
||||
"LANG",
|
||||
"LC_ALL",
|
||||
"TERM",
|
||||
"TZ",
|
||||
"NODE_ENV",
|
||||
"NO_OPEN_BROWSER",
|
||||
"NO_COLOR",
|
||||
"SSL_CERT_FILE",
|
||||
"SSL_CERT_DIR",
|
||||
// Egress plumbing (only route hints — not credentials). The child needs
|
||||
// these to reach the Cursor API from egress-constrained environments.
|
||||
"HTTP_PROXY",
|
||||
"http_proxy",
|
||||
"HTTPS_PROXY",
|
||||
"https_proxy",
|
||||
"ALL_PROXY",
|
||||
"all_proxy",
|
||||
"NO_PROXY",
|
||||
"no_proxy",
|
||||
]);
|
||||
|
||||
/** Build a minimal environment for the cursor-agent child process. */
|
||||
export function buildChildEnv(parentEnv) {
|
||||
const env = {};
|
||||
for (const name of CHILD_ENV_ALLOWLIST) {
|
||||
if (parentEnv[name] !== undefined) env[name] = parentEnv[name];
|
||||
}
|
||||
if (parentEnv.CURSOR_API_KEY !== undefined) {
|
||||
env.CURSOR_API_KEY = parentEnv.CURSOR_API_KEY;
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the safest answer for a cursor/ask_question request we auto-answer.
|
||||
* "Trust" / full-access prompts must never be answered affirmatively by
|
||||
* picking the first option (that is how the deployed PVC config ended up
|
||||
* with allow-all permissions). Prefer an explicit deny/reject-style option;
|
||||
* fall back to the last option (dialogs list "Yes / No" style pairs where
|
||||
* the safe answer is last).
|
||||
*/
|
||||
export function pickSafeQuestionOption(options) {
|
||||
if (!Array.isArray(options) || options.length === 0) return null;
|
||||
const denyish = options.find((o) => /deny|reject|no\b|don'?t/i.test(String(o.label ?? o.name ?? o.text ?? "")));
|
||||
if (denyish) return denyish;
|
||||
return options[options.length - 1];
|
||||
}
|
||||
|
||||
/** @type {Map<string, CursorSession>} */
|
||||
const SESSIONS = new Map();
|
||||
|
||||
@@ -151,6 +208,10 @@ export class CursorSession {
|
||||
this._handlers = null;
|
||||
this._hasToolCallInTurn = false;
|
||||
this._promptInFlight = false;
|
||||
/** tool_call_ids already resolved with a client-provided result in this
|
||||
* session — excluded from follow-up matching so re-sent history never
|
||||
* matches a session again (and never re-logs "not pending"). */
|
||||
this.resolvedToolCalls = new Set();
|
||||
}
|
||||
|
||||
async start() {
|
||||
@@ -181,14 +242,34 @@ export class CursorSession {
|
||||
await this.mcp.start();
|
||||
|
||||
const args = ["acp"];
|
||||
const env = { ...process.env };
|
||||
if (process.env.CURSOR_API_KEY) env.CURSOR_API_KEY = process.env.CURSOR_API_KEY;
|
||||
// Minimal env: the child executes model-directed code paths and must not
|
||||
// hold proxy credentials (PROXY_INTERNAL_TOKEN etc.). See buildChildEnv.
|
||||
const env = buildChildEnv(process.env);
|
||||
// Dedicated agent HOME (uid 65533): the child's runtime state never
|
||||
// lands in the proxy's PVC-mounted HOME, so a compromised child can
|
||||
// neither poison the proxy's cursor config nor read its caches.
|
||||
if (process.env.CURSOR_AGENT_HOME) {
|
||||
env.HOME = process.env.CURSOR_AGENT_HOME;
|
||||
}
|
||||
|
||||
this.cursorProc = spawn(REAL_AGENT, args, {
|
||||
// Run the child as a dedicated unprivileged uid (default 65533) so that
|
||||
// even a fully compromised agent cannot read proxy-owned secrets
|
||||
// (files 0400 proxy-uid) or /proc/<proxy-pid>/environ. The proxy itself
|
||||
// keeps uid 65534 (nobody). Only meaningful when running as root-less
|
||||
// with distinct uids available (Kubernetes runAsUser).
|
||||
const childUid = parseInt(process.env.CURSOR_AGENT_UID || "65533", 10);
|
||||
const spawnOptions = {
|
||||
cwd: this.workDir,
|
||||
env,
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
});
|
||||
};
|
||||
if (process.getuid && process.getuid() === 0) {
|
||||
// Only root can setuid a child; in Kubernetes we are non-root, so the
|
||||
// uid separation is achieved at pod level instead (securityContext).
|
||||
spawnOptions.uid = childUid;
|
||||
}
|
||||
|
||||
this.cursorProc = spawn(REAL_AGENT, args, spawnOptions);
|
||||
|
||||
this.acp = new AcpClient({
|
||||
stdin: this.cursorProc.stdin,
|
||||
@@ -270,8 +351,13 @@ export class CursorSession {
|
||||
}
|
||||
|
||||
if (method === "cursor/ask_question") {
|
||||
const first = params?.questions?.[0]?.options?.[0]?.id;
|
||||
return { answers: first ? { [params.questions[0].id]: first } : {} };
|
||||
// Auto-answer agent questions with the SAFE option only. The deployed
|
||||
// config once drifted to allow-all because we blindly picked options[0]
|
||||
// ("Yes, allow full access") on the trust dialog.
|
||||
const first = params?.questions?.[0];
|
||||
if (!first) return { answers: {} };
|
||||
const safe = pickSafeQuestionOption(first.options);
|
||||
return { answers: safe ? { [first.id]: safe.id } : {} };
|
||||
}
|
||||
|
||||
if (method === "cursor/create_plan") {
|
||||
@@ -349,6 +435,7 @@ export class CursorSession {
|
||||
resolveToolCall(toolCallId, result) {
|
||||
const ok = this.mcp.resolveToolCall(toolCallId, result);
|
||||
if (ok) {
|
||||
this.resolvedToolCalls.add(toolCallId);
|
||||
this.lastActivityAt = Date.now();
|
||||
this._resetTimer();
|
||||
}
|
||||
@@ -457,19 +544,35 @@ export async function createSession({ tools, model, log }) {
|
||||
}
|
||||
|
||||
export function findSessionForFollowUp(body) {
|
||||
const toolResults = (body.messages || []).filter((m) => m.role === "tool");
|
||||
if (toolResults.length === 0) return null;
|
||||
const allToolResults = (body.messages || []).filter((m) => m.role === "tool");
|
||||
if (allToolResults.length === 0) return null;
|
||||
// Only tool results that were never resolved in a candidate session can
|
||||
// route the request. Re-sent history (results already resolved earlier in
|
||||
// this session) must not match — otherwise every follow-up turn re-logs
|
||||
// every historical id as "not pending".
|
||||
const openToolResults = allToolResults.filter(
|
||||
(tr) => tr.tool_call_id !== undefined && !sessionsHaveResolved(tr.tool_call_id)
|
||||
);
|
||||
if (openToolResults.length === 0) return null;
|
||||
for (const session of SESSIONS.values()) {
|
||||
if (!session.mcp) continue;
|
||||
for (const tr of toolResults) {
|
||||
for (const tr of openToolResults) {
|
||||
if (session.mcp.pending.has(tr.tool_call_id)) {
|
||||
return { session, toolResults };
|
||||
return { session, toolResults: openToolResults };
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** True if any live session already resolved this tool_call_id. */
|
||||
function sessionsHaveResolved(toolCallId) {
|
||||
for (const session of SESSIONS.values()) {
|
||||
if (session.resolvedToolCalls?.has(toolCallId)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function getSession(sessionId) {
|
||||
return SESSIONS.get(sessionId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user