Skip to content

Commit 8fffd3e

Browse files
committed
fix(http-proxy): remove disallowed headers when response header contains trailer
Signed-off-by: zibo <ziboilihua@gmail.com>
1 parent 2cced43 commit 8fffd3e

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/handlers/response-interceptor.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ type Interceptor = (
1212
res: http.ServerResponse
1313
) => Promise<Buffer | string>;
1414

15+
const TrailerDisallowHeaders: string[] = [
16+
'content-length',
17+
'host',
18+
'content-type',
19+
'authorization',
20+
'cache-control',
21+
'max-forwards',
22+
'te',
23+
'set-cookie',
24+
'content-encoding',
25+
'content-range',
26+
];
27+
1528
/**
1629
* Intercept responses from upstream.
1730
* Automatically decompress (deflate, gzip, brotli).
@@ -45,8 +58,14 @@ export function responseInterceptor(interceptor: Interceptor) {
4558

4659
// set correct content-length (with double byte character support)
4760
debug('set content-length: %s', Buffer.byteLength(interceptedBuffer, 'utf8'));
48-
res.setHeader('content-length', Buffer.byteLength(interceptedBuffer, 'utf8'));
49-
61+
// some headers are disallowed when response headers contains trailer
62+
if (proxyRes.headers.trailer === undefined) {
63+
res.setHeader('content-length', Buffer.byteLength(interceptedBuffer, 'utf8'));
64+
} else {
65+
TrailerDisallowHeaders.forEach((value) => {
66+
res.removeHeader(value);
67+
});
68+
}
5069
debug('write intercepted response');
5170
res.write(interceptedBuffer);
5271
res.end();

test/e2e/response-interceptor.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ describe('responseInterceptor()', () => {
4141
const response = await agent.get(`/json`).expect(200);
4242
expect(response.body.favorite).toEqual('叉燒包');
4343
});
44+
45+
it('should not contains disallow headers to trailer in response headers http://httpbin.org/response-headers', async () => {
46+
const response = await agent
47+
.get('/response-headers?Trailer=X-Stream-Error&Host=localhost')
48+
.expect(200);
49+
expect(response.header['host']).toBeUndefined();
50+
});
4451
});
4552

4653
describe('intercept responses with original headers', () => {

0 commit comments

Comments
 (0)