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:
+42
-18
@@ -36,6 +36,7 @@ import {
|
||||
import { textChunk, toolCallChunk, finishChunk, nonStreamResponse } from "./stream-translator.mjs";
|
||||
|
||||
const PORT = parseInt(process.env.PORT || "4646", 10);
|
||||
const EDGE_PORT = parseInt(process.env.EDGE_PORT || "4647", 10);
|
||||
const SHUTDOWN_TIMEOUT_MS = parseInt(process.env.SHUTDOWN_TIMEOUT_MS || "10000", 10);
|
||||
const MAX_BODY_BYTES = parseInt(process.env.PROXY_MAX_BODY_BYTES || String(24 * 1024 * 1024), 10);
|
||||
const INTERNAL_TOKEN = process.env.PROXY_INTERNAL_TOKEN || "";
|
||||
@@ -91,26 +92,34 @@ function isPublicPath(pathname) {
|
||||
}
|
||||
|
||||
/**
|
||||
* When PROXY_INTERNAL_TOKEN is set, require it for API routes unless the
|
||||
* request was already authenticated by an Authentik proxy outpost.
|
||||
* Internal port (4646): in-cluster / mesh callers. ONLY PROXY_INTERNAL_TOKEN
|
||||
* is accepted (Bearer or X-Proxy-Internal-Token). The Authentik trust
|
||||
* header is NOT honored here — it is trivially forgeable by any in-cluster
|
||||
* client and must never grant access to the token-checked surface.
|
||||
*/
|
||||
function isAuthorized(req) {
|
||||
function isAuthorizedInternal(req) {
|
||||
if (!INTERNAL_TOKEN) return true;
|
||||
|
||||
const url = new URL(req.url, "http://internal");
|
||||
if (isPublicPath(url.pathname)) return true;
|
||||
|
||||
if (req.headers["x-authentik-username"]) return true;
|
||||
|
||||
const bearer = extractBearerToken(req);
|
||||
if (bearer === INTERNAL_TOKEN) return true;
|
||||
|
||||
const headerToken = req.headers["x-proxy-internal-token"];
|
||||
if (typeof headerToken === "string" && headerToken === INTERNAL_TOKEN) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edge port (4647): reached only through the Authentik proxy outpost, which
|
||||
* authenticates the user and injects X-Authentik-Username. NetworkPolicy
|
||||
* restricts this port to the outpost; requiring the header adds defense in
|
||||
* depth (a direct in-cluster hit without the outpost cannot pass).
|
||||
*/
|
||||
function isAuthorizedEdge(req) {
|
||||
const url = new URL(req.url, "http://internal");
|
||||
if (isPublicPath(url.pathname)) return true;
|
||||
return typeof req.headers["x-authentik-username"] === "string" && req.headers["x-authentik-username"].length > 0;
|
||||
}
|
||||
|
||||
function sendUnauthorized(res) {
|
||||
sendJson(res, 401, {
|
||||
error: {
|
||||
@@ -125,8 +134,8 @@ function sendUnauthorized(res) {
|
||||
// Route handlers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function handleModels(req, res) {
|
||||
if (!isAuthorized(req)) {
|
||||
async function handleModels(req, res, edge) {
|
||||
if (!(edge ? isAuthorizedEdge(req) : isAuthorizedInternal(req))) {
|
||||
sendUnauthorized(res);
|
||||
return;
|
||||
}
|
||||
@@ -141,8 +150,8 @@ function handleHealth(_req, res) {
|
||||
sendJson(res, 200, { status: "ok", timestamp: new Date().toISOString() });
|
||||
}
|
||||
|
||||
async function handleChatCompletions(req, res) {
|
||||
if (!isAuthorized(req)) {
|
||||
async function handleChatCompletions(req, res, edge) {
|
||||
if (!(edge ? isAuthorizedEdge(req) : isAuthorizedInternal(req))) {
|
||||
sendUnauthorized(res);
|
||||
return;
|
||||
}
|
||||
@@ -409,7 +418,10 @@ function collectSessionTurn(session, blocks) {
|
||||
// Server bootstrap & graceful shutdown
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
// Shared request handler. `edge` selects the auth surface: edge traffic is
|
||||
// pre-authenticated by the Authentik outpost, internal traffic must present
|
||||
// PROXY_INTERNAL_TOKEN.
|
||||
async function requestHandler(req, res) {
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
||||
@@ -419,12 +431,13 @@ const server = createServer(async (req, res) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const edge = req.socket.localPort === EDGE_PORT;
|
||||
const url = new URL(req.url, "http://internal");
|
||||
try {
|
||||
if (req.method === "GET" && url.pathname === "/v1/models") {
|
||||
await handleModels(req, res);
|
||||
await handleModels(req, res, edge);
|
||||
} else if (req.method === "POST" && url.pathname === "/v1/chat/completions") {
|
||||
await handleChatCompletions(req, res);
|
||||
await handleChatCompletions(req, res, edge);
|
||||
} else if (req.method === "GET" && (url.pathname === "/health" || url.pathname === "/")) {
|
||||
handleHealth(req, res);
|
||||
} else {
|
||||
@@ -442,10 +455,20 @@ const server = createServer(async (req, res) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const server = createServer(requestHandler);
|
||||
const edgeServer = createServer(requestHandler);
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.error(`[server] cursor-agent-api-proxy (ACP) listening on :${PORT}`);
|
||||
console.error(`[server] cursor-agent-api-proxy (ACP) internal :${PORT} (token-gated)`);
|
||||
});
|
||||
|
||||
// Edge listener: separate server instance on EDGE_PORT. Requests there are
|
||||
// pre-authenticated by the Authentik outpost in front of them (defense in
|
||||
// depth: header required + NetworkPolicy restricts to the outpost).
|
||||
edgeServer.listen(EDGE_PORT, () => {
|
||||
console.error(`[server] edge :${EDGE_PORT} (authentik outpost only)`);
|
||||
});
|
||||
|
||||
let shuttingDown = false;
|
||||
@@ -454,6 +477,7 @@ async function shutdown(signal) {
|
||||
shuttingDown = true;
|
||||
console.error(`[server] ${signal} received, shutting down...`);
|
||||
server.close();
|
||||
edgeServer.close();
|
||||
const force = setTimeout(() => {
|
||||
console.error("[server] shutdown timeout, forcing exit");
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user