@@ -127,6 +127,7 @@ const BOT_PUBLIC_KEY = getPublicKey(Uint8Array.from(Buffer.from(PRIVATE_KEY, "he
127127const SENDER_PUBLIC_KEY = getPublicKey ( Uint8Array . from ( Buffer . from ( SENDER_PRIVATE_KEY , "hex" ) ) ) ;
128128const SENDER_SECRET_KEY = Uint8Array . from ( Buffer . from ( SENDER_PRIVATE_KEY , "hex" ) ) ;
129129const RELAY_PUBLIC_KEY = "f" . repeat ( 64 ) ;
130+ const BUZZ_RELAY_INFO_MAX_BYTES = 16 * 1024 * 1024 ;
130131const tempDirs = new Set < string > ( ) ;
131132let previousStateDir : string | undefined ;
132133let stateDir : string ;
@@ -209,13 +210,12 @@ describe("Buzz bus lifecycle", () => {
209210 relayMocks . stallRoomEoseChannelId = undefined ;
210211 vi . stubGlobal (
211212 "fetch" ,
212- vi . fn ( async ( ) => ( {
213- ok : true ,
214- json : async ( ) => ( {
213+ vi . fn ( async ( ) =>
214+ Response . json ( {
215215 self : RELAY_PUBLIC_KEY ,
216216 software : "https://github.com/block/buzz" ,
217217 } ) ,
218- } ) ) ,
218+ ) ,
219219 ) ;
220220 } ) ;
221221
@@ -291,6 +291,58 @@ describe("Buzz bus lifecycle", () => {
291291 vi . useRealTimers ( ) ;
292292 } ) ;
293293
294+ it ( "cancels oversized NIP-11 relay information before consuming the entire response" , async ( ) => {
295+ relayMocks . auth . mockResolvedValue ( "ok" ) ;
296+ const cancel = vi . fn ( ) ;
297+ const chunk = new Uint8Array ( 1024 * 1024 ) . fill ( "x" . charCodeAt ( 0 ) ) ;
298+ let emittedChunks = 0 ;
299+ const body = new ReadableStream < Uint8Array > ( {
300+ pull ( controller ) {
301+ if ( emittedChunks === 0 ) {
302+ controller . enqueue (
303+ new TextEncoder ( ) . encode ( `{"self":"${ RELAY_PUBLIC_KEY } ","description":"` ) ,
304+ ) ;
305+ } else if ( emittedChunks <= 17 ) {
306+ controller . enqueue ( chunk ) ;
307+ } else {
308+ controller . enqueue ( new TextEncoder ( ) . encode ( '"}' ) ) ;
309+ controller . close ( ) ;
310+ }
311+ emittedChunks += 1 ;
312+ } ,
313+ cancel,
314+ } ) ;
315+ vi . stubGlobal (
316+ "fetch" ,
317+ vi . fn < typeof fetch > ( async ( ) => new Response ( body ) ) ,
318+ ) ;
319+
320+ await expect ( startTestBus ( ) ) . rejects . toThrow (
321+ `Buzz relay information: JSON response exceeds ${ BUZZ_RELAY_INFO_MAX_BYTES } bytes` ,
322+ ) ;
323+
324+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
325+ expect ( emittedChunks ) . toBeLessThan ( 19 ) ;
326+ expect ( relayMocks . close ) . toHaveBeenCalledOnce ( ) ;
327+ } ) ;
328+
329+ it . each ( [
330+ [ "truncated JSON" , '{"self":' ] ,
331+ [ "null" , "null" ] ,
332+ [ "an array" , "[]" ] ,
333+ [ "a primitive" , "true" ] ,
334+ ] ) ( "rejects malformed NIP-11 relay information containing %s" , async ( _label , body ) => {
335+ relayMocks . auth . mockResolvedValue ( "ok" ) ;
336+ vi . stubGlobal (
337+ "fetch" ,
338+ vi . fn < typeof fetch > ( async ( ) => new Response ( body ) ) ,
339+ ) ;
340+
341+ await expect ( startTestBus ( ) ) . rejects . toThrow ( "Buzz relay information: malformed JSON response" ) ;
342+
343+ expect ( relayMocks . close ) . toHaveBeenCalledOnce ( ) ;
344+ } ) ;
345+
294346 it ( "publishes and closes a standalone authenticated send" , async ( ) => {
295347 relayMocks . auth . mockResolvedValue ( "ok" ) ;
296348
0 commit comments