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

Remove the host header on redirect #1241

Merged
merged 3 commits into from May 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/core/index.ts
Expand Up @@ -1051,6 +1051,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {

// Redirecting to a different site, clear sensitive data.
if (redirectUrl.hostname !== url.hostname) {
if ('host' in options.headers) {
delete options.headers.host;
}

if ('cookie' in options.headers) {
delete options.headers.cookie;
}
Expand Down
12 changes: 11 additions & 1 deletion test/redirects.ts
Expand Up @@ -2,7 +2,7 @@ import {TLSSocket} from 'tls';
import test from 'ava';
import {Handler} from 'express';
import nock = require('nock');
import {MaxRedirectsError} from '../source';
import got, {MaxRedirectsError} from '../source';
import withServer from './helpers/with-server';

const reachedHandler: Handler = (_request, response) => {
Expand Down Expand Up @@ -432,3 +432,13 @@ test('clears the authorization header when redirecting to a different hostname',
}).json();
t.is(headers.Authorization, undefined);
});

test('clears the host header when redirecting to a different hostname', async t => {
nock('https://testweb.com').get('/redirect').reply(302, undefined, {location: 'https://webtest.com/'});
nock('https://webtest.com').get('/').reply(function (_uri, _body) {
return [200, this.req.getHeader('host')];
});

const resp = await got('https://testweb.com/redirect', {headers: {host: 'wrongsite.com'}});
t.is(resp.body, 'webtest.com');
});