fix(security): re-validate LLM endpoint fetch redirects per hop (GHSA-fmq9)

safeFetchLlm left undici's default redirect:'follow', so a configured LLM
endpoint could 302 to http://169.254.169.254/ and reach cloud-metadata
credentials — the DNS pin does not cover an IP-literal redirect hop, since
net.connect skips the pinned lookup for a literal IP. Follow redirects
manually now, re-resolving/re-checking/re-pinning each hop (allowing LAN/
localhost as before). Also block the Alibaba metadata IPs directly.
This commit is contained in:
jubnl
2026-07-11 21:57:59 +02:00
parent 30d09a019c
commit 4ad8146dbf
2 changed files with 133 additions and 25 deletions
+71
View File
@@ -431,4 +431,75 @@ describe('safeFetchLlm', () => {
await safeFetchLlm('http://192.168.1.50:8000/v1/chat/completions');
expect(mockFetch).toHaveBeenCalledTimes(1);
});
// Regression (GHSA-fmq9-ggh3-647p follow-up): the guard must re-validate on EVERY
// redirect hop, not just the initial URL. A public endpoint that 302s to the
// metadata IP-literal would otherwise slip through — the DNS pin does not cover an
// IP-literal redirect hop (Node's net.connect skips the pinned lookup for a literal IP).
function llmResponse(opts: { status: number; location?: string }) {
return {
status: opts.status,
ok: opts.status >= 200 && opts.status < 300,
headers: { get: (h: string) => (h.toLowerCase() === 'location' ? (opts.location ?? null) : null) },
body: { cancel: () => Promise.resolve() },
};
}
it('requests each hop with redirect:manual so the platform never auto-follows', async () => {
mockIp('203.0.113.10');
const mockFetch = vi.fn().mockResolvedValue(llmResponse({ status: 200 }));
vi.stubGlobal('fetch', mockFetch);
await safeFetchLlm('https://api.provider.example/v1/chat/completions');
expect(mockFetch.mock.calls[0][1]).toMatchObject({ redirect: 'manual' });
});
it('blocks a redirect from a public endpoint to the metadata IP-literal', async () => {
mockLookup
.mockResolvedValueOnce({ address: '203.0.113.10', family: 4 }) // configured endpoint (public)
.mockResolvedValue({ address: '169.254.169.254', family: 4 }); // redirect target → metadata
const mockFetch = vi.fn().mockResolvedValueOnce(
llmResponse({ status: 302, location: 'http://169.254.169.254/latest/meta-data/' }),
);
vi.stubGlobal('fetch', mockFetch);
await expect(
safeFetchLlm('https://api.provider.example/v1/chat/completions', { method: 'POST' }),
).rejects.toThrow(SsrfBlockedError);
// The metadata hop is refused BEFORE its fetch — only the initial hop ran.
expect(mockFetch).toHaveBeenCalledTimes(1);
});
it('follows a legitimate redirect between allowed hosts (http→https upgrade) to the final response', async () => {
mockLookup
.mockResolvedValueOnce({ address: '127.0.0.1', family: 4 }) // http Ollama
.mockResolvedValue({ address: '127.0.0.1', family: 4 }); // https upgrade, same host
const mockFetch = vi
.fn()
.mockResolvedValueOnce(llmResponse({ status: 301, location: 'https://localhost:11434/v1/chat/completions' }))
.mockResolvedValueOnce(llmResponse({ status: 200 }));
vi.stubGlobal('fetch', mockFetch);
const res = await safeFetchLlm('http://localhost:11434/v1/chat/completions', { method: 'POST' });
expect(mockFetch).toHaveBeenCalledTimes(2);
expect(res.status).toBe(200);
});
it('blocks the Alibaba Cloud metadata IPs directly (CGNAT range is otherwise allowed for LAN)', async () => {
vi.stubGlobal('fetch', vi.fn());
mockIp('100.100.100.200');
await expect(safeFetchLlm('http://model.example/chat')).rejects.toThrow(SsrfBlockedError);
mockIp('100.100.100.100');
await expect(safeFetchLlm('http://model.example/chat')).rejects.toThrow(SsrfBlockedError);
});
it('stops following after the redirect cap', async () => {
mockIp('203.0.113.10');
const mockFetch = vi
.fn()
.mockResolvedValue(llmResponse({ status: 302, location: 'https://api.provider.example/next' }));
vi.stubGlobal('fetch', mockFetch);
await expect(
safeFetchLlm('https://api.provider.example/v1/chat/completions', undefined, 2),
).rejects.toThrow(/Too many redirects/i);
// initial + 2 allowed hops = 3 fetches, then the 4th is refused before fetch.
expect(mockFetch).toHaveBeenCalledTimes(3);
});
});