Skip to content

Commit a76e1de

Browse files
committed
limit body buffer size
1 parent 4ca1bae commit a76e1de

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

packages/node/src/integrations/http/SentryHttpInstrumentation.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ type SentryHttpInstrumentationOptions = InstrumentationConfig & {
4141
ignoreOutgoingRequests?: (url: string, request: RequestOptions) => boolean;
4242
};
4343

44+
// We only want to capture request bodies up to 500kb.
45+
const MAX_BODY_BYTE_LENGTH = 1024 * 500;
46+
4447
/**
4548
* This custom HTTP instrumentation is used to isolate incoming requests and annotate them with additional information.
4649
* It does not emit any spans.
@@ -347,6 +350,10 @@ function getBreadcrumbData(request: http.ClientRequest): Partial<SanitizedReques
347350
function patchRequestToCaptureBody(req: IncomingMessage, normalizedRequest: Request): void {
348351
const chunks: Buffer[] = [];
349352

353+
function getChunksSize(): number {
354+
return chunks.reduce((acc, chunk) => acc + chunk.byteLength, 0);
355+
}
356+
350357
/**
351358
* We need to keep track of the original callbacks, in order to be able to remove listeners again.
352359
* 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
363370
if (event === 'data') {
364371
const callback = new Proxy(listener, {
365372
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+
368380
return Reflect.apply(target, thisArg, args);
369381
},
370382
});

0 commit comments

Comments
 (0)