Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(rpc-proxy): map control-api 401/403 on connectors to a real status (R1-H2)

Diego Zacariasb99a89689370
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.ts
index f1cda4b96..5bf3423ed 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.7.0',
externalRestApiVersion: '0.1.81',
- rpcProxyVersion: '0.1.69',
+ rpcProxyVersion: '0.1.70',
desktopVersion: '0.7.0',
minimumDesktopVersion: '0.1.252',
}
diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.json
index c6e06d9b8..394884333 100644
--- a/rpc-proxy/package-lock.json
+++ b/rpc-proxy/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.69",
+ "version": "0.1.70",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-rpc-proxy",
- "version": "0.1.69",
+ "version": "0.1.70",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
diff --git a/rpc-proxy/package.json b/rpc-proxy/package.json
index 53ef788c9..0d68e267d 100644
--- a/rpc-proxy/package.json
+++ b/rpc-proxy/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.69",
+ "version": "0.1.70",
"description": "External-facing RPC proxy for tenant-scoped MCP access",
"main": "dist/main.js",
"engines": {
diff --git a/rpc-proxy/src/__tests__/controlApiRestService.connectors.test.ts b/rpc-proxy/src/__tests__/controlApiRestService.connectors.test.ts
index 88d02ea36..09df9c183 100644
--- a/rpc-proxy/src/__tests__/controlApiRestService.connectors.test.ts
+++ b/rpc-proxy/src/__tests__/controlApiRestService.connectors.test.ts
@@ -1,5 +1,8 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
-import { fetchUserConnectorsFromControlApi } from '../services/controlApiRestService.js'
+import {
+ ControlApiConnectorsRejectedError,
+ fetchUserConnectorsFromControlApi,
+} from '../services/controlApiRestService.js'
// spec 11 U1 — unit test for the connectors SANEADOR. Unlike the route test
// (rpc-connectors-route.test.ts) which mocks this function whole, here we stub
@@ -128,6 +131,22 @@ describe('fetchUserConnectorsFromControlApi — non-secret projection', () => {
)
})
+ // H2 — 401/403 carry a TYPED rejection (with status), so the route can map
+ // them to a real client status instead of a non-refreshable 500.
+ it.each([401, 403] as const)('throws a typed rejection carrying status %d', async status => {
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () => jsonResponse({}, false, status))
+ )
+ await expect(fetchUserConnectorsFromControlApi('local-user', 'rpc-tok')).rejects.toMatchObject({
+ name: 'ControlApiConnectorsRejectedError',
+ status,
+ })
+ // And it is the concrete class, so `instanceof` in the route holds.
+ const err = await fetchUserConnectorsFromControlApi('local-user', 'rpc-tok').catch(e => e)
+ expect(err).toBeInstanceOf(ControlApiConnectorsRejectedError)
+ })
+
it('bounds the control-api call with an abort signal (SM3 — no unbounded socket)', async () => {
// Without a signal, a hung control-api pins the proxy socket indefinitely.
// The wiring must pass an AbortSignal (AbortSignal.timeout) so a stalled
diff --git a/rpc-proxy/src/__tests__/rpc-connectors-route.test.ts b/rpc-proxy/src/__tests__/rpc-connectors-route.test.ts
index 26f45e5aa..0317bdbca 100644
--- a/rpc-proxy/src/__tests__/rpc-connectors-route.test.ts
+++ b/rpc-proxy/src/__tests__/rpc-connectors-route.test.ts
@@ -9,11 +9,23 @@ import { createRpcRouter } from '../routes/rpc.js'
// the scope gate, the session-derived identity, and passthrough.
const authTokenMock = vi.hoisted(() => ({ verifyRpcToken: vi.fn() }))
-const controlApiMock = vi.hoisted(() => ({
- fetchUserConnectorsFromControlApi: vi.fn(),
- fetchHostConnectionFromControlApi: vi.fn(),
- requestHostWakeFromControlApi: vi.fn(),
-}))
+const controlApiMock = vi.hoisted(() => {
+ // The route does `error instanceof ControlApiConnectorsRejectedError`, so the
+ // mocked module must expose the real class shape — otherwise the import is
+ // undefined and the instanceof check throws.
+ class ControlApiConnectorsRejectedError extends Error {
+ constructor(readonly status: 401 | 403) {
+ super(`Control API rejected connectors read (${status})`)
+ this.name = 'ControlApiConnectorsRejectedError'
+ }
+ }
+ return {
+ ControlApiConnectorsRejectedError,
+ fetchUserConnectorsFromControlApi: vi.fn(),
+ fetchHostConnectionFromControlApi: vi.fn(),
+ requestHostWakeFromControlApi: vi.fn(),
+ }
+})
// createRpcRouter also imports the proxy service barrel; stub it so the module
// graph loads without real upstreams.
const serviceMock = vi.hoisted(() => ({
@@ -117,4 +129,26 @@ describe('GET /rpc/connectors', () => {
const res = await request(makeApp()).get('/rpc/connectors').set('authorization', 'Bearer tok')
expect(res.status).toBe(500)
})
+
+ // H2 — a control-api 401 must reach the client AS 401, not collapse to 500.
+ // The desktop only refreshes the rpc access token on 401 (or a scope 403);
+ // a 500 is non-refreshable, so an expired token surfaced as 500 would leave
+ // the panel permanently stuck.
+ it('maps a control-api 401 to a client 401 (keeps token refresh alive)', async () => {
+ authTokenMock.verifyRpcToken.mockReturnValue(claims(['mcp:servers:list']))
+ controlApiMock.fetchUserConnectorsFromControlApi.mockRejectedValue(
+ new controlApiMock.ControlApiConnectorsRejectedError(401)
+ )
+ const res = await request(makeApp()).get('/rpc/connectors').set('authorization', 'Bearer tok')
+ expect(res.status).toBe(401)
+ })
+
+ it('maps a control-api 403 to a client 403 (not 500)', async () => {
+ authTokenMock.verifyRpcToken.mockReturnValue(claims(['mcp:servers:list']))
+ controlApiMock.fetchUserConnectorsFromControlApi.mockRejectedValue(
+ new controlApiMock.ControlApiConnectorsRejectedError(403)
+ )
+ const res = await request(makeApp()).get('/rpc/connectors').set('authorization', 'Bearer tok')
+ expect(res.status).toBe(403)
+ })
})
diff --git a/rpc-proxy/src/routes/rpc.ts b/rpc-proxy/src/routes/rpc.ts
index 0846a1e81..2b42a30f3 100644
--- a/rpc-proxy/src/routes/rpc.ts
+++ b/rpc-proxy/src/routes/rpc.ts
@@ -10,6 +10,7 @@ import {
} from '../middleware/auth.js'
import { rpcInvocationContext } from '../rpcAccessContext.js'
import {
+ ControlApiConnectorsRejectedError,
type HostWakeApiResponse,
fetchUserConnectorsFromControlApi,
requestHostWakeFromControlApi,
@@ -239,6 +240,19 @@ export function createRpcRouter(): Router {
const result = await fetchUserConnectorsFromControlApi(auth.sub, rpcAccessToken)
res.status(200).json(result)
} catch (error) {
+ // Map a control-api 401/403 to a real status instead of the default
+ // 500. A 500 is non-refreshable on the desktop (shouldRefreshRpcToken),
+ // so an expired rpc access token surfaced as 500 would never trigger a
+ // refresh and would leave the panel stuck. 401 keeps the refresh path
+ // alive; everything else stays 500/504 via the app error handler.
+ if (error instanceof ControlApiConnectorsRejectedError) {
+ if (error.status === 401) {
+ res.status(401).json({ error: 'Unauthorized' })
+ return
+ }
+ res.status(403).json({ error: 'Forbidden: user cannot read connectors' })
+ return
+ }
next(error)
}
}
diff --git a/rpc-proxy/src/services/controlApiRestService.ts b/rpc-proxy/src/services/controlApiRestService.ts
index 5f1bfe971..65d772ac3 100644
--- a/rpc-proxy/src/services/controlApiRestService.ts
+++ b/rpc-proxy/src/services/controlApiRestService.ts
@@ -48,6 +48,18 @@ export class ControlApiHostAccessRejectedError extends Error {
}
}
+// Typed rejection for the connectors read-model, mirroring the host rail above.
+// A generic Error collapses to 500 in the app error handler, which the desktop
+// reads as non-refreshable — so an expired/rotated rpc access token (401) would
+// never trigger a token refresh and the panel would stay broken. Carrying the
+// status lets the route map 401/403 to a real status the client can act on.
+export class ControlApiConnectorsRejectedError extends Error {
+ constructor(readonly status: 401 | 403) {
+ super(`Control API rejected connectors read (${status})`)
+ this.name = 'ControlApiConnectorsRejectedError'
+ }
+}
+
export async function fetchUserAllowedServersFromControlApi(
userId: string,
rpcAccessToken: string
@@ -170,6 +182,9 @@ export async function fetchUserConnectorsFromControlApi(
}
)
+ if (response.status === 401 || response.status === 403) {
+ throw new ControlApiConnectorsRejectedError(response.status)
+ }
if (!response.ok) {
throw new Error(`Control API MCP connectors lookup failed (${response.status})`)
}