diff --git a/server/src/nest/ai-connections/ai-chat-client.service.ts b/server/src/nest/ai-connections/ai-chat-client.service.ts index 787e242d2..63b2faadf 100644 --- a/server/src/nest/ai-connections/ai-chat-client.service.ts +++ b/server/src/nest/ai-connections/ai-chat-client.service.ts @@ -1,4 +1,4 @@ -import { safeFetch, safeFetchLlm } from '../../utils/ssrfGuard'; +import { safeFetch, safeFetchAdminConfigured, safeFetchLlm } from '../../utils/ssrfGuard'; import type { AiChatClient } from './ai-chat-client.interface'; import { AI_BASE_URLS, ownServerChatBase } from './ai-connections.constants'; import type { ResolvedAiConnection } from './ai-connections.types'; @@ -16,7 +16,9 @@ import { Injectable } from '@nestjs/common'; * is the adapter's. */ const cloudFetch: AiFetch = (url, init) => safeFetch(url, { ...init, signal: init.signal }, { maxBytes: null }); -const hostedFetch: AiFetch = (url, init) => cloudFetch(url, { ...init, redirect: 'error' }); + +/** The operator's HTTPS gateway can resolve to its private proxy inside the tenant network. */ +const hostedFetch: AiFetch = (url, init) => safeFetchAdminConfigured(url, init, 0, undefined, null); /** * An admin's own server, which may sit on loopback or in the LAN: the guard diff --git a/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts b/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts index 2a1306270..9774e78f6 100644 --- a/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts +++ b/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts @@ -16,7 +16,8 @@ import { OidcService } from '../../../../src/nest/oidc/oidc.service'; import { createSnapshotTestDb } from '../../../helpers/db-mock'; import { createTestModule, type TestModule } from '../../../helpers/test-module'; -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import dns from 'node:dns/promises'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; const db = createSnapshotTestDb(); let t: TestModule; @@ -76,4 +77,40 @@ describe('AiChatClientService', () => { expect(openAiWantsResponses(model), model).toBe(false); } }); + + it('uses the operator gateway through its private proxy and refuses a redirect', async () => { + const lookup = vi.spyOn(dns, 'lookup').mockResolvedValue([{ address: '10.210.0.2', family: 4 }]); + const fetch = vi.spyOn(globalThis, 'fetch'); + try { + const base = 'https://gate.example/v1'; + const client = t.get(AiChatClientService).create({ + ...connection('openrouter', 'qwen/qwen3.7-flash'), + managed: true, + baseUrl: base, + }); + const ask = () => + client.stream( + { system: 'Test instructions', messages: [{ role: 'user', text: 'Hello' }], tools: [] }, + new AbortController().signal, + ); + fetch.mockResolvedValueOnce( + new Response('data: [DONE]\n\n', { headers: { 'content-type': 'text/event-stream' } }), + ); + const events = await Array.fromAsync(ask()); + expect(events.at(-1)).toMatchObject({ type: 'finish' }); + expect(fetch).toHaveBeenLastCalledWith( + `${base}/chat/completions`, + expect.objectContaining({ redirect: 'manual' }), + ); + + fetch.mockResolvedValueOnce( + new Response(null, { status: 307, headers: { location: 'https://other.example/v1' } }), + ); + await expect(Array.fromAsync(ask())).rejects.toMatchObject({ code: 'provider_unavailable' }); + expect(fetch).toHaveBeenCalledTimes(2); + } finally { + fetch.mockRestore(); + lookup.mockRestore(); + } + }); });