Skip to content

Commit c7fb6e1

Browse files
authored
ref: Skip body parsing for GET/HEAD requests (getsentry#2504)
1 parent dea97aa commit c7fb6e1

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

packages/node/src/handlers.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,14 @@ function extractRequestData(req: { [key: string]: any }, keys: boolean | string[
148148
request.query_string = url.parse(originalUrl || '', false).query;
149149
break;
150150
case 'data':
151-
// body data:
152-
// node, express, koa: req.body
153-
let data = req.body;
154151
if (method === 'GET' || method === 'HEAD') {
155-
if (typeof data === 'undefined') {
156-
data = '<unavailable>';
157-
}
152+
break;
158153
}
159-
if (data && !isString(data)) {
160-
// Make sure the request body is a string
161-
data = JSON.stringify(normalize(data));
154+
// body data:
155+
// node, express, koa: req.body
156+
if (req.body !== undefined) {
157+
request.data = isString(req.body) ? req.body : JSON.stringify(normalize(req.body));
162158
}
163-
request.data = data;
164159
break;
165160
default:
166161
if ({}.hasOwnProperty.call(req, key)) {

packages/node/test/handlers.test.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { parseRequest } from '../src/handlers';
33

44
describe('parseRequest', () => {
55
const mockReq = {
6-
body: '',
6+
body: 'foo',
77
cookies: { test: 'test' },
88
headers: {
99
host: 'mattrobenolt.com',
1010
},
11-
method: 'GET',
11+
method: 'POST',
1212
url: '/some/path?key=value',
1313
user: {
1414
custom_property: 'foo',
@@ -72,16 +72,22 @@ describe('parseRequest', () => {
7272
describe('parseRequest.request properties', () => {
7373
test('parseRequest.request only contains the default set of properties from the request', () => {
7474
const DEFAULT_REQUEST_PROPERTIES = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];
75-
const parsedRequest: Event = parseRequest({}, mockReq, {});
76-
expect(Object.keys(parsedRequest.request as any[])).toEqual(DEFAULT_REQUEST_PROPERTIES);
75+
const parsedRequest: Event = parseRequest({}, mockReq);
76+
expect(Object.keys(parsedRequest.request as {})).toEqual(DEFAULT_REQUEST_PROPERTIES);
7777
});
7878

7979
test('parseRequest.request only contains the specified properties in the options.request array', () => {
8080
const INCLUDED_PROPERTIES = ['data', 'headers', 'query_string', 'url'];
8181
const parsedRequest: Event = parseRequest({}, mockReq, {
8282
request: INCLUDED_PROPERTIES,
8383
});
84-
expect(Object.keys(parsedRequest.request as any[])).toEqual(['data', 'headers', 'query_string', 'url']);
84+
expect(Object.keys(parsedRequest.request as {})).toEqual(INCLUDED_PROPERTIES);
85+
});
86+
87+
test('parseRequest.request skips `body` property for GET and HEAD requests', () => {
88+
expect(parseRequest({}, mockReq, {}).request).toHaveProperty('data');
89+
expect(parseRequest({}, { ...mockReq, method: 'GET' }, {}).request).not.toHaveProperty('data');
90+
expect(parseRequest({}, { ...mockReq, method: 'HEAD' }, {}).request).not.toHaveProperty('data');
8591
});
8692
});
8793
});

0 commit comments

Comments
 (0)