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(core): Handle gzip and deflate compressed request payloads #7461

Merged
merged 1 commit into from
Oct 18, 2023
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
19 changes: 17 additions & 2 deletions packages/cli/src/middlewares/bodyParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import getRawBody from 'raw-body';
import { type Readable } from 'stream';
import { createGunzip, createInflate } from 'zlib';
import type { Request, RequestHandler } from 'express';
import { parse as parseQueryString } from 'querystring';
import { Parser as XmlParser } from 'xml2js';
Expand All @@ -20,8 +22,21 @@ export const rawBodyReader: RequestHandler = async (req, res, next) => {

req.readRawBody = async () => {
if (!req.rawBody) {
req.rawBody = await getRawBody(req, {
length: req.headers['content-length'],
let stream: Readable = req;
let contentLength: string | undefined;
const contentEncoding = req.headers['content-encoding'];
switch (contentEncoding) {
case 'gzip':
stream = req.pipe(createGunzip());
break;
case 'deflate':
stream = req.pipe(createInflate());
break;
default:
contentLength = req.headers['content-length'];
}
req.rawBody = await getRawBody(stream, {
length: contentLength,
limit: `${String(payloadSizeMax)}mb`,
});
req._body = true;
Expand Down
40 changes: 40 additions & 0 deletions packages/cli/test/integration/middlewares/bodyParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createServer } from 'http';
import { gzipSync, deflateSync } from 'zlib';
import type { Request, Response } from 'express';
import request from 'supertest';
import { rawBodyReader, bodyParser } from '@/middlewares/bodyParser';

describe('bodyParser', () => {
const server = createServer((req: Request, res: Response) => {
rawBodyReader(req, res, async () => {
bodyParser(req, res, () => res.end(JSON.stringify(req.body)));
});
});

it('should handle uncompressed data', async () => {
const response = await request(server).post('/').send({ hello: 'world' }).expect(200);
expect(response.text).toEqual('{"hello":"world"}');
});

it('should handle gzip data', async () => {
const response = await request(server)
.post('/')
.set('content-encoding', 'gzip')
// @ts-ignore
.serialize((d) => gzipSync(JSON.stringify(d)))
.send({ hello: 'world' })
.expect(200);
expect(response.text).toEqual('{"hello":"world"}');
});

it('should handle deflate data', async () => {
const response = await request(server)
.post('/')
.set('content-encoding', 'deflate')
// @ts-ignore
.serialize((d) => deflateSync(JSON.stringify(d)))
.send({ hello: 'world' })
.expect(200);
expect(response.text).toEqual('{"hello":"world"}');
});
});
Loading