@@ -41,6 +41,9 @@ type SentryHttpInstrumentationOptions = InstrumentationConfig & {
41
41
ignoreOutgoingRequests ?: ( url : string , request : RequestOptions ) => boolean ;
42
42
} ;
43
43
44
+ // We only want to capture request bodies up to 500kb.
45
+ const MAX_BODY_BYTE_LENGTH = 1024 * 500 ;
46
+
44
47
/**
45
48
* This custom HTTP instrumentation is used to isolate incoming requests and annotate them with additional information.
46
49
* It does not emit any spans.
@@ -347,6 +350,10 @@ function getBreadcrumbData(request: http.ClientRequest): Partial<SanitizedReques
347
350
function patchRequestToCaptureBody ( req : IncomingMessage , normalizedRequest : Request ) : void {
348
351
const chunks : Buffer [ ] = [ ] ;
349
352
353
+ function getChunksSize ( ) : number {
354
+ return chunks . reduce ( ( acc , chunk ) => acc + chunk . byteLength , 0 ) ;
355
+ }
356
+
350
357
/**
351
358
* We need to keep track of the original callbacks, in order to be able to remove listeners again.
352
359
* Since `off` depends on having the exact same function reference passed in, we need to be able to map
@@ -363,8 +370,13 @@ function patchRequestToCaptureBody(req: IncomingMessage, normalizedRequest: Requ
363
370
if ( event === 'data' ) {
364
371
const callback = new Proxy ( listener , {
365
372
apply : ( target , thisArg , args : Parameters < typeof listener > ) => {
366
- const chunk = args [ 0 ] ;
367
- chunks . push ( chunk ) ;
373
+ // If we have already read more than the max body length, we stop addiing chunks
374
+ // To avoid growing the memory indefinitely if a respons is e.g. streamed
375
+ if ( getChunksSize ( ) < MAX_BODY_BYTE_LENGTH ) {
376
+ const chunk = args [ 0 ] as Buffer ;
377
+ chunks . push ( chunk ) ;
378
+ }
379
+
368
380
return Reflect . apply ( target , thisArg , args ) ;
369
381
} ,
370
382
} ) ;
0 commit comments