Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http-server): discard request body if the content-length header i… #2103

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have taken the freedom to add a NVM RC file. Let me know if you would like this out

Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,27 @@ describe('body params validation', () => {
});

describe('and no content is specified', () => {
test('returns 200', async () => {
test('returns 200 with no body', async () => {
const response = await makeRequest('/empty-body', {
method: 'GET',
headers: { 'content-type': 'application/json' },
});
expect(response.status).toBe(200);
});
test('returns 200 with empty plain body', async () => {
const response = await makeRequest('/empty-body', {
method: 'GET',
headers: { 'content-type': 'text/plain', 'content-length': '0' },
});
expect(response.status).toBe(200);
});
test('returns 200 with empty JSON body', async () => {
const response = await makeRequest('/empty-body', {
method: 'GET',
headers: { 'content-type': 'application/json', 'content-length': '0' },
});
expect(response.status).toBe(200);
});
});
});

Expand Down Expand Up @@ -707,7 +721,7 @@ describe('body params validation', () => {
test('returns 422', async () => {
const response = await makeRequest('/path', {
method: 'POST',
body: '',
body: 'success=false',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
});

Expand Down
8 changes: 5 additions & 3 deletions packages/http-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ function addressInfoToString(addressInfo: AddressInfo | string | null) {
function parseRequestBody(request: IncomingMessage) {
// if no body provided then return null instead of empty string
if (
request.headers['content-type'] === undefined &&
request.headers['transfer-encoding'] === undefined &&
(request.headers['content-length'] === '0' || request.headers['content-length'] === undefined)
// If the body size is null, it means the body itself is null so the promise can resolve with a null value
request.headers['content-length'] === '0' ||
(request.headers['content-type'] === undefined &&
request.headers['transfer-encoding'] === undefined &&
request.headers['content-length'] === undefined)
) {
return Promise.resolve(null);
}
Expand Down