Skip to content

Commit bafd05f

Browse files
committed
review suggestions
1 parent 7a21af7 commit bafd05f

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

dev-packages/node-integration-tests/suites/express/with-http/maxRequestBodySize/instrument-none.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ Sentry.init({
66
release: '1.0',
77
tracesSampleRate: 1.0,
88
transport: loggingTransport,
9-
integrations: [Sentry.httpIntegration({ maxRequestBodySize: 'none' })],
9+
integrations: [
10+
Sentry.httpIntegration({
11+
maxRequestBodySize: 'none',
12+
ignoreIncomingRequestBody: url => url.includes('/ignore-request-body'),
13+
}),
14+
],
1015
});

dev-packages/node-integration-tests/suites/express/with-http/maxRequestBodySize/scenario.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ app.post('/test-body-size', (req, res) => {
1818
});
1919
});
2020

21+
app.post('/ignore-request-body', (req, res) => {
22+
const receivedSize = JSON.stringify(req.body).length;
23+
res.json({
24+
success: true,
25+
receivedSize,
26+
message: 'Payload processed successfully',
27+
});
28+
});
29+
2130
Sentry.setupExpressErrorHandler(app);
2231

2332
startExpressServerAndSendPortToRunner(app);

dev-packages/node-integration-tests/suites/express/with-http/maxRequestBodySize/test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,39 @@ describe('express with httpIntegration and maxRequestBodySize: "none"', () => {
8989

9090
await runner.completed();
9191
});
92+
93+
test('does not capture any request bodies with none setting and "ignoreIncomingRequestBody"', async () => {
94+
const runner = createRunner()
95+
.expect({
96+
transaction: {
97+
transaction: 'POST /test-body-size',
98+
request: expect.not.objectContaining({
99+
data: expect.any(String),
100+
}),
101+
},
102+
})
103+
.expect({
104+
transaction: {
105+
transaction: 'POST /ignore-request-body',
106+
request: expect.not.objectContaining({
107+
data: expect.any(String),
108+
}),
109+
},
110+
})
111+
.start();
112+
113+
await runner.makeRequest('post', '/test-body-size', {
114+
headers: { 'Content-Type': 'application/json' },
115+
data: JSON.stringify(generatePayload(500)),
116+
});
117+
118+
await runner.makeRequest('post', '/ignore-request-body', {
119+
headers: { 'Content-Type': 'application/json' },
120+
data: JSON.stringify(generatePayload(500)),
121+
});
122+
123+
await runner.completed();
124+
});
92125
},
93126
{ failsOnEsm: false },
94127
);

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
263263
const ipAddress = (request as { ip?: string }).ip || request.socket?.remoteAddress;
264264

265265
const url = request.url || '/';
266-
if (!ignoreIncomingRequestBody?.(url, request) || maxRequestBodySize !== 'none') {
266+
if (!ignoreIncomingRequestBody?.(url, request) && maxRequestBodySize !== 'none') {
267267
patchRequestToCaptureBody(request, isolationScope, maxRequestBodySize);
268268
}
269269

@@ -372,7 +372,7 @@ function getBreadcrumbData(request: http.ClientRequest): Partial<SanitizedReques
372372
function patchRequestToCaptureBody(
373373
req: http.IncomingMessage,
374374
isolationScope: Scope,
375-
maxRequestBodySize: 'none' | 'small' | 'medium' | 'always',
375+
maxRequestBodySize: 'small' | 'medium' | 'always',
376376
): void {
377377
let bodyByteLength = 0;
378378
const chunks: Buffer[] = [];
@@ -386,10 +386,6 @@ function patchRequestToCaptureBody(
386386
*/
387387
const callbackMap = new WeakMap();
388388

389-
if (maxRequestBodySize === 'none') {
390-
return;
391-
}
392-
393389
const maxBodySize =
394390
maxRequestBodySize === 'small' ? 1_000 : maxRequestBodySize === 'medium' ? 10_000 : MAX_BODY_BYTE_LENGTH;
395391

@@ -400,10 +396,9 @@ function patchRequestToCaptureBody(
400396
const [event, listener, ...restArgs] = args;
401397

402398
if (event === 'data') {
403-
if (DEBUG_BUILD) {
404-
logger.log(INSTRUMENTATION_NAME, 'Handling request.on("data")');
405-
logger.log(INSTRUMENTATION_NAME, `Requested maximum body size: ${maxBodySize}b`);
406-
}
399+
DEBUG_BUILD &&
400+
logger.log(INSTRUMENTATION_NAME, `Handling request.on("data") with maximum body size of ${maxBodySize}b`);
401+
407402
const callback = new Proxy(listener, {
408403
apply: (target, thisArg, args: Parameters<typeof listener>) => {
409404
try {

0 commit comments

Comments
 (0)