Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix: close the forged-machine_meta path through the backend's own relay

Kelvinc717e33cf220
diff --git a/backend/src/lib/webWs.test.ts b/backend/src/lib/webWs.test.ts
new file mode 100644
index 000000000..63c35ea4d
--- /dev/null
+++ b/backend/src/lib/webWs.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from 'vitest'
+import { BACKEND_ONLY_DOWN_TYPES } from './webWs.js'
+
+/**
+ * `handleFrame` forwards anything it does not recognise straight to the adapter
+ * (`client.sendDown(frame)`), so the only thing standing between a web client and a frame the
+ * adapter obeys as the backend's own is the refusal list. `handleFrame` itself needs a live socket
+ * to exercise, so what is pinned here is that list — the realistic regression is an entry being
+ * dropped during a tidy-up, which reopens the hole silently.
+ */
+describe('frames a web client may never forge', () => {
+ it('refuses the two backend-authoritative frames that are not `__`-prefixed', () => {
+ // machine_meta names the account's private grid — the inference endpoint every agent on that
+ // computer is then pointed at. machine_revoked makes the adapter clear its session and exit.
+ expect([...BACKEND_ONLY_DOWN_TYPES].sort()).toEqual(['machine_meta', 'machine_revoked'])
+ })
+
+ it('covers only frames that are NOT already caught by the `__` rule', () => {
+ // A `__`-prefixed type here would be dead weight: handleFrame drops those earlier, and listing
+ // one would suggest this set is the whole rule rather than the exception to it.
+ for (const type of BACKEND_ONLY_DOWN_TYPES) expect(type.startsWith('__')).toBe(false)
+ })
+})
diff --git a/backend/src/lib/webWs.ts b/backend/src/lib/webWs.ts
index 3bb58c7e6..2c61f62f1 100644
--- a/backend/src/lib/webWs.ts
+++ b/backend/src/lib/webWs.ts
@@ -55,6 +55,18 @@ import {
} from './p2pSignaling.js'
import { recordRemoteUsage } from './dailyTracking.js'
+/**
+ * Down-frames the backend mints for an adapter, which a web client must never be able to forge.
+ *
+ * The `__` prefix already marks most of these; these two predate that convention and are not
+ * prefixed, so they fell through this handler's namespace checks into the verbatim forward at the
+ * bottom. See the block in `handleFrame` for what each one does when forged.
+ *
+ * Senders, all backend-side: `lib/adapterWs.ts` (on connect) and `services/MachineService.ts`
+ * (rename, revoke).
+ */
+export const BACKEND_ONLY_DOWN_TYPES = new Set(['machine_meta', 'machine_revoked'])
+
const wss = createWss(WS_LIMITS.web, { echoFirstProtocol: true })
// Device-status re-seed: covers the silent-offline case (worker crash → presence TTL 45s ages out
@@ -341,6 +353,19 @@ function attachUserClient(ws: WebSocket, user: AuthUser): void {
// Double-underscore frames are backend-to-Harness control messages. A web
// client must never be able to forge its own lifecycle notification.
if (typeof type === 'string' && type.startsWith('__')) return
+ // ⚠️ Same rule, for the two control frames that are NOT `__`-prefixed and so escaped it. Anything
+ // reaching this handler holds a valid access token for the account and nothing more, while the
+ // frames below are instructions the adapter obeys as the backend's own — everything else here
+ // falls through to `client.sendDown(frame)`, which forwards verbatim:
+ // - `machine_meta` names the account's private grid, i.e. the inference endpoint every agent on
+ // that computer is then pointed at. Forged, it redirects the account's work.
+ // - `machine_revoked` makes the adapter clear its stored session and exit.
+ // Both are minted here (`adapterWs.ts`, `services/MachineService.ts`); no client in this
+ // repository sends either, so refusing them costs nothing. The adapter refuses them from
+ // non-backend transports too (`cli/src/backendSocket.ts`, BACKEND_ONLY_DOWN_TYPES) — but it
+ // cannot tell a frame the backend decided on from one the backend relayed for a web client, so
+ // that check alone does not cover this path. This is where that distinction still exists.
+ if (typeof type === 'string' && BACKEND_ONLY_DOWN_TYPES.has(type)) return
const isTerminal = typeof type === 'string' && TERMINAL_DOWN_TYPES.has(type)
const terminalNamespace = typeof type === 'string' && type.startsWith('terminal_')
if (terminalNamespace && !isTerminal) {
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.ts
index eb6e9b796..5ba7c763f 100644
--- a/cli/src/backendSocket.spec.ts
+++ b/cli/src/backendSocket.spec.ts
@@ -1727,6 +1727,29 @@ describe('machine_meta carries the grid name without clobbering it on rename', (
await socket.stop()
})
+
+ it('refuses a machine_revoked over the LOCAL socket — one frame would otherwise sign this computer out', async () => {
+ const socket = new BackendSocket('token')
+ let revoked = 0
+ socket.onRevoked = () => { revoked += 1 }
+ socket.connect()
+ const ws = wsMock.instances[0]
+ ws.open()
+
+ // `onRevoked` clears the stored SSO session and exits the daemon, so accepting this from a local
+ // process is a one-frame forced sign-out and denial of service.
+ const local = 'local:1'
+ socket.registerLocalClient(local, { sendFrame: () => true, sendBinary: () => true })
+ socket.handleLocalFrame(local, { type: 'machine_revoked', payload: {} })
+ await new Promise((r) => setTimeout(r, 50))
+ expect(revoked).toBe(0)
+
+ // The backend's own frame still works — the gate is about the sender, not the frame.
+ ws.message({ t: 'down', connId: '', frame: { type: 'machine_revoked', payload: {} } })
+ await vi.waitFor(() => expect(revoked).toBe(1))
+
+ await socket.stop()
+ })
})
/** The read-only hardware line for the run-a-harness-compute dialog, answered next to `grid_models_list`. */
diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.ts
index 18513ff59..58f903b0f 100644
--- a/cli/src/backendSocket.ts
+++ b/cli/src/backendSocket.ts
@@ -154,6 +154,23 @@ export function isLocalClientId(connId: string): boolean {
*/
export type DownTransport = 'relay' | 'local' | 'p2p'
+/**
+ * Down-frames only the BACKEND may send, refused from every other transport.
+ *
+ * Each one hands the daemon an instruction no client is entitled to give:
+ * - `machine_meta` names the account's private grid — the inference endpoint every agent on this
+ * computer is then pointed at. Forged, it redirects the account's work to a grid of the
+ * sender's choosing. A leftover test script did exactly this by accident once.
+ * - `machine_revoked` clears the stored SSO session and exits the daemon. Forged, it is a
+ * one-frame forced sign-out and denial of service.
+ *
+ * Neither is sent by any client in this repository — only by `backend/src/lib/adapterWs.ts` and
+ * `backend/src/services/MachineService.ts` — so there is nothing to stay compatible with. The
+ * backend blocks its OWN `__`-prefixed control frames from web clients for the same reason; these
+ * two escaped that rule because they are not `__`-prefixed.
+ */
+const BACKEND_ONLY_DOWN_TYPES = new Set(['machine_meta', 'machine_revoked'])
+
interface QueueItem {
id: number
data: string
@@ -1194,6 +1211,13 @@ export class BackendSocket {
// frames were then read as the BACKEND's. The transport half closes that, because it is stamped
// at enqueue by the caller that had just verified membership. Either one being true is local.
const local = transport === 'local' || this.localClients.has(connId)
+ // ⚠️ The backend's own instructions, refused from anywhere else. See BACKEND_ONLY_DOWN_TYPES.
+ // `transport === 'relay'` rather than `!local`, so this keeps holding if the p2p allowlist
+ // (`TERMINAL_P2P_DOWN_TYPES`) ever widens; "not local" would quietly start admitting p2p.
+ if (transport !== 'relay' && BACKEND_ONLY_DOWN_TYPES.has(type)) {
+ console.warn(`[backend] ignoring ${type} from ${transport} (${connId}) — only the backend may send it`)
+ return
+ }
// E2EE control frames (pairing/handshake) are handled by the manager, never as node RPCs.
if (type.startsWith('e2e_')) {
if (local) {
@@ -1286,14 +1310,8 @@ export class BackendSocket {
// port redirect the account's inference somewhere of its choosing, and a leftover test script
// doing exactly that by accident cost hours to find. No client sends this frame; there is
// nothing to be compatible with.
+ // The source check is above, with the other frames only the backend may send.
//
- // Stricter than the `!local` its neighbours use, on purpose: this says "the backend link", so
- // it keeps holding if the p2p allowlist (`TERMINAL_P2P_DOWN_TYPES`) ever widens. "Not local"
- // would quietly start admitting p2p on the day that set grew.
- if (transport !== 'relay') {
- console.warn(`[backend] ignoring machine_meta from ${transport} (${connId}) — only the backend may send it`)
- return
- }
// A malformed/hostile frame's payload need not be an object; `'gridName' in meta` would throw
// on a primitive (and drop the whole frame via enqueueDown's catch). Guard the type first, the
// way the plain property reads elsewhere in this dispatcher tolerate one.