mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
fix(ssrf): answer a pinned lookup on a later tick so a failed connect cannot crash the server
Called back at once, a connect that fails right away (no route to an IPv6 address) tore the socket down while tls.connect was still setting it up; undici saw 'fetch failed' and the socket's own error then reached no listener and ended the process. The pinned lookup now answers on the next tick, as dns.lookup does.
This commit is contained in:
@@ -36,12 +36,16 @@ function mockIp(ip: string) {
|
||||
mockLookup.mockResolvedValue({ address: ip, family: ip.includes(':') ? 6 : 4 });
|
||||
}
|
||||
|
||||
type PinnedLookup = (h: string, o: object, cb: (...a: unknown[]) => void) => void;
|
||||
|
||||
/** The arguments a pinned lookup calls back with, on the later tick it answers on. */
|
||||
function answerOf(lookup: PinnedLookup, hostname: string, opts: object): Promise<unknown[]> {
|
||||
return new Promise((resolve) => lookup(hostname, opts, (...args: unknown[]) => resolve(args)));
|
||||
}
|
||||
|
||||
/** What the last pinned dispatcher hands a socket that asks for every address. */
|
||||
function pinnedList(): unknown {
|
||||
const lookup = agentCapture.options.connect.lookup as (h: string, o: object, cb: (...a: unknown[]) => void) => void;
|
||||
const seen: unknown[] = [];
|
||||
lookup('any.example', { all: true }, (...args: unknown[]) => seen.push(...args));
|
||||
return seen[1];
|
||||
async function pinnedList(): Promise<unknown> {
|
||||
return (await answerOf(agentCapture.options.connect.lookup as PinnedLookup, 'any.example', { all: true }))[1];
|
||||
}
|
||||
|
||||
describe('checkSsrf', () => {
|
||||
@@ -596,30 +600,43 @@ describe('createPinnedDispatcher', () => {
|
||||
expect(typeof dispatcher).toBe('object');
|
||||
});
|
||||
|
||||
it('pinned lookup callback calls back with the resolved IPv4 address', () => {
|
||||
it('pinned lookup callback calls back with the resolved IPv4 address', async () => {
|
||||
createPinnedDispatcher('93.184.216.34');
|
||||
const lookup = agentCapture.options?.connect?.lookup;
|
||||
expect(typeof lookup).toBe('function');
|
||||
const cb = vi.fn();
|
||||
lookup('example.com', {}, cb);
|
||||
expect(cb).toHaveBeenCalledWith(null, '93.184.216.34', 4);
|
||||
expect(await answerOf(lookup, 'example.com', {})).toEqual([null, '93.184.216.34', 4]);
|
||||
});
|
||||
|
||||
it('pinned lookup callback uses family 6 for IPv6 address', () => {
|
||||
it('pinned lookup callback uses family 6 for IPv6 address', async () => {
|
||||
createPinnedDispatcher('2001:4860:4860::8888');
|
||||
const lookup = agentCapture.options?.connect?.lookup;
|
||||
const cb = vi.fn();
|
||||
lookup('example.com', {}, cb);
|
||||
expect(cb).toHaveBeenCalledWith(null, '2001:4860:4860::8888', 6);
|
||||
expect(await answerOf(lookup, 'example.com', {})).toEqual([null, '2001:4860:4860::8888', 6]);
|
||||
});
|
||||
|
||||
it('returns array format when opts.all is true', () => {
|
||||
it('returns array format when opts.all is true', async () => {
|
||||
createPinnedDispatcher('93.184.216.34');
|
||||
const lookup = agentCapture.options?.connect?.lookup;
|
||||
const cb = vi.fn();
|
||||
lookup('example.com', { all: true }, cb);
|
||||
expect(cb).toHaveBeenCalledWith(null, [{ address: '93.184.216.34', family: 4 }]);
|
||||
expect(await answerOf(lookup, 'example.com', { all: true })).toEqual([
|
||||
null,
|
||||
[{ address: '93.184.216.34', family: 4 }],
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([{}, { all: true }])(
|
||||
'SSRF-PIN-004: the pinned lookup answers on a later tick, as dns.lookup does (%o)',
|
||||
async (opts) => {
|
||||
// A socket whose connect fails at once (no route to the address) is torn down
|
||||
// inside an answer given on the same tick, before tls.connect is done with it,
|
||||
// and its error then reaches no listener and ends the process.
|
||||
createPinnedDispatcher(['203.0.113.10', '2001:db8::10']);
|
||||
const lookup = agentCapture.options?.connect?.lookup;
|
||||
const cb = vi.fn();
|
||||
lookup('example.com', opts, cb);
|
||||
expect(cb).not.toHaveBeenCalled();
|
||||
await new Promise((resolve) => process.nextTick(resolve));
|
||||
expect(cb).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('safeFetchLlm', () => {
|
||||
@@ -945,7 +962,7 @@ describe('dual-stack names: every address is checked and every address is pinned
|
||||
resolvedIp: '203.0.113.10',
|
||||
resolvedIps: ['203.0.113.10'],
|
||||
});
|
||||
expect(pinnedList()).toEqual([{ address: '203.0.113.10', family: 4 }]);
|
||||
expect(await pinnedList()).toEqual([{ address: '203.0.113.10', family: 4 }]);
|
||||
});
|
||||
|
||||
it('SEC-DUAL-004: the pinned dispatcher hands the socket the whole checked list, IPv4 first', async () => {
|
||||
@@ -957,10 +974,8 @@ describe('dual-stack names: every address is checked and every address is pinned
|
||||
|
||||
await safeFetchFollow('https://idp.example/');
|
||||
|
||||
const lookup = agentCapture.options.connect.lookup as (h: string, o: object, cb: (...a: unknown[]) => void) => void;
|
||||
const seen: unknown[] = [];
|
||||
lookup('somebody-else.example', { all: true }, (...args: unknown[]) => seen.push(...args));
|
||||
expect(seen).toEqual([
|
||||
const lookup = agentCapture.options.connect.lookup as PinnedLookup;
|
||||
expect(await answerOf(lookup, 'somebody-else.example', { all: true })).toEqual([
|
||||
null,
|
||||
[
|
||||
{ address: '203.0.113.10', family: 4 },
|
||||
@@ -969,9 +984,7 @@ describe('dual-stack names: every address is checked and every address is pinned
|
||||
]);
|
||||
// Asked for one address, the socket gets the first of the same list; the
|
||||
// name it asks for never matters, that is the whole point of the pin.
|
||||
const single: unknown[] = [];
|
||||
lookup('somebody-else.example', {}, (...args: unknown[]) => single.push(...args));
|
||||
expect(single).toEqual([null, '203.0.113.10', 4]);
|
||||
expect(await answerOf(lookup, 'somebody-else.example', {})).toEqual([null, '203.0.113.10', 4]);
|
||||
});
|
||||
|
||||
it('SEC-DUAL-005: the admin lane never hands a metadata address in the answer to the socket', async () => {
|
||||
@@ -985,7 +998,7 @@ describe('dual-stack names: every address is checked and every address is pinned
|
||||
await safeFetchLlm('https://models.example/v1');
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
expect(pinnedList()).toEqual([{ address: '203.0.113.10', family: 4 }]);
|
||||
expect(await pinnedList()).toEqual([{ address: '203.0.113.10', family: 4 }]);
|
||||
});
|
||||
|
||||
it('SEC-DUAL-006: a redirect hop resolves and pins its own full list again', async () => {
|
||||
@@ -1002,14 +1015,7 @@ describe('dual-stack names: every address is checked and every address is pinned
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn().mockImplementation(async () => {
|
||||
const lookup = agentCapture.options.connect.lookup as (
|
||||
h: string,
|
||||
o: object,
|
||||
cb: (...a: unknown[]) => void,
|
||||
) => void;
|
||||
const seen: unknown[] = [];
|
||||
lookup('x', { all: true }, (...args: unknown[]) => seen.push(...args));
|
||||
pins.push(seen);
|
||||
pins.push(await answerOf(agentCapture.options.connect.lookup as PinnedLookup, 'x', { all: true }));
|
||||
return pins.length === 1
|
||||
? {
|
||||
status: 302,
|
||||
@@ -1117,11 +1123,13 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
await guard.safeFetchAdminConfigured('https://auth.home.example/.well-known/openid-configuration');
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
expect(pinnedList()).toEqual([v4('10.0.40.239')]);
|
||||
expect(await pinnedList()).toEqual([v4('10.0.40.239')]);
|
||||
// A socket that asks for one address gets the same vetted one.
|
||||
const single: unknown[] = [];
|
||||
agentCapture.options.connect.lookup('auth.home.example', {}, (...args: unknown[]) => single.push(...args));
|
||||
expect(single).toEqual([null, '10.0.40.239', 4]);
|
||||
expect(await answerOf(agentCapture.options.connect.lookup as PinnedLookup, 'auth.home.example', {})).toEqual([
|
||||
null,
|
||||
'10.0.40.239',
|
||||
4,
|
||||
]);
|
||||
});
|
||||
|
||||
it('SEC-2506-002: link-local records ahead of the IPv4 change nothing', async () => {
|
||||
@@ -1130,10 +1138,11 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
await guard.safeFetchAdminConfigured('http://maurice-pc:3191/.well-known/openid-configuration');
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
expect(pinnedList()).toEqual([v4('192.168.178.36')]);
|
||||
expect(await pinnedList()).toEqual([v4('192.168.178.36')]);
|
||||
});
|
||||
|
||||
it('SEC-2506-003: no link-local or metadata spelling reaches the socket, whatever ALLOW_INTERNAL_NETWORK says', async () => {
|
||||
const pinned: Promise<unknown>[] = [];
|
||||
for (const flag of ['true', 'false']) {
|
||||
const { guard } = await guardWith({ ALLOW_INTERNAL_NETWORK: flag }, [
|
||||
...METADATA_AND_LINK_LOCAL,
|
||||
@@ -1143,8 +1152,13 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
|
||||
await guard.safeFetchAdminConfigured('https://auth.home.example/');
|
||||
|
||||
expect(pinnedList()).toEqual([v4('10.0.40.239'), v6('fd12:3456::1')]);
|
||||
// Asked now, answered on the next tick: the list of this flag's dispatcher.
|
||||
pinned.push(pinnedList());
|
||||
}
|
||||
expect(await Promise.all(pinned)).toEqual([
|
||||
[v4('10.0.40.239'), v6('fd12:3456::1')],
|
||||
[v4('10.0.40.239'), v6('fd12:3456::1')],
|
||||
]);
|
||||
});
|
||||
|
||||
it('SEC-2506-004: a name with nothing but link-local and metadata records is still refused', async () => {
|
||||
@@ -1166,7 +1180,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
|
||||
await guard.safeFetchAdminConfigured('https://keycloak.example.com/');
|
||||
|
||||
expect(pinnedList()).toEqual([v4('169.254.1.2')]);
|
||||
expect(await pinnedList()).toEqual([v4('169.254.1.2')]);
|
||||
});
|
||||
|
||||
it('SEC-2506-006: every redirect hop leaves out its own link-local records and pins what is left', async () => {
|
||||
@@ -1174,7 +1188,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
lookup.mockResolvedValueOnce(REPORTED as never).mockResolvedValueOnce(LINK_LOCAL_FIRST as never);
|
||||
const pins: unknown[] = [];
|
||||
fetchSpy.mockImplementation(async () => {
|
||||
pins.push(pinnedList());
|
||||
pins.push(await pinnedList());
|
||||
return pins.length === 1
|
||||
? redirectTo('https://second.home.example/')
|
||||
: { status: 200, ok: true, headers: new Headers() };
|
||||
@@ -1213,7 +1227,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
await guard.safeFetch('https://immich.home.example/api/users/me');
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
expect(pinnedList()).toEqual([v4('10.0.40.239')]);
|
||||
expect(await pinnedList()).toEqual([v4('10.0.40.239')]);
|
||||
});
|
||||
|
||||
it('SEC-2506-011: with the flag off the refusal names ALLOW_INTERNAL_NETWORK, not link-local', async () => {
|
||||
@@ -1253,7 +1267,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
});
|
||||
await guard.safeFetchFollow('https://photos.example/');
|
||||
|
||||
expect(pinnedList()).toEqual([v4('203.0.113.10'), v6('2001:db8::10')]);
|
||||
expect(await pinnedList()).toEqual([v4('203.0.113.10'), v6('2001:db8::10')]);
|
||||
});
|
||||
|
||||
it('SEC-2506-013: a name whose every record is always blocked is refused as before', async () => {
|
||||
@@ -1316,7 +1330,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
resolvedIps: ['169.254.1.2'],
|
||||
});
|
||||
await guard.safeFetch('https://immich.example/');
|
||||
expect(pinnedList()).toEqual([v4('169.254.1.2')]);
|
||||
expect(await pinnedList()).toEqual([v4('169.254.1.2')]);
|
||||
});
|
||||
|
||||
// ALLOW_INTERNAL_NETWORK opens ULA and CGNAT space, and the AWS IMDSv6 and
|
||||
@@ -1335,7 +1349,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
resolvedIps: ['10.0.40.239', 'fd12:3456::1'],
|
||||
});
|
||||
await lan.guard.safeFetchFollow('https://immich.home.example/');
|
||||
expect(pinnedList()).toEqual([v4('10.0.40.239'), v6('fd12:3456::1')]);
|
||||
expect(await pinnedList()).toEqual([v4('10.0.40.239'), v6('fd12:3456::1')]);
|
||||
|
||||
const pub = await guardWith({ ALLOW_INTERNAL_NETWORK: 'false' }, [
|
||||
...METADATA_AND_LINK_LOCAL,
|
||||
@@ -1348,7 +1362,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
resolvedIps: ['203.0.113.10', '2001:db8::10'],
|
||||
});
|
||||
await pub.guard.safeFetchFollow('https://photos.example/');
|
||||
expect(pinnedList()).toEqual([v4('203.0.113.10'), v6('2001:db8::10')]);
|
||||
expect(await pinnedList()).toEqual([v4('203.0.113.10'), v6('2001:db8::10')]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -1387,7 +1401,7 @@ describe('a name with a link-local record next to a usable one (#2506)', () => {
|
||||
resolvedIps: ['10.0.0.5'],
|
||||
});
|
||||
await guard.safeFetch('https://nas.home.example/');
|
||||
expect(pinnedList()).toEqual([v4('10.0.0.5')]);
|
||||
expect(await pinnedList()).toEqual([v4('10.0.0.5')]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user