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-proxy): remove disallowed headers when response headers contains trailer #830

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/handlers/response-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ type Interceptor = (
res: http.ServerResponse
) => Promise<Buffer | string>;

const TrailerDisallowHeaders: string[] = [
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add a link to verify the completeness of these headers?

Copy link
Author

Choose a reason for hiding this comment

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

'content-length',
'host',
'content-type',
'authorization',
'cache-control',
'max-forwards',
'te',
'set-cookie',
'content-encoding',
'content-range',
];

/**
* Intercept responses from upstream.
* Automatically decompress (deflate, gzip, brotli).
Expand Down Expand Up @@ -45,8 +58,14 @@ export function responseInterceptor(interceptor: Interceptor) {

// set correct content-length (with double byte character support)
debug('set content-length: %s', Buffer.byteLength(interceptedBuffer, 'utf8'));
res.setHeader('content-length', Buffer.byteLength(interceptedBuffer, 'utf8'));

// some headers are disallowed when response headers contains trailer
if (proxyRes.headers.trailer === undefined) {
res.setHeader('content-length', Buffer.byteLength(interceptedBuffer, 'utf8'));
} else {
TrailerDisallowHeaders.forEach((value) => {
res.removeHeader(value);
});
}
debug('write intercepted response');
res.write(interceptedBuffer);
res.end();
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/response-interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ describe('responseInterceptor()', () => {
const response = await agent.get(`/json`).expect(200);
expect(response.body.favorite).toEqual('叉燒包');
});

it('should not contains disallow headers to trailer in response headers http://httpbin.org/response-headers', async () => {
const response = await agent
.get('/response-headers?Trailer=X-Stream-Error&Host=localhost')
.expect(200);
expect(response.header['host']).toBeUndefined();
});
});

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