Skip to content

Commit 7e740ba

Browse files
committed
fix(irc): disconnect on oversized inbound lines
1 parent b47c9b2 commit 7e740ba

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

‎extensions/irc/src/client.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { connectIrcClient } from "./client.js";
66
type LoopbackIrcServer = {
77
port: number;
88
lines: string[];
9+
openSocketCount(): number;
10+
writeToClients(data: string): void;
911
close(): Promise<void>;
1012
};
1113

@@ -51,6 +53,9 @@ async function startLoopbackIrcServer(options?: {
5153
socket.on("close", () => {
5254
sockets.delete(socket);
5355
});
56+
socket.on("error", () => {
57+
// The client intentionally resets the socket when a protocol limit is exceeded.
58+
});
5459
});
5560
await new Promise<void>((resolve) => {
5661
server.listen(0, "127.0.0.1", resolve);
@@ -62,6 +67,12 @@ async function startLoopbackIrcServer(options?: {
6267
return {
6368
port: address.port,
6469
lines,
70+
openSocketCount: () => sockets.size,
71+
writeToClients: (data) => {
72+
for (const socket of sockets) {
73+
socket.write(data);
74+
}
75+
},
6576
close: async () => {
6677
for (const socket of sockets) {
6778
socket.destroy();
@@ -268,6 +279,98 @@ describe("irc client readiness timeout", () => {
268279
});
269280
});
270281

282+
describe("irc client inbound line byte limit", () => {
283+
it("reports an error and closes on an oversized unterminated line", async () => {
284+
const server = await startLoopbackIrcServer();
285+
const errors: Error[] = [];
286+
try {
287+
const client = await connectIrcClient({
288+
host: "127.0.0.1",
289+
port: server.port,
290+
tls: false,
291+
nick: "bot",
292+
username: "bot",
293+
realname: "OpenClaw Bot",
294+
connectTimeoutMs: 5000,
295+
onError: (error) => errors.push(error),
296+
});
297+
298+
server.writeToClients("x".repeat(1024 * 1024));
299+
300+
await waitForIrcCondition(
301+
() => errors.length > 0 && server.openSocketCount() === 0,
302+
"expected oversized IRC input to close the client socket",
303+
);
304+
expect(errors.map((error) => error.message)).toEqual([
305+
"IRC inbound line exceeds the 512-byte limit",
306+
]);
307+
expect(client.isReady()).toBe(false);
308+
} finally {
309+
await server.close();
310+
}
311+
});
312+
313+
it("accepts a legal 510-byte line split across socket writes", async () => {
314+
const server = await startLoopbackIrcServer();
315+
const receivedLines: string[] = [];
316+
try {
317+
const client = await connectIrcClient({
318+
host: "127.0.0.1",
319+
port: server.port,
320+
tls: false,
321+
nick: "bot",
322+
username: "bot",
323+
realname: "OpenClaw Bot",
324+
connectTimeoutMs: 5000,
325+
onLine: (line) => receivedLines.push(line),
326+
});
327+
const line = `PING :${"x".repeat(504)}`;
328+
329+
server.writeToClients(line.slice(0, 300));
330+
server.writeToClients(`${line.slice(300)}\r\n`);
331+
332+
await waitForIrcCondition(
333+
() =>
334+
receivedLines.includes(line) && server.lines.some((entry) => entry.startsWith("PONG :")),
335+
"expected split legal IRC line to be processed",
336+
);
337+
expect(Buffer.byteLength(`${line}\r\n`, "utf8")).toBe(512);
338+
expect(client.isReady()).toBe(true);
339+
client.close();
340+
} finally {
341+
await server.close();
342+
}
343+
});
344+
345+
it("applies the inbound limit to UTF-8 bytes", async () => {
346+
const server = await startLoopbackIrcServer();
347+
const errors: Error[] = [];
348+
try {
349+
await connectIrcClient({
350+
host: "127.0.0.1",
351+
port: server.port,
352+
tls: false,
353+
nick: "bot",
354+
username: "bot",
355+
realname: "OpenClaw Bot",
356+
connectTimeoutMs: 5000,
357+
onError: (error) => errors.push(error),
358+
});
359+
360+
server.writeToClients(`NOTICE bot :${"猫".repeat(167)}\r\n`);
361+
362+
await waitForIrcCondition(
363+
() => errors.length > 0 && server.openSocketCount() === 0,
364+
"expected multibyte oversized IRC line to close the client socket",
365+
);
366+
expect(errors).toHaveLength(1);
367+
expect(errors[0]?.message).toContain("512-byte limit");
368+
} finally {
369+
await server.close();
370+
}
371+
});
372+
});
373+
271374
describe("irc client fallback nick", () => {
272375
it("produces unique fallback nicks across sequential collisions", async () => {
273376
const first = await connectAfterNickCollision("bot");

‎extensions/irc/src/client.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
const IRC_ERROR_CODES = new Set(["432", "464", "465"]);
1414
const IRC_NICK_COLLISION_CODES = new Set(["433", "436"]);
1515
const IRC_MAX_LINE_BYTES = 512;
16+
const IRC_MAX_LINE_PAYLOAD_BYTES = IRC_MAX_LINE_BYTES - Buffer.byteLength("\r\n", "utf8");
1617

1718
function takeIrcPrivmsgChunk(text: string, maxChars: number, maxBytes: number): string {
1819
let end = 0;
@@ -292,6 +293,11 @@ export async function connectIrcClient(options: IrcClientOptions): Promise<IrcCl
292293
buffer = buffer.slice(idx + 1);
293294
idx = buffer.indexOf("\n");
294295

296+
if (Buffer.byteLength(rawLine, "utf8") > IRC_MAX_LINE_PAYLOAD_BYTES) {
297+
failAndClose(new Error(`IRC inbound line exceeds the ${IRC_MAX_LINE_BYTES}-byte limit`));
298+
return;
299+
}
300+
295301
if (!rawLine) {
296302
continue;
297303
}
@@ -409,6 +415,13 @@ export async function connectIrcClient(options: IrcClientOptions): Promise<IrcCl
409415
}
410416
}
411417
}
418+
419+
// Reserve the terminating CRLF while the final record is still pending.
420+
// Otherwise a peer can grow this retained buffer without bound.
421+
const pendingLine = buffer.endsWith("\r") ? buffer.slice(0, -1) : buffer;
422+
if (Buffer.byteLength(pendingLine, "utf8") > IRC_MAX_LINE_PAYLOAD_BYTES) {
423+
failAndClose(new Error(`IRC inbound line exceeds the ${IRC_MAX_LINE_BYTES}-byte limit`));
424+
}
412425
});
413426

414427
socket.once("connect", () => {

0 commit comments

Comments
 (0)