[Console] Fix overriding of host header name (#59143) (#59269)

* fix: only override hostname if none was provided

Copy the behaviour or Wreck.request from 7.4. This is a regression
for setting the Host header value.

* Refactor variable name

* [skip ci] Fix comment
This commit is contained in:
Jean-Louis Leysens 2020-03-04 11:05:36 +01:00 committed by GitHub
parent 617c874e2a
commit fda06ee06a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 8 deletions

View file

@ -30,11 +30,6 @@ describe(`Console's send request`, () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
stub = sandbox.stub(http, 'request').callsFake(() => {
fakeRequest = {
abort: sinon.stub(),
on() {},
once() {},
} as any;
return fakeRequest;
});
});
@ -45,6 +40,11 @@ describe(`Console's send request`, () => {
});
it('correctly implements timeout and abort mechanism', async () => {
fakeRequest = {
abort: sinon.stub(),
on() {},
once() {},
} as any;
try {
await proxyRequest({
agent: null as any,
@ -60,4 +60,55 @@ describe(`Console's send request`, () => {
expect((fakeRequest.abort as sinon.SinonStub).calledOnce).toBe(true);
}
});
it('correctly sets the "host" header entry', async () => {
fakeRequest = {
abort: sinon.stub(),
on() {},
once(event: string, fn: any) {
if (event === 'response') {
return fn('done');
}
},
} as any;
// Don't set a host header this time
const result1 = await proxyRequest({
agent: null as any,
headers: {},
method: 'get',
payload: null as any,
timeout: 30000,
uri: new URL('http://noone.nowhere.none'),
});
expect(result1).toEqual('done');
const [httpRequestOptions1] = stub.firstCall.args;
expect((httpRequestOptions1 as any).headers).toEqual({
'content-type': 'application/json',
host: 'noone.nowhere.none', // Defaults to the provided host name
'transfer-encoding': 'chunked',
});
// Set a host header
const result2 = await proxyRequest({
agent: null as any,
headers: { Host: 'myhost' },
method: 'get',
payload: null as any,
timeout: 30000,
uri: new URL('http://noone.nowhere.none'),
});
expect(result2).toEqual('done');
const [httpRequestOptions2] = stub.secondCall.args;
expect((httpRequestOptions2 as any).headers).toEqual({
'content-type': 'application/json',
Host: 'myhost', // Uses provided host name
'transfer-encoding': 'chunked',
});
});
});

View file

@ -57,19 +57,24 @@ export const proxyRequest = ({
reject = rej;
});
const finalUserHeaders = { ...headers };
const hasHostHeader = Object.keys(finalUserHeaders).some(key => key.toLowerCase() === 'host');
if (!hasHostHeader) {
finalUserHeaders.host = hostname;
}
const req = client.request({
method: method.toUpperCase(),
// We support overriding this on a per request basis to support legacy proxy config. See ./proxy_config.
rejectUnauthorized: typeof rejectUnauthorized === 'boolean' ? rejectUnauthorized : undefined,
host: hostname,
port: port === '' ? undefined : Number(port),
port: port === '' ? undefined : parseInt(port, 10),
protocol,
path: `${pathname}${search || ''}`,
headers: {
...headers,
...finalUserHeaders,
'content-type': 'application/json',
'transfer-encoding': 'chunked',
host: hostname,
},
agent,
});