Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(rpc-proxy): keep mcp-host 403 codes reserved and stop replaying mutating POSTs
Alfredo Lopezce53e25ec667
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.tsindex f9d12294a..db572bcd7 100644--- a/external-rest-api/src/releaseManifest.ts+++ b/external-rest-api/src/releaseManifest.ts@@ -9,7 +9,7 @@ export type ReleaseManifest = {export const releaseManifest: ReleaseManifest = {releaseId: 'v0.10.0',externalRestApiVersion: '0.1.84',- rpcProxyVersion: '0.1.101',+ rpcProxyVersion: '0.1.102',desktopVersion: '0.10.0',minimumDesktopVersion: '0.1.252',}diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.jsonindex 1a855a2d2..657a6aee4 100644--- a/rpc-proxy/package-lock.json+++ b/rpc-proxy/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-rpc-proxy",- "version": "0.1.101",+ "version": "0.1.102","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-rpc-proxy",- "version": "0.1.101",+ "version": "0.1.102","dependencies": {"cors": "^2.8.5","express": "^4.21.2",diff --git a/rpc-proxy/package.json b/rpc-proxy/package.jsonindex 38d2b00ce..88a2121f3 100644--- a/rpc-proxy/package.json+++ b/rpc-proxy/package.json@@ -1,6 +1,6 @@{"name": "clerum-rpc-proxy",- "version": "0.1.101",+ "version": "0.1.102","description": "External-facing RPC proxy for tenant-scoped MCP access","main": "dist/main.js","engines": {diff --git a/rpc-proxy/src/__tests__/host-access-denial-code.route.test.ts b/rpc-proxy/src/__tests__/host-access-denial-code.route.test.tsindex 1c38aca6c..053cd2b8f 100644--- a/rpc-proxy/src/__tests__/host-access-denial-code.route.test.ts+++ b/rpc-proxy/src/__tests__/host-access-denial-code.route.test.ts@@ -296,6 +296,27 @@ describe('passthrough and non-authorization responses are left untouched', () =>expect(JSON.parse(res.text)).toEqual({ error: 'artifact not readable' })})+ it('an mcp-host 403 text body naming a reserved code is relayed verbatim', async () => {+ hostAnswer = { status: 403, body: 'host_access_revoked' }++ const res = await send(DENYING_ROUTES.find(route => route.label === 'approve')!)++ expect(hostCalls).toHaveLength(1)+ expect(res.status).toBe(403)+ expect(res.text).toBe('host_access_revoked')+ })++ it('a non-403 mcp-host body is relayed verbatim even when it carries a reserved code', async () => {+ const body = JSON.stringify({ ok: true, code: 'host_access_revoked' })+ hostAnswer = { status: 200, body }++ const res = await send(DENYING_ROUTES.find(route => route.label === 'approve')!)++ expect(hostCalls).toHaveLength(1)+ expect(res.status).toBe(200)+ expect(res.text).toBe(body)+ })+it('control-api 401 is not a Host denial and gets no `code`', async () => {controlApiAnswer = { status: 401, body: { error: 'token rejected' } }authTokenMock.verifyRpcToken.mockReturnValue({ ...CLAIMS })@@ -306,3 +327,95 @@ describe('passthrough and non-authorization responses are left untouched', () =>expect(res.body).not.toHaveProperty('code')})})++// PR #849 ADV-SEC-1. Desktop reads the `code` of any 403 JSON body as+// rpc-proxy's own verdict, so an mcp-host 403 carrying a reserved code would+// forge a confirmed revocation (or denial). Every route that relays an mcp-host+// body must drop that field while keeping the status and the other fields.+describe('an mcp-host 403 never reaches the client with a reserved Host-access code', () => {+ type PassthroughCase = RouteCase & {+ /** The mcp-host path the route forwards to (witness the host was called). */+ hostPath: string+ /** A route that answers rpc-proxy's own 403 (cancel answers its own denial as 404). */+ ownDenial?: RouteCase+ }++ const byLabel = (label: string) => DENYING_ROUTES.find(route => route.label === label)!++ // Every site in rpc.ts that relays an mcp-host response body.+ const PASSTHROUGH_ROUTES: PassthroughCase[] = [+ { ...byLabel('approve'), hostPath: '/v1/runtime/approvals/approve' },+ { ...byLabel('deny'), hostPath: '/v1/runtime/approvals/deny' },+ { ...byLabel('sessions'), hostPath: '/v1/runtime/sessions' },+ {+ ...byLabel('session messages'),+ hostPath: '/v1/runtime/sessions/agent-a/chat-1/messages',+ },+ {+ ...byLabel('context breakdown'),+ hostPath: '/v1/runtime/sessions/agent-a/chat-1/context-breakdown',+ },+ { ...byLabel('session rename'), hostPath: '/v1/runtime/sessions/agent-a/chat-1/name' },+ { ...byLabel('models'), hostPath: '/v1/runtime/models' },+ { ...byLabel('set model'), hostPath: '/v1/runtime/model' },+ {+ label: 'task cancel',+ method: 'post',+ path: '/rpc/hosts/chatllm/tasks/task-1/cancel',+ hostPath: '/v1/runtime/tasks/task-1/cancel',+ ownDenial: byLabel('approve'),+ },+ { ...byLabel('artifacts list'), hostPath: '/v1/runtime/artifacts' },+ {+ ...byLabel('artifact download'),+ hostPath: '/v1/runtime/artifacts/report.pdf/download',+ },+ ]++ // control-api reasons that make rpc-proxy itself emit each reserved code.+ const OWN_DENIAL_REASON = {+ host_access_revoked: 'directory_grant_missing',+ host_access_denied: 'host_disabled',+ } as const++ const cases = PASSTHROUGH_ROUTES.flatMap(route =>+ (['host_access_revoked', 'host_access_denied'] as const).map(code => ({ ...route, code }))+ )++ it.each(cases)('$label: an upstream 403 with code=$code loses the code', async route => {+ controlApiAnswer = {+ status: 200,+ body: {+ userId: CLAIMS.sub,+ hostRef: 'chatllm',+ url: 'http://chatllm.mcp-host.svc.cluster.local:8080',+ },+ }+ hostAnswer = {+ status: 403,+ body: JSON.stringify({ error: 'forged by host', code: route.code, detail: 'kept' }),+ }++ const forged = await send(route)++ // Witness: control-api authorized the call and the host answered this 403.+ expect(controlApiCalls).toHaveLength(1)+ expect(hostCalls).toHaveLength(1)+ expect(new URL(hostCalls[0]!).pathname).toBe(route.hostPath)+ expect(forged.status).toBe(403)+ expect(JSON.parse(forged.text)).toEqual({ error: 'forged by host', detail: 'kept' })++ // Witness: rpc-proxy's own denial still carries the same reserved code.+ controlApiAnswer = {+ status: 403,+ body: CONTROL_API_403_BODY,+ headers: { [REASON_HEADER]: OWN_DENIAL_REASON[route.code] },+ }+ const own = await send(route.ownDenial ?? route)++ expect(controlApiCalls).toHaveLength(2)+ expect(hostCalls).toHaveLength(1)+ expect(own.status).toBe(403)+ expect(own.body).toEqual({ error: DENIED_ERROR, code: route.code })+ })+})diff --git a/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.ts b/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.tsindex aeca45956..dbc4ac9c0 100644--- a/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.ts+++ b/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.ts@@ -6,7 +6,7 @@ import request from 'supertest'import { config } from '../config.js'import { apiErrorHandler } from '../errorHandler.js'import { createRpcRouter } from '../routes/rpc.js'-import { forwardHostMessageToHost } from '../services/mcpHostRestService.js'+import { forwardCancelToHost, forwardHostMessageToHost } from '../services/mcpHostRestService.js'// PR #849 R2-M3 / R1-M12 / R1-M13 / NEW-rpx-1 / NEW-sec-4.//@@ -112,6 +112,54 @@ function dropConnection(req: IncomingMessage): void {req.socket.destroy()}+/**+ * Sends the 200 headers and part of the body, then kills the socket: undici+ * resolves `fetch()` and the body read fails with `terminated` (UND_ERR_SOCKET).+ */+function sendHeadersThenDestroy(req: IncomingMessage, res: ServerResponse): void {+ res.writeHead(200, { 'content-type': 'application/json' })+ res.write('{"applied":', () => {+ setTimeout(() => req.socket.destroy(), 20)+ })+}++// The symbol undici's setGlobalDispatcher writes; Node's fetch reads it per call.+const UNDICI_GLOBAL_DISPATCHER = Symbol.for('undici.globalDispatcher.1')+type UndiciDispatcher = { close(): Promise<void> }+type UndiciAgentClass = new (options: {+ headersTimeout: number+ bodyTimeout: number+}) => UndiciDispatcher++/**+ * Runs `fn` with Node's own undici Agent swapped for one whose header/body+ * timers are 200 ms instead of 300 s, so undici's real UND_ERR_HEADERS_TIMEOUT /+ * UND_ERR_BODY_TIMEOUT errors are produced by the production fetch calls.+ */+async function withShortUndiciTimeouts<T>(fn: () => Promise<T>): Promise<T> {+ const globals = globalThis as unknown as Record<symbol, UndiciDispatcher | undefined>+ if (!globals[UNDICI_GLOBAL_DISPATCHER]) {+ // Node creates its global Agent on the first fetch: make one to a+ // throwaway server so the upstream under test sees no extra request.+ const warmup = createServer((_req, res) => res.end())+ await new Promise<void>(resolve => warmup.listen(0, '127.0.0.1', resolve))+ const { port } = warmup.address() as AddressInfo+ await (await fetch(`http://127.0.0.1:${port}/`)).text()+ await new Promise<void>(resolve => warmup.close(() => resolve()))+ }+ const original = globals[UNDICI_GLOBAL_DISPATCHER]+ if (!original) throw new Error('Node fetch did not install an undici global dispatcher')+ const Agent = original.constructor as UndiciAgentClass+ const shortAgent = new Agent({ headersTimeout: 200, bodyTimeout: 200 })+ globals[UNDICI_GLOBAL_DISPATCHER] = shortAgent+ try {+ return await fn()+ } finally {+ globals[UNDICI_GLOBAL_DISPATCHER] = original+ await shortAgent.close()+ }+}+const original = {upstreamTimeoutMs: config.upstreamTimeoutMs,artifactDownloadTimeoutMs: config.artifactDownloadTimeoutMs,@@ -265,6 +313,92 @@ describe.each(MUTATING_ROUTES)('R2-M3 / R1-M12 $label route deadlines', route =>`POST ${route.upstreamPath}`,])})++ // ADV-HCC-1 / ADV-SEC-3: once the response headers arrived the host has the+ // POST (it may already have applied it), so a socket that dies mid-body is a+ // 502 for this one request, never a down host to wake and re-POST to.+ it('a socket that dies after the response headers answers 502 with exactly one POST', async () => {+ await startUpstream(sendHeadersThenDestroy)++ const res = await post(route).expect(502)++ expect(res.body).toEqual({ error: 'Upstream host unavailable' })+ // Witness: the first attempt reached the upstream and got its headers out.+ expect(seen.map(entry => `${entry.method} ${entry.url}`)).toEqual([+ `POST ${route.upstreamPath}`,+ ])+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()+ })++ it('a wake retry whose body dies after the headers answers 502 without a third POST', async () => {+ config.wakeMaxHoldMs = 4_000+ await startUpstream((req, res, index) => {+ // First attempt: dropped before the headers (host down, wake). Retry:+ // headers, then the socket dies mid-body.+ if (index === 0) dropConnection(req)+ else sendHeadersThenDestroy(req, res)+ })++ const res = await post(route).expect(502)++ expect(res.body).toEqual({ error: 'Upstream host unavailable' })+ // Witness: the pre-headers failure did wake and re-issue once.+ expect(controlApiMock.requestHostWakeFromControlApi).toHaveBeenCalledTimes(1)+ expect(seen.map(entry => entry.url)).toEqual([route.upstreamPath, route.upstreamPath])+ })++ // undici's own header timer fires on a connection the host accepted: a slow+ // host, not a down one. It must answer 504, not wake and re-issue the POST.+ it("undici's headers timeout answers 504 with exactly one POST", async () => {+ await startUpstream(() => {+ // never answers: the upstream holds the POST+ })++ const res = await withShortUndiciTimeouts(() => post(route).expect(504))++ expect(res.body).toEqual({ error: 'Gateway Timeout' })+ expect(seen.map(entry => `${entry.method} ${entry.url}`)).toEqual([+ `POST ${route.upstreamPath}`,+ ])+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()+ })++ it("undici's body timeout after the headers answers 504 with exactly one POST", async () => {+ await startUpstream((_req, res) => {+ res.writeHead(200, { 'content-type': 'application/json' })+ res.write('{"applied":') // then stalls+ })++ const res = await withShortUndiciTimeouts(() => post(route).expect(504))++ expect(res.body).toEqual({ error: 'Gateway Timeout' })+ expect(seen.map(entry => `${entry.method} ${entry.url}`)).toEqual([+ `POST ${route.upstreamPath}`,+ ])+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()+ })+})++describe('ADV-HCC-1 task cancel is not re-issued after the response headers', () => {+ it('a socket that dies after the response headers answers 502 with exactly one POST', async () => {+ serviceMock.forwardCancelToHost.mockImplementation(+ forwardCancelToHost as typeof serviceMock.forwardCancelToHost+ )+ await startUpstream(sendHeadersThenDestroy)++ const res = await request(makeApp())+ .post('/rpc/hosts/chatllm/tasks/task-1/cancel')+ .set('authorization', 'Bearer tok')+ .expect(502)++ expect(res.body).toEqual({ error: 'Upstream host unavailable' })+ // Witness: the real forwardCancelToHost reached the upstream once.+ expect(serviceMock.forwardCancelToHost).toHaveBeenCalledTimes(1)+ expect(seen.map(entry => `${entry.method} ${entry.url}`)).toEqual([+ 'POST /v1/runtime/tasks/task-1/cancel',+ ])+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()+ })})describe('R1-M12 /messages keeps the retry-until-deadline loop (it carries a messageId)', () => {diff --git a/rpc-proxy/src/errorHandler.ts b/rpc-proxy/src/errorHandler.tsindex 20e19f2e1..9d588dc42 100644--- a/rpc-proxy/src/errorHandler.ts+++ b/rpc-proxy/src/errorHandler.ts@@ -1,4 +1,5 @@import type { NextFunction, Request, Response } from 'express'+import { UpstreamBodyReadError } from './services/upstreamBody.js'import { isUpstreamTimeoutError } from './services/wakeAndHold.js'/**@@ -31,6 +32,13 @@ export function apiErrorHandler(return}+ // The upstream answered with headers and its body then failed: a gateway+ // failure on a request the host received, never an rpc-proxy fault (500).+ if (err instanceof UpstreamBodyReadError) {+ res.status(502).json({ error: 'Upstream host unavailable' })+ return+ }+if (isEntityTooLargeError(err)) {res.status(413).json({ error: 'Payload Too Large' })returndiff --git a/rpc-proxy/src/routes/rpc.ts b/rpc-proxy/src/routes/rpc.tsindex d96cc5b5a..b7a0fce97 100644--- a/rpc-proxy/src/routes/rpc.ts+++ b/rpc-proxy/src/routes/rpc.ts@@ -21,6 +21,7 @@ import {type HostAccessDenialCode,isHostAccessDenied,respondHostAccessDenied,+ withoutReservedHostAccessCode,} from '../services/hostAccessDenial.js'import {type HostRuntimeMessageRequest,@@ -36,6 +37,7 @@ import {resolveServerConnectionForUser,validateRpcRequest,} from '../services/mcpProxyService.js'+import { readMutatingResponseBody } from '../services/upstreamBody.js'import {isUpstreamTimeoutError,isWakeEligibleHostError,@@ -114,6 +116,23 @@ function guardedNext(res: ExpressResponse, next: NextFunction, error: unknown):next(error)}+/**+ * The single writer of a relayed mcp-host response body. Every passthrough+ * route answers through here so an upstream 403 can never carry the reserved+ * Host-access `code` that only respondHostAccessDenied may set. `contentType`+ * is set only by the routes that relay the upstream content type.+ */+function sendUpstreamBody(+ res: ExpressResponse,+ status: number,+ body: string,+ contentType?: string+): void {+ res.status(status)+ if (contentType !== undefined) res.type(contentType)+ res.send(withoutReservedHostAccessCode(status, body))+}+/*** A 503 carrying mcp-host's `host_draining` fence is a wake-eligible upstream* error, not a client-visible failure: convert it so the finite routes below@@ -661,10 +680,10 @@ export function createRpcRouter(): Router {body: JSON.stringify(upstreamBody),signal: mutatingCallSignal(timeoutMs),})- const body = await response.text()+ const body = await readMutatingResponseBody(response)const draining = sessionDrainingFence(response, body)if (draining) throw draining- res.status(response.status).send(body)+ sendUpstreamBody(res, response.status, body)}try {await attempt()@@ -725,10 +744,10 @@ export function createRpcRouter(): Router {body: JSON.stringify(upstreamBody),signal: mutatingCallSignal(timeoutMs),})- const body = await response.text()+ const body = await readMutatingResponseBody(response)const draining = sessionDrainingFence(response, body)if (draining) throw draining- res.status(response.status).send(body)+ sendUpstreamBody(res, response.status, body)}try {await attempt()@@ -811,10 +830,12 @@ export function createRpcRouter(): Router {const body = await response.text()const draining = sessionDrainingFence(response, body)if (draining) throw draining- res- .status(response.status)- .type(response.headers.get('content-type') || 'application/json')- .send(body)+ sendUpstreamBody(+ res,+ response.status,+ body,+ response.headers.get('content-type') || 'application/json'+ )}try {await forwardSessionList()@@ -912,10 +933,12 @@ export function createRpcRouter(): Router {const body = await response.text()const draining = sessionDrainingFence(response, body)if (draining) throw draining- res- .status(response.status)- .type(response.headers.get('content-type') || 'application/json')- .send(body)+ sendUpstreamBody(+ res,+ response.status,+ body,+ response.headers.get('content-type') || 'application/json'+ )}try {await forwardTranscript()@@ -988,10 +1011,12 @@ export function createRpcRouter(): Router {const body = await response.text()const draining = sessionDrainingFence(response, body)if (draining) throw draining- res- .status(response.status)- .type(response.headers.get('content-type') || 'application/json')- .send(body)+ sendUpstreamBody(+ res,+ response.status,+ body,+ response.headers.get('content-type') || 'application/json'+ )}try {await attempt()@@ -1081,10 +1106,12 @@ export function createRpcRouter(): Router {})const body = await response.text()- res- .status(response.status)- .type(response.headers.get('content-type') || 'application/json')- .send(body)+ sendUpstreamBody(+ res,+ response.status,+ body,+ response.headers.get('content-type') || 'application/json'+ )} catch (error) {respondUpstreamUnavailable(res, error)}@@ -1134,10 +1161,12 @@ export function createRpcRouter(): Router {const body = await response.text()const draining = sessionDrainingFence(response, body)if (draining) throw draining- res- .status(response.status)- .type(response.headers.get('content-type') || 'application/json')- .send(body)+ sendUpstreamBody(+ res,+ response.status,+ body,+ response.headers.get('content-type') || 'application/json'+ )}try {await attempt()@@ -1198,13 +1227,15 @@ export function createRpcRouter(): Router {body: JSON.stringify(body),signal: mutatingCallSignal(timeoutMs),})- const upstreamBody = await response.text()+ const upstreamBody = await readMutatingResponseBody(response)const draining = sessionDrainingFence(response, upstreamBody)if (draining) throw draining- res- .status(response.status)- .type(response.headers.get('content-type') || 'application/json')- .send(upstreamBody)+ sendUpstreamBody(+ res,+ response.status,+ upstreamBody,+ response.headers.get('content-type') || 'application/json'+ )}try {await attempt()@@ -1327,10 +1358,12 @@ export function createRpcRouter(): Router {const attemptCancel = async (timeoutMs = config.upstreamTimeoutMs) => {const result = await forwardCancelToHost(host, taskId, auth.sub, timeoutMs)if (result.body) {- res- .status(result.status)- .type(result.contentType || 'application/json')- .send(result.body)+ sendUpstreamBody(+ res,+ result.status,+ result.body,+ result.contentType || 'application/json'+ )} else {res.status(result.status).end()}@@ -1403,7 +1436,7 @@ export function createRpcRouter(): Router {const body = await response.text()const draining = sessionDrainingFence(response, body)if (draining) throw draining- res.status(response.status).send(body)+ sendUpstreamBody(res, response.status, body)}try {await attempt()@@ -1499,7 +1532,7 @@ export function createRpcRouter(): Router {const errBody = await response.text()const draining = sessionDrainingFence(response, errBody)if (draining) throw draining- res.status(response.status).send(errBody)+ sendUpstreamBody(res, response.status, errBody)return}const contentType = response.headers.get('content-type') || 'application/octet-stream'diff --git a/rpc-proxy/src/services/__tests__/controlApiRestService.hostAccessDenialBody.test.ts b/rpc-proxy/src/services/__tests__/controlApiRestService.hostAccessDenialBody.test.tsnew file mode 100644index 000000000..3173ce1a1--- /dev/null+++ b/rpc-proxy/src/services/__tests__/controlApiRestService.hostAccessDenialBody.test.ts@@ -0,0 +1,75 @@+import { describe, expect, it } from 'vitest'+import {+ ControlApiHostAccessRejectedError,+ fetchHostConnectionFromControlApi,+} from '../controlApiRestService.js'++// control-api's 403 for a Host-access denial carries its reason in a header and+// a fixed body the proxy never parses. The body must still be read to the end:+// an unread response body keeps the upstream connection checked out until the+// garbage collector finds it. The stand-in Response streams its body in chunks+// and reports when the last one was consumed, so "drained" is observed on the+// stream itself, not inferred from the mapped code.++const REASON_HEADER = 'x-host-access-denial-reason'+const CHUNKS = 3++function streamedDenial(reason: string): {+ response: Response+ state: { pulls: number; drained: boolean }+} {+ const state = { pulls: 0, drained: false }+ const encoder = new TextEncoder()+ const body = new ReadableStream<Uint8Array>({+ pull(controller) {+ state.pulls += 1+ controller.enqueue(encoder.encode('{"error":"Forbidden"}'))+ if (state.pulls >= CHUNKS) {+ controller.close()+ state.drained = true+ }+ },+ })+ return {+ response: new Response(body, { status: 403, headers: { [REASON_HEADER]: reason } }),+ state,+ }+}++const BINDING = { runId: 'run-1', sessionId: 'session-1', origin: 'direct_chat' } as const++describe('fetchHostConnectionFromControlApi 403 body', () => {+ it('reads the whole 403 body of a host lookup before returning the denial code', async () => {+ const { response, state } = streamedDenial('team_membership_missing')++ const resolved = await fetchHostConnectionFromControlApi('user-1', 'host-1', 'token', {+ fetchImpl: async () => response,+ })++ // Liveness witness: the reason header was read and mapped, so the 403 branch ran.+ expect(resolved).toEqual({ denied: true, code: 'host_access_revoked' })+ expect(response.bodyUsed).toBe(true)+ expect(state.drained).toBe(true)+ expect(state.pulls).toBe(CHUNKS)+ })++ it('reads the whole 403 body of a direct-run lookup before throwing the rejection', async () => {+ const { response, state } = streamedDenial('host_disabled')++ const error = await fetchHostConnectionFromControlApi('user-1', 'host-1', 'token', {+ directRunBinding: BINDING,+ fetchImpl: async () => response,+ }).then(+ () => {+ throw new Error('expected the direct-run lookup to reject')+ },+ (caught: unknown) => caught+ )++ expect(error).toBeInstanceOf(ControlApiHostAccessRejectedError)+ expect((error as ControlApiHostAccessRejectedError).denialCode).toBe('host_access_denied')+ expect(response.bodyUsed).toBe(true)+ expect(state.drained).toBe(true)+ expect(state.pulls).toBe(CHUNKS)+ })+})diff --git a/rpc-proxy/src/services/__tests__/wakeAndHold.test.ts b/rpc-proxy/src/services/__tests__/wakeAndHold.test.tsindex 654fbfa98..6739003fd 100644--- a/rpc-proxy/src/services/__tests__/wakeAndHold.test.ts+++ b/rpc-proxy/src/services/__tests__/wakeAndHold.test.ts@@ -3,6 +3,7 @@ import type { Response as ExpressResponse } from 'express'import { config } from '../../config.js'import type { ResolvedServerConnection, RpcAccessClaims, RpcScope } from '../../types.js'import type { HostWakeApiResponse } from '../controlApiRestService.js'+import { UpstreamBodyReadError } from '../upstreamBody.js'import {WakeAndHoldCoordinator,type WakeCoordinatorDeps,@@ -426,6 +427,20 @@ describe('host error classification', () => {})expect(isHostDownNetworkError(upstream)).toBe(false)})++ // ADV-HCC-1: the wrapper's message embeds the body failure's text, which can+ // itself read like a down host ("read ECONNRESET", "socket hang up"). The+ // headers arrived, so it is still never a down-host signal.+ it('never classifies a body failure after the headers as a down host', () => {+ const bodyFailure = new Error('read ECONNRESET')+ // Witness: the same failure raised before fetch() resolved IS a down host.+ expect(isHostDownNetworkError(bodyFailure)).toBe(true)++ expect(isHostDownNetworkError(new UpstreamBodyReadError(bodyFailure))).toBe(false)+ expect(isHostDownNetworkError(new UpstreamBodyReadError(new Error('socket hang up')))).toBe(+ false+ )+ })})describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () => {diff --git a/rpc-proxy/src/services/hostAccessDenial.ts b/rpc-proxy/src/services/hostAccessDenial.tsindex bbc2cdc4f..d758ca5b7 100644--- a/rpc-proxy/src/services/hostAccessDenial.ts+++ b/rpc-proxy/src/services/hostAccessDenial.ts@@ -41,6 +41,38 @@ export function isHostAccessDenied<T extends object>(return (resolved as Partial<HostAccessDenial>).denied === true}+const RESERVED_HOST_ACCESS_CODES: ReadonlySet<unknown> = new Set<HostAccessDenialCode>([+ 'host_access_revoked',+ 'host_access_denied',+])++/**+ * The body rpc-proxy may relay for an upstream (mcp-host) response. Desktop+ * trusts the `code` of a 403 JSON body as rpc-proxy's own authorization verdict,+ * so a passthrough 403 must never carry a reserved Host-access code: an mcp-host+ * could otherwise forge a "confirmed revocation" that hides the agent. Such a+ * `code` field is dropped; the status and every other field are kept. A non-403+ * status, a non-JSON body and a JSON value that is not an object pass unchanged+ * (Desktop reads the code only from a parsed JSON object of a 403).+ */+export function withoutReservedHostAccessCode(status: number, body: string): string {+ if (status !== 403) return body+ let parsed: unknown+ try {+ parsed = JSON.parse(body)+ } catch {+ // Not JSON: Desktop cannot read a `code` from it, so it is relayed as is.+ return body+ }+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return body+ const { code, ...rest } = parsed as Record<string, unknown>+ if (!RESERVED_HOST_ACCESS_CODES.has(code)) return body+ console.warn(+ `[RPC_PROXY] dropped reserved host-access code from an upstream 403 body code=${String(code)}`+ )+ return JSON.stringify(rest)+}+/** The single writer of rpc-proxy's own Host authorization 403. */export function respondHostAccessDenied(res: ExpressResponse,diff --git a/rpc-proxy/src/services/mcpHostRestService.ts b/rpc-proxy/src/services/mcpHostRestService.tsindex 48a197389..cbae084a1 100644--- a/rpc-proxy/src/services/mcpHostRestService.ts+++ b/rpc-proxy/src/services/mcpHostRestService.ts@@ -1,6 +1,7 @@import { config } from '../config.js'import type { TraceContextV1 } from '../traceContext.js'import { ResolvedServerConnection } from '../types.js'+import { readMutatingResponseBody } from './upstreamBody.js'/*** Per-MCP-server health row as emitted by mcp-host and forwarded by rpc-proxy.@@ -245,7 +246,9 @@ export async function forwardCancelToHost(signal: abortController.signal,})- const body = await response.text()+ // Cancel is a mutating POST without an idempotency key: a body failure+ // after the headers must not re-issue it through the wake path.+ const body = await readMutatingResponseBody(response)return {status: response.status,body,diff --git a/rpc-proxy/src/services/upstreamBody.ts b/rpc-proxy/src/services/upstreamBody.tsnew file mode 100644index 000000000..23efe098c--- /dev/null+++ b/rpc-proxy/src/services/upstreamBody.ts@@ -0,0 +1,32 @@+/**+ * A failure while reading an upstream response body AFTER its headers arrived.+ * The upstream has already received the request (and for a mutating call may+ * have applied it), so this is never a down-host signal: the wake-and-hold+ * classifier excludes it by name and a mutating POST is not re-issued. The+ * original failure is kept as `cause` (a timeout there still maps to 504).+ */+export class UpstreamBodyReadError extends Error {+ constructor(cause: unknown) {+ super(+ `Upstream response body read failed after headers: ${+ cause instanceof Error ? cause.message : String(cause)+ }`,+ { cause }+ )+ this.name = 'UpstreamBodyReadError'+ }+}++/**+ * Reads the body of a mutating upstream call (approve, deny, set-model,+ * cancel). Only an error raised before `fetch()` resolved may trigger a wake+ * and a re-issued POST; a socket that dies mid-body surfaces as+ * {@link UpstreamBodyReadError} instead.+ */+export async function readMutatingResponseBody(response: Response): Promise<string> {+ try {+ return await response.text()+ } catch (error) {+ throw new UpstreamBodyReadError(error)+ }+}diff --git a/rpc-proxy/src/services/wakeAndHold.ts b/rpc-proxy/src/services/wakeAndHold.tsindex abe86a550..672b85683 100644--- a/rpc-proxy/src/services/wakeAndHold.ts+++ b/rpc-proxy/src/services/wakeAndHold.ts@@ -527,20 +527,42 @@ export function isHostDrainingError(error: unknown): boolean {return upstream.status === 503 && String(upstream.bodySnippet || '').includes('host_draining')}+/**+ * undici's own header/body timers (300 s defaults). They fire on a connection+ * the host accepted, so they prove a slow host, not a down one: a timeout, and+ * never a reason to wake and re-issue the request.+ */+const UNDICI_TIMEOUT_CODES: ReadonlySet<unknown> = new Set([+ 'UND_ERR_HEADERS_TIMEOUT',+ 'UND_ERR_BODY_TIMEOUT',+])+/** Fetch uses AbortError for controller aborts and TimeoutError for- * AbortSignal.timeout(). Both represent the same sanitized 504 boundary. */+ * AbortSignal.timeout(); undici's own header/body timers reject with their+ * code on `cause`. All represent the same sanitized 504 boundary. An+ * UpstreamBodyReadError is a timeout when the body read it wraps was one. */export function isUpstreamTimeoutError(error: unknown): boolean {- return error instanceof Error && (error.name === 'AbortError' || error.name === 'TimeoutError')+ if (!(error instanceof Error)) return false+ if (error.name === 'AbortError' || error.name === 'TimeoutError') return true+ if (error.name === 'UpstreamBodyReadError') return isUpstreamTimeoutError(error.cause)+ return UNDICI_TIMEOUT_CODES.has((error as Error & { cause?: { code?: unknown } }).cause?.code)}/*** True for a network-level fetch failure against the upstream host (no HTTP- * response at all — suspended pod, no endpoints). Excludes AbortError (today's- * 504 path) and UpstreamHostError (the host answered).+ * response at all — suspended pod, no endpoints). Excludes timeouts (today's+ * 504 path), UpstreamHostError (the host answered) and UpstreamBodyReadError+ * (the headers arrived, so the host received the request).*/export function isHostDownNetworkError(error: unknown): boolean {if (!(error instanceof Error)) return false- if (isUpstreamTimeoutError(error) || error.name === 'UpstreamHostError') return false+ if (+ isUpstreamTimeoutError(error) ||+ error.name === 'UpstreamHostError' ||+ error.name === 'UpstreamBodyReadError'+ ) {+ return false+ }const cause = (error as Error & { cause?: { code?: unknown } }).causeconst details = `${error.message} ${String(cause?.code || '')}`.toLowerCase()return (