From 7890ce11a7c22c267c7bdae23b364c048bc997a6 Mon Sep 17 00:00:00 2001 From: Josh Cox Date: Sat, 5 Dec 2026 07:17:56 -0600 Subject: [PATCH] fix(adapter-utils): handle client socket errors in sandbox allowlist proxy (#22501) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The local process sandbox runs agents with a network allowlist > - The sandbox proxy serves CONNECT tunnels on a Unix socket > - A client reset on a CONNECT tunnel emits an error on the client socket > - The client socket has no error listener, so the error is unhandled > - An unhandled error stops the whole server process > - This pull request adds error listeners to the proxy sockets > - The benefit is that a client reset ends only that tunnel, not the server ## Linked Issues or Issue Description Fixes #22394 ## What Changed - The CONNECT handler now listens for errors on the client socket and destroys the upstream socket when an error occurs (`packages/adapter-utils/src/local-process-sandbox.ts`) - The proxy `connection` handler now listens for errors on each accepted socket and destroys that socket when an error occurs - A new regression test resets a client socket on an allowed CONNECT tunnel and on a denied CONNECT request, then confirms the proxy still serves a plain request ## Verification - `vitest run packages/adapter-utils/src/local-process-sandbox.test.ts`: 11 passed, 5 skipped, no unhandled errors - `tsc --noEmit` in `packages/adapter-utils`: clean - Negative control: with only the fix reverted, the new test raises the exact unhandled `read ECONNRESET` error from the issue (`Pipe.onStreamRead`, errno -104); with the fix applied, no unhandled error occurs - The full server typecheck is not run locally because it exceeds local memory; CI is the arbiter for that check - The embedded-Postgres suites self-skip in this sandbox because it runs as root; CI is the arbiter for those suites ## Risks - Low risk. The change adds two error listeners and one regression test. It changes no proxy policy, no allowlist rule, and no public API. - The plain HTTP request path is unchanged. This pull request covers only the CONNECT handler and the `connection` handler named in the issue. ## Model Used - Meta Muse Spark 1.3 ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] I will address all Greptile and reviewer comments before requesting merge Signed-off-by: Josh Cox Co-authored-by: Review --- .../src/local-process-sandbox.test.ts | 69 +++++++++++++++++++ .../src/local-process-sandbox.ts | 2 + 2 files changed, 71 insertions(+) diff --git a/packages/adapter-utils/src/local-process-sandbox.test.ts b/packages/adapter-utils/src/local-process-sandbox.test.ts index cdb58cd923c..2b57ad1f8f9 100644 --- a/packages/adapter-utils/src/local-process-sandbox.test.ts +++ b/packages/adapter-utils/src/local-process-sandbox.test.ts @@ -235,6 +235,75 @@ describe("local process sandbox", () => { } }); + it.runIf(process.platform === "linux")("survives client resets on CONNECT tunnels and denied connections", async () => { + const workspace = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-network-proxy-reset-")); + cleanup.push(workspace); + const payload = Buffer.alloc(1024 * 1024, "x"); + const plainServer = http.createServer((_request, response) => response.end("allowed-response")); + await new Promise((resolve) => plainServer.listen(0, "127.0.0.1", resolve)); + const plainAddress = plainServer.address(); + if (!plainAddress || typeof plainAddress === "string") throw new Error("Expected TCP test server address."); + const tunnelSockets = new Set(); + const tunnelServer = net.createServer((socket) => { + tunnelSockets.add(socket); + socket.on("error", () => {}); + socket.on("close", () => tunnelSockets.delete(socket)); + socket.write(payload); + }); + await new Promise((resolve) => tunnelServer.listen(0, "127.0.0.1", resolve)); + const tunnelAddress = tunnelServer.address(); + if (!tunnelAddress || typeof tunnelAddress === "string") throw new Error("Expected TCP test server address."); + const target = await buildLocalProcessSandboxSpawnTarget({ + executable: process.execPath, + args: ["-e", "process.exit(0)"], + cwd: workspace, + options: { + workspaceDir: workspace, + filesystemScope: "workspace", + networkScope: "allowlist", + networkAllowlist: [`127.0.0.1:${plainAddress.port}`, `127.0.0.1:${tunnelAddress.port}`], + }, + }); + const delimiterIndex = target.args.indexOf("--"); + const socketPath = target.args[delimiterIndex + 3]; + + const resetAfterConnect = (requestLine: string) => new Promise((resolve) => { + const socket = net.createConnection(socketPath, () => { + socket.write(requestLine); + setTimeout(() => { + socket.destroy(); + resolve(); + }, 300); + }); + socket.on("error", () => {}); + }); + + try { + await resetAfterConnect(`CONNECT 127.0.0.1:${tunnelAddress.port} HTTP/1.1\r\nHost: 127.0.0.1:${tunnelAddress.port}\r\n\r\n`); + await resetAfterConnect("CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n"); + await new Promise((resolve) => setTimeout(resolve, 300)); + const response = await new Promise<{ status: number; body: string }>((resolve, reject) => { + const outgoing = http.request({ + socketPath, + path: `http://127.0.0.1:${plainAddress.port}/canary`, + headers: { host: `127.0.0.1:${plainAddress.port}` }, + }, (incoming) => { + let body = ""; + incoming.on("data", (chunk) => { body += chunk; }); + incoming.on("end", () => resolve({ status: incoming.statusCode ?? 0, body })); + }); + outgoing.on("error", reject); + outgoing.end(); + }); + expect(response).toEqual({ status: 200, body: "allowed-response" }); + } finally { + await target.cleanup?.(); + for (const socket of tunnelSockets) socket.destroy(); + await new Promise((resolve) => tunnelServer.close(() => resolve())); + await new Promise((resolve) => plainServer.close(() => resolve())); + } + }); + it.runIf(process.platform === "linux")("always permits trusted Paperclip control-plane URLs", async () => { const workspace = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-network-trusted-")); cleanup.push(workspace); diff --git a/packages/adapter-utils/src/local-process-sandbox.ts b/packages/adapter-utils/src/local-process-sandbox.ts index 95f6227a903..d13729ec1dd 100644 --- a/packages/adapter-utils/src/local-process-sandbox.ts +++ b/packages/adapter-utils/src/local-process-sandbox.ts @@ -287,11 +287,13 @@ async function startNetworkAllowlistProxy( clientSocket.pipe(upstream); }); upstream.on("error", () => clientSocket.destroy()); + clientSocket.on("error", () => upstream.destroy()); clientSocket.on("close", () => upstream.destroy()); }); const sockets = new Set(); server.on("connection", (socket) => { sockets.add(socket); + socket.on("error", () => socket.destroy()); socket.on("close", () => sockets.delete(socket)); }); await new Promise((resolve, reject) => {