From 0b4cb82c5619c28981c5053a548ff058f3ca5c2e Mon Sep 17 00:00:00 2001 From: aiirvizionz Date: Thu, 23 Jul 2026 12:14:54 -0600 Subject: [PATCH] fix(ai-nebius): require clean api base urls --- packages/ai/nebius/src/index.test.ts | 25 ++++++++++++++++++++++--- packages/ai/nebius/src/index.ts | 22 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/ai/nebius/src/index.test.ts b/packages/ai/nebius/src/index.test.ts index bbdc5802..023ab303 100644 --- a/packages/ai/nebius/src/index.test.ts +++ b/packages/ai/nebius/src/index.test.ts @@ -72,22 +72,41 @@ describe('Nebius Token Factory chat completions generation', () => { }); it('supports text-style choices from compatible Nebius responses', async () => { - vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ + const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ model: 'meta-llama/Llama-3.3-70B-Instruct', choices: [{ text: 'text choice response' }], }), - })); + }); + vi.stubGlobal('fetch', fetchMock); - const result = await adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://nebius.test' }); + const result = await adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://nebius.test/' }); + expect(fetchMock).toHaveBeenCalledOnce(); + expect(fetchMock.mock.calls[0]?.[0]).toBe('https://nebius.test/v1/chat/completions'); expect(result).toEqual({ text: 'text choice response', model: 'meta-llama/Llama-3.3-70B-Instruct', }); }); + it.each([ + ['missing scheme', 'nebius.test'], + ['ftp scheme', 'ftp://nebius.test'], + ['credentials', 'https://user:pass@nebius.test'], + ['query string', 'https://nebius.test?token=secret'], + ['fragment', 'https://nebius.test#chat'], + ])('rejects unclean custom baseUrl values: %s', async (_label, baseUrl) => { + const fetchMock = vi.fn(); + vi.stubGlobal('fetch', fetchMock); + + await expect(adapter.generate(ctx(), 'hello', {}, { baseUrl })).rejects.toThrow( + /Nebius baseUrl/ + ); + expect(fetchMock).not.toHaveBeenCalled(); + }); + it('includes status and response body excerpt on errors', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false, diff --git a/packages/ai/nebius/src/index.ts b/packages/ai/nebius/src/index.ts index 3d902329..7227feb7 100644 --- a/packages/ai/nebius/src/index.ts +++ b/packages/ai/nebius/src/index.ts @@ -28,7 +28,8 @@ export default defineAi({ if (opts.system) messages.push({ role: 'system', content: opts.system }); messages.push({ role: 'user', content: prompt }); - const res = await fetch(`${config.baseUrl ?? DEFAULT_BASE}/v1/chat/completions`, { + const baseUrl = cleanBaseUrl(config.baseUrl ?? DEFAULT_BASE); + const res = await fetch(`${baseUrl}/v1/chat/completions`, { method: 'POST', headers: { authorization: `Bearer ${apiKey}`, @@ -87,3 +88,22 @@ interface NebiusChatResponse { completion_tokens?: number; }; } + +function cleanBaseUrl(value: string): string { + let url: URL; + try { + url = new URL(value); + } catch { + throw new Error('Nebius baseUrl must be a valid URL'); + } + + if (url.protocol !== 'https:' && url.protocol !== 'http:') { + throw new Error('Nebius baseUrl must use http or https'); + } + + if (url.username || url.password || url.search || url.hash) { + throw new Error('Nebius baseUrl must be a clean API base without credentials, query, or hash'); + } + + return url.toString().replace(/\/+$/, ''); +}