Skip to content

fix(fixRequestBody): check readableLength #1097

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

Merged
merged 1 commit into from
Apr 10, 2025
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
40 changes: 6 additions & 34 deletions src/handlers/fix-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ import type * as http from 'http';
import type { Request } from '../types';
import * as querystring from 'querystring';

type HandleBadRequestArgs = {
proxyReq: http.ClientRequest;
req: http.IncomingMessage;
res: http.ServerResponse;
};

/**
* Fix proxied body if bodyParser is involved.
*/
export function fixRequestBody(
proxyReq: http.ClientRequest,
req: http.IncomingMessage,
res: http.ServerResponse
): void {
export function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingMessage): void {
// skip fixRequestBody() when req.readableLength not 0 (bodyParser failure)
if (req.readableLength !== 0) {
return;
}

const requestBody = (req as Request).body;

if (!requestBody) {
Expand All @@ -28,18 +23,6 @@ export function fixRequestBody(
return;
}

// Handle bad request when unexpected "Connect: Upgrade" header is provided
if (/upgrade/gi.test(proxyReq.getHeader('Connection') as string)) {
handleBadRequest({ proxyReq, req, res });
return;
}

// Handle bad request when invalid request body is provided
if (hasInvalidKeys(requestBody)) {
handleBadRequest({ proxyReq, req, res });
return;
}

const writeBody = (bodyData: string) => {
// deepcode ignore ContentLengthInCode: bodyParser fix
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
Expand All @@ -52,14 +35,3 @@ export function fixRequestBody(
writeBody(querystring.stringify(requestBody));
}
}

function hasInvalidKeys(obj) {
return Object.keys(obj).some((key) => /[\n\r]/.test(key));
}

function handleBadRequest({ proxyReq, req, res }: HandleBadRequestArgs) {
res.writeHead(400);
res.end('Bad Request');
proxyReq.destroy();
req.destroy();
}
81 changes: 24 additions & 57 deletions test/unit/fix-request-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ const fakeProxyResponse = (): ServerResponse => {
return res;
};

const createRequestWithBody = (body: unknown): Request => {
const req = new IncomingMessage(new Socket()) as Request;
req.url = '/test_path';
req.body = body;
return req;
};

describe('fixRequestBody', () => {
it('should not write when body is undefined', () => {
const proxyRequest = fakeProxyRequest();

jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(proxyRequest, { body: undefined } as Request, fakeProxyResponse());
fixRequestBody(proxyRequest, createRequestWithBody(undefined));

expect(proxyRequest.setHeader).not.toHaveBeenCalled();
expect(proxyRequest.write).not.toHaveBeenCalled();
Expand All @@ -37,7 +44,7 @@ describe('fixRequestBody', () => {
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(proxyRequest, { body: {} } as Request, fakeProxyResponse());
fixRequestBody(proxyRequest, createRequestWithBody({}));

expect(proxyRequest.setHeader).toHaveBeenCalled();
expect(proxyRequest.write).toHaveBeenCalled();
Expand All @@ -50,11 +57,7 @@ describe('fixRequestBody', () => {
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(
proxyRequest,
{ body: { someField: 'some value' } } as Request,
fakeProxyResponse()
);
fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));

const expectedBody = JSON.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
Expand All @@ -68,11 +71,7 @@ describe('fixRequestBody', () => {
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(
proxyRequest,
{ body: { someField: 'some value' } } as Request,
fakeProxyResponse()
);
fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));

const expectedBody = querystring.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
Expand All @@ -86,11 +85,7 @@ describe('fixRequestBody', () => {
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(
proxyRequest,
{ body: { someField: 'some value' } } as Request,
fakeProxyResponse()
);
fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));

const expectedBody = querystring.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
Expand All @@ -104,62 +99,34 @@ describe('fixRequestBody', () => {
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');

fixRequestBody(
proxyRequest,
{ body: { someField: 'some value' } } as Request,
fakeProxyResponse()
);
fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));

const expectedBody = JSON.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
expect(proxyRequest.write).toHaveBeenCalledTimes(1);
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});

it('should return 400 and abort request on "Connection: Upgrade" header', () => {
it('should not fixRequestBody() when there bodyParser fails', () => {
const proxyRequest = fakeProxyRequest();
const request = { body: { someField: 'some value' } } as Request;

proxyRequest.destroy = jest.fn();
request.destroy = jest.fn();

const proxyResponse = fakeProxyResponse();
proxyRequest.setHeader('connection', 'upgrade');
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded');

jest.spyOn(proxyRequest, 'destroy');
jest.spyOn(request, 'destroy');
jest.spyOn(proxyResponse, 'writeHead');
jest.spyOn(proxyResponse, 'end');

fixRequestBody(proxyRequest, request, proxyResponse);

expect(proxyResponse.writeHead).toHaveBeenCalledWith(400);
expect(proxyResponse.end).toHaveBeenCalledTimes(1);
expect(proxyRequest.destroy).toHaveBeenCalledTimes(1);
expect(request.destroy).toHaveBeenCalledTimes(1);
});

it('should return 400 and abort request on invalid request data', () => {
const proxyRequest = fakeProxyRequest();
const request = { body: { 'INVALID \n\r DATA': '' } } as Request;

proxyRequest.destroy = jest.fn();
request.destroy = jest.fn();
const request = {
get readableLength() {
return 4444; // simulate bodyParser failure
},
} as Request;

const proxyResponse = fakeProxyResponse();
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded');

jest.spyOn(proxyRequest, 'write');
jest.spyOn(proxyRequest, 'destroy');
jest.spyOn(request, 'destroy');
jest.spyOn(proxyResponse, 'writeHead');
jest.spyOn(proxyResponse, 'end');

fixRequestBody(proxyRequest, request, proxyResponse);
fixRequestBody(proxyRequest, request);

expect(proxyResponse.writeHead).toHaveBeenCalledWith(400);
expect(proxyResponse.end).toHaveBeenCalledTimes(1);
expect(proxyRequest.destroy).toHaveBeenCalledTimes(1);
expect(request.destroy).toHaveBeenCalledTimes(1);
expect(proxyResponse.end).toHaveBeenCalledTimes(0);
expect(proxyRequest.write).toHaveBeenCalledTimes(0);
expect(proxyRequest.destroy).toHaveBeenCalledTimes(0);
});
});
Loading