Skip to content

Commit

Permalink
Fix options.port on redirect (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giotino committed Sep 5, 2020
1 parent d12d6af commit 408e22a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
decodeURI(redirectString);

// Redirecting to a different site, clear sensitive data.
if (redirectUrl.hostname !== url.hostname) {
if (redirectUrl.hostname !== url.hostname || redirectUrl.port !== url.port) {
if ('host' in options.headers) {
delete options.headers.host;
}
Expand All @@ -2088,6 +2088,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
// @ts-expect-error
delete options.password;
}

if ('port' in options) {
delete options.port;
}
}

this.redirects.push(redirectString);
Expand Down
25 changes: 25 additions & 0 deletions test/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,28 @@ test('clears the host header when redirecting to a different hostname', async t
const resp = await got('https://testweb.com/redirect', {headers: {host: 'wrongsite.com'}});
t.is(resp.body, 'webtest.com');
});

test('correct port on redirect', withServer, async (t, server1, got) => {
await withServer(t, async (t, server2) => {
server1.get('/redirect', (_request, response) => {
response.redirect(`http://${server2.hostname}:${server2.port}/`);
});

server1.get('/', (_request, response) => {
response.end('SERVER1');
});

server2.get('/', (_request, response) => {
response.end('SERVER2');
});

const response = await got({
protocol: 'http:',
hostname: server1.hostname,
port: server1.port,
pathname: '/redirect'
});

t.is(response.body, 'SERVER2');
});
});

0 comments on commit 408e22a

Please sign in to comment.