Skip to content

Commit

Permalink
Do not ignore userinfo on a redirect to the same origin
Browse files Browse the repository at this point in the history
Fixes #1351
  • Loading branch information
szmarczak committed Sep 17, 2020
1 parent 38ba09c commit 52de13b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 9 additions & 9 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

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

// Make it possible to change `options.prefixUrl`
let {prefixUrl} = options;
Object.defineProperty(options, 'prefixUrl', {
Expand Down Expand Up @@ -2088,16 +2092,12 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

if (options.username || options.password) {
// TODO: Fix this ignore.
// @ts-expect-error
delete options.username;
// @ts-expect-error
delete options.password;
}

if ('port' in options) {
delete options.port;
options.username = '';
options.password = '';
}
} else {
redirectUrl.username = options.username;
redirectUrl.password = options.password;
}

this.redirects.push(redirectString);
Expand Down
16 changes: 16 additions & 0 deletions test/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,22 @@ test('clears the authorization header when redirecting to a different hostname',
t.is(headers.Authorization, undefined);
});

test('preserves userinfo on redirect to the same origin', withServer, async (t, server) => {
server.get('/redirect', (_request, response) => {
response.writeHead(303, {
location: `http://localhost:${server.port}/`
});
response.end();
});

server.get('/', (request, response) => {
t.is(request.headers.authorization, 'Basic aGVsbG86d29ybGQ=');
response.end();
});

await got(`http://hello:world@localhost:${server.port}/redirect`);
});

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) {
Expand Down

0 comments on commit 52de13b

Please sign in to comment.