fix(cloud): route the hosted assistant through its private gateway

This commit is contained in:
Maurice
2026-12-01 19:28:28 +01:00
parent 4ad926aafc
commit 89035059ec
2 changed files with 42 additions and 3 deletions
@@ -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
@@ -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();
}
});
});