From 41772c0d8ee79c64aef09e47814d3da0ac60b0a1 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 28 Nov 2026 18:12:39 +0100 Subject: [PATCH] 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. --- server/src/utils/ssrfGuard.ts | 18 ++- .../tests/unit/utils/ssrf-llm-timeout.test.ts | 7 +- server/tests/unit/utils/ssrf-proxy.test.ts | 5 +- server/tests/unit/utils/ssrfGuard.test.ts | 110 ++++++++++-------- 4 files changed, 81 insertions(+), 59 deletions(-) diff --git a/server/src/utils/ssrfGuard.ts b/server/src/utils/ssrfGuard.ts index cfb0bd80a..f64460e3f 100644 --- a/server/src/utils/ssrfGuard.ts +++ b/server/src/utils/ssrfGuard.ts @@ -796,12 +796,18 @@ export function createPinnedDispatcher( connect: { rejectUnauthorized, lookup: (_hostname: string, opts: Record, callback: Function) => { - // Node asks with `all: true` when it may choose between families itself. - if (opts?.all) { - callback(null, addresses); - } else { - callback(null, addresses[0].address, addresses[0].family); - } + // Answered on a later tick, as dns.lookup answers. Called back at once, a + // connect that fails right away (no route to the address) tears the socket + // down while tls.connect still sets it up, and its error then crashes the + // process because nothing listens for it yet. + process.nextTick(() => { + // Node asks with `all: true` when it may choose between families itself. + if (opts?.all) { + callback(null, addresses); + } else { + callback(null, addresses[0].address, addresses[0].family); + } + }); }, }, }); diff --git a/server/tests/unit/utils/ssrf-llm-timeout.test.ts b/server/tests/unit/utils/ssrf-llm-timeout.test.ts index fe143cd35..842249dca 100644 --- a/server/tests/unit/utils/ssrf-llm-timeout.test.ts +++ b/server/tests/unit/utils/ssrf-llm-timeout.test.ts @@ -81,13 +81,14 @@ describe('createPinnedDispatcher — response ceiling', () => { expect(optionsOf().bodyTimeout).toBeUndefined(); }); - it('still pins the connection to the validated IP', () => { + it('still pins the connection to the validated IP', async () => { createPinnedDispatcher('10.0.0.5', true, 900_000); const lookup = (optionsOf().connect as { lookup: (h: string, o: object, cb: (...a: unknown[]) => void) => void }) .lookup; - const seen: unknown[] = []; - lookup('evil.example', {}, (...args: unknown[]) => seen.push(...args)); + const seen = await new Promise((resolve) => + lookup('evil.example', {}, (...args: unknown[]) => resolve(args)), + ); expect(seen).toContain('10.0.0.5'); }); }); diff --git a/server/tests/unit/utils/ssrf-proxy.test.ts b/server/tests/unit/utils/ssrf-proxy.test.ts index 67ce637b6..d02bc77df 100644 --- a/server/tests/unit/utils/ssrf-proxy.test.ts +++ b/server/tests/unit/utils/ssrf-proxy.test.ts @@ -152,8 +152,9 @@ describe('createOutboundDispatcher, connections kept (the routing relay)', () => const lookup = ( built.agents[1] as { connect: { lookup: (h: string, o: object, cb: (...a: unknown[]) => void) => void } } ).connect.lookup; - const seen: unknown[] = []; - lookup('valhalla.liketrek.com', {}, (...args: unknown[]) => seen.push(...args)); + const seen = await new Promise((resolve) => + lookup('valhalla.liketrek.com', {}, (...args: unknown[]) => resolve(args)), + ); expect(seen).toContain('203.0.113.8'); expect(seen).not.toContain('203.0.113.7'); }); diff --git a/server/tests/unit/utils/ssrfGuard.test.ts b/server/tests/unit/utils/ssrfGuard.test.ts index 336e1c333..41960b805 100644 --- a/server/tests/unit/utils/ssrfGuard.test.ts +++ b/server/tests/unit/utils/ssrfGuard.test.ts @@ -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 { + 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 { + 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[] = []; 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')]); }); }); });