@@ -6,6 +6,8 @@ import { connectIrcClient } from "./client.js";
66type 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+
271374describe ( "irc client fallback nick" , ( ) => {
272375 it ( "produces unique fallback nicks across sequential collisions" , async ( ) => {
273376 const first = await connectAfterNickCollision ( "bot" ) ;
0 commit comments