Skip to content

Commit bce8ce7

Browse files
committed
Backport 861ccd9
1 parent 8ced192 commit bce8ce7

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

source/core/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,16 @@ export default class Request extends Duplex implements RequestEvents<Request> {
21002100
const redirectString = redirectUrl.toString();
21012101
decodeURI(redirectString);
21022102

2103+
// eslint-disable-next-line no-inner-declarations
2104+
function isUnixSocketURL(url: URL) {
2105+
return url.protocol === 'unix:' || url.hostname === 'unix';
2106+
}
2107+
2108+
if (!isUnixSocketURL(url) && isUnixSocketURL(redirectUrl)) {
2109+
this._beforeError(new RequestError('Cannot redirect to UNIX socket', {}, this));
2110+
return;
2111+
}
2112+
21032113
// Redirecting to a different site, clear sensitive data.
21042114
if (redirectUrl.hostname !== url.hostname || redirectUrl.port !== url.port) {
21052115
if ('host' in options.headers) {

test/redirects.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22
import {Handler} from 'express';
33
import nock = require('nock');
4-
import got, {MaxRedirectsError} from '../source';
4+
import got, {MaxRedirectsError, RequestError} from '../source';
55
import withServer, {withHttpsServer} from './helpers/with-server';
66

77
const reachedHandler: Handler = (_request, response) => {
@@ -509,3 +509,32 @@ test('correct port on redirect', withServer, async (t, server1, got) => {
509509
t.is(response.body, 'SERVER2');
510510
});
511511
});
512+
513+
const unixProtocol: Handler = (_request, response) => {
514+
response.writeHead(302, {
515+
location: 'unix:/var/run/docker.sock:/containers/json'
516+
});
517+
response.end();
518+
};
519+
520+
const unixHostname: Handler = (_request, response) => {
521+
response.writeHead(302, {
522+
location: 'http://unix:/var/run/docker.sock:/containers/json'
523+
});
524+
response.end();
525+
};
526+
527+
test('cannot redirect to unix protocol', withServer, async (t, server, got) => {
528+
server.get('/protocol', unixProtocol);
529+
server.get('/hostname', unixHostname);
530+
531+
await t.throwsAsync(got('protocol'), {
532+
message: 'Cannot redirect to UNIX socket',
533+
instanceOf: RequestError
534+
});
535+
536+
await t.throwsAsync(got('hostname'), {
537+
message: 'Cannot redirect to UNIX socket',
538+
instanceOf: RequestError
539+
});
540+
});

test/unix-socket.ts

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ const okHandler: Handler = (_request, response) => {
88
response.end('ok');
99
};
1010

11+
const redirectHandler: Handler = (_request, response) => {
12+
response.writeHead(302, {
13+
location: 'foo'
14+
});
15+
response.end();
16+
};
17+
1118
if (process.platform !== 'win32') {
1219
test('works', withSocketServer, async (t, server) => {
1320
server.on('/', okHandler);
@@ -53,3 +60,11 @@ if (process.platform !== 'win32') {
5360
t.is((await got(url)).body, 'ok');
5461
});
5562
}
63+
64+
test('redirects work', withSocketServer, async (t, server) => {
65+
server.on('/', redirectHandler);
66+
server.on('/foo', okHandler);
67+
68+
const url = format('http://unix:%s:%s', server.socketPath, '/');
69+
t.is((await got(url)).body, 'ok');
70+
});

0 commit comments

Comments
 (0)