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

Fixed options.port on redirect #1439

Merged
merged 3 commits into from
Sep 5, 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
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');
});
});