Skip to content

Commit 52de13b

Browse files
committedSep 17, 2020
Do not ignore userinfo on a redirect to the same origin
Fixes #1351
1 parent 38ba09c commit 52de13b

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed
 

‎source/core/index.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
16311631
}
16321632

16331633
if (options.url) {
1634+
if ('port' in options) {
1635+
delete options.port;
1636+
}
1637+
16341638
// Make it possible to change `options.prefixUrl`
16351639
let {prefixUrl} = options;
16361640
Object.defineProperty(options, 'prefixUrl', {
@@ -2088,16 +2092,12 @@ export default class Request extends Duplex implements RequestEvents<Request> {
20882092
}
20892093

20902094
if (options.username || options.password) {
2091-
// TODO: Fix this ignore.
2092-
// @ts-expect-error
2093-
delete options.username;
2094-
// @ts-expect-error
2095-
delete options.password;
2096-
}
2097-
2098-
if ('port' in options) {
2099-
delete options.port;
2095+
options.username = '';
2096+
options.password = '';
21002097
}
2098+
} else {
2099+
redirectUrl.username = options.username;
2100+
redirectUrl.password = options.password;
21012101
}
21022102

21032103
this.redirects.push(redirectString);

‎test/redirects.ts

+16
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,22 @@ test('clears the authorization header when redirecting to a different hostname',
440440
t.is(headers.Authorization, undefined);
441441
});
442442

443+
test('preserves userinfo on redirect to the same origin', withServer, async (t, server) => {
444+
server.get('/redirect', (_request, response) => {
445+
response.writeHead(303, {
446+
location: `http://localhost:${server.port}/`
447+
});
448+
response.end();
449+
});
450+
451+
server.get('/', (request, response) => {
452+
t.is(request.headers.authorization, 'Basic aGVsbG86d29ybGQ=');
453+
response.end();
454+
});
455+
456+
await got(`http://hello:world@localhost:${server.port}/redirect`);
457+
});
458+
443459
test('clears the host header when redirecting to a different hostname', async t => {
444460
nock('https://testweb.com').get('/redirect').reply(302, undefined, {location: 'https://webtest.com/'});
445461
nock('https://webtest.com').get('/').reply(function (_uri, _body) {

0 commit comments

Comments
 (0)
Please sign in to comment.