an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
fix(hooks): harden socket frame decoding and bounds
hsien wei72c1d5fe0a95
diff --git a/src/main/hooks.ts b/src/main/hooks.tsindex f51dfd19..79b29bb1 100644--- a/src/main/hooks.ts+++ b/src/main/hooks.ts@@ -20,6 +20,9 @@ import type { CircuitBreaker } from './breaker';import { estimateCostUsd } from './pricing';import { validateHookEvent } from '../shared/hookEvents';+/** Maximum JSON payload bytes in one newline-delimited hook frame. */+const MAX_HOOK_FRAME_BYTES = 256 * 1024;+interface HookPayload {hook_event_name?: string;agent_id?: string | null;@@ -85,13 +88,22 @@ export class HookServer {try { if (existsSync(sock)) rmSync(sock); } catch { /* noop */ }this.server = createServer((conn) => {- let buf = '';- conn.on('data', (d) => {- buf += d.toString();- const nl = buf.indexOf('\n');- if (nl === -1) return; // wait for the full line+ let pending = Buffer.alloc(0);+ conn.on('data', (chunk) => {+ pending = Buffer.concat([pending, chunk]);+ const nl = pending.indexOf(0x0a);+ if (nl === -1) {+ if (pending.length > MAX_HOOK_FRAME_BYTES) conn.destroy();+ return; // wait for the full line+ }+ // The byte limit covers the JSON payload and excludes its newline.+ if (nl > MAX_HOOK_FRAME_BYTES) {+ conn.destroy();+ return;+ }+ const frame = pending.subarray(0, nl).toString('utf8');let payload: HookPayload = {};- try { payload = JSON.parse(buf.slice(0, nl)); } catch { /* ignore */ }+ try { payload = JSON.parse(frame); } catch { /* ignore */ }let res: unknown = {};try { res = this.handle(payload); } catch { res = {}; }conn.end(JSON.stringify(res ?? {}));diff --git a/test/hooks-framing.test.cjs b/test/hooks-framing.test.cjsnew file mode 100644index 00000000..e0846889--- /dev/null+++ b/test/hooks-framing.test.cjs@@ -0,0 +1,168 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');+const fs = require('node:fs');+const net = require('node:net');+const os = require('node:os');+const path = require('node:path');+const { once } = require('node:events');+const loadTs = require('./load-ts.cjs');++const electron = require.resolve('electron');+require.cache[electron] = {+ id: electron,+ filename: electron,+ loaded: true,+ exports: { Notification: class { static isSupported() { return false; } } }+};++const { HookServer } = loadTs('src/main/hooks.ts');+const MAX_HOOK_FRAME_BYTES = 256 * 1024;+let socketSequence = 0;++const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));++function socketPath(base) {+ const id = `${process.pid}-${Date.now()}-${socketSequence++}`;+ return process.platform === 'win32'+ ? `\\\\.\\pipe\\md-hooks-frame-${id}`+ : path.join(base, `hooks-${id}.sock`);+}++async function openServer(t) {+ const base = fs.mkdtempSync(path.join(os.tmpdir(), 'md-hooks-frame-'));+ const sock = socketPath(base);+ const routed = [];+ const hive = { sockPath: () => sock };+ const server = new HookServer(hive, () => null, () => ({ notifications: false }));++ // Exercise the real net.Server framing path while isolating these tests from+ // HookServer's unrelated routing behavior.+ server.handle = (payload) => {+ routed.push(payload);+ return {};+ };++ server.start();+ assert.ok(server.server, 'HookServer did not create its net.Server');+ if (!server.server.listening) await once(server.server, 'listening');++ t.after(() => {+ server.stop();+ fs.rmSync(base, { recursive: true, force: true });+ });+ return { sock, routed };+}++async function sendChunks(sock, chunks, pauseMs = 0, timeoutMs = 1000) {+ const socket = net.createConnection(sock);+ socket.on('error', () => { /* rejection may reset the client connection */ });+ await once(socket, 'connect');++ const response = [];+ socket.on('data', (chunk) => response.push(chunk));+ const closed = new Promise((resolve) => {+ socket.once('end', () => resolve(true));+ socket.once('close', () => resolve(true));+ });++ for (let i = 0; i < chunks.length; i += 1) {+ socket.write(chunks[i]);+ if (pauseMs > 0 && i < chunks.length - 1) await delay(pauseMs);+ }++ const didClose = await Promise.race([+ closed,+ delay(timeoutMs).then(() => false)+ ]);+ if (!didClose) socket.destroy();+ return { didClose, response: Buffer.concat(response).toString('utf8') };+}++function framedPayloadWithByteLength(byteLength) {+ const empty = Buffer.byteLength(JSON.stringify({ message: '' }), 'utf8');+ assert.ok(byteLength >= empty);+ const text = JSON.stringify({ message: 'x'.repeat(byteLength - empty) });+ assert.equal(Buffer.byteLength(text, 'utf8'), byteLength);+ return Buffer.from(`${text}\n`, 'utf8');+}++function splitInside(frame, character) {+ const start = frame.indexOf(Buffer.from(character, 'utf8'));+ assert.notEqual(start, -1, `test frame does not contain ${character}`);+ return start + 1;+}++test('CJK payload survives a split inside a multibyte code point', async (t) => {+ const { sock, routed } = await openServer(t);+ const payload = { message: '靽桀儔皜祈岫' };+ const frame = Buffer.from(`${JSON.stringify(payload)}\n`, 'utf8');+ const split = splitInside(frame, '靽');++ const result = await sendChunks(sock, [frame.subarray(0, split), frame.subarray(split)], 25);++ assert.equal(result.didClose, true);+ assert.deepEqual(routed, [payload]);+});++test('emoji payload survives a split inside a four-byte code point', async (t) => {+ const { sock, routed } = await openServer(t);+ const payload = { message: 'deploy 🚀 ready' };+ const frame = Buffer.from(`${JSON.stringify(payload)}\n`, 'utf8');+ const split = splitInside(frame, '🚀');++ const result = await sendChunks(sock, [frame.subarray(0, split), frame.subarray(split)], 25);++ assert.equal(result.didClose, true);+ assert.deepEqual(routed, [payload]);+});++test('ordinary fragmented frame retains existing one-request behavior', async (t) => {+ const { sock, routed } = await openServer(t);+ const payload = { hook_event_name: 'Stop', message: 'plain ASCII' };+ const frame = Buffer.from(`${JSON.stringify(payload)}\n`, 'utf8');++ const result = await sendChunks(sock, [+ frame.subarray(0, 5),+ frame.subarray(5, 17),+ frame.subarray(17)+ ], 10);++ assert.equal(result.didClose, true);+ assert.deepEqual(routed, [payload]);+ assert.equal(result.response, '{}');+});++test('oversized incomplete frame is rejected without routing a payload', async (t) => {+ const { sock, routed } = await openServer(t);++ const result = await sendChunks(sock, [Buffer.alloc(MAX_HOOK_FRAME_BYTES + 1, 0x78)], 0, 400);++ assert.equal(result.didClose, true, 'server retained an oversized incomplete frame');+ assert.deepEqual(routed, []);+ assert.equal(result.response, '');+});++test('payload exactly at the byte limit is accepted', async (t) => {+ const { sock, routed } = await openServer(t);+ const frame = framedPayloadWithByteLength(MAX_HOOK_FRAME_BYTES);++ const result = await sendChunks(sock, [frame]);++ assert.equal(result.didClose, true);+ assert.equal(routed.length, 1);+ assert.equal(routed[0].message.length > 0, true);+ assert.equal(result.response, '{}');+});++test('payload one byte above the limit is rejected', async (t) => {+ const { sock, routed } = await openServer(t);+ const frame = framedPayloadWithByteLength(MAX_HOOK_FRAME_BYTES + 1);++ const result = await sendChunks(sock, [frame]);++ assert.equal(result.didClose, true);+ assert.deepEqual(routed, []);+ assert.equal(result.response, '');+});