From 4e381c2f42cc9b427b49355dfed3cad7c63f8e3b Mon Sep 17 00:00:00 2001 From: aiirvizionz Date: Thu, 23 Jul 2026 11:40:44 -0600 Subject: [PATCH] fix(ai-together): require clean api base urls --- packages/ai/together/src/index.test.ts | 25 ++++++++++++++++++++++--- packages/ai/together/src/index.ts | 22 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/ai/together/src/index.test.ts b/packages/ai/together/src/index.test.ts index 1e4c3239..ba804b49 100644 --- a/packages/ai/together/src/index.test.ts +++ b/packages/ai/together/src/index.test.ts @@ -84,26 +84,45 @@ describe('Together AI generation', () => { }); it('supports text-style choices from compatible responses', async () => { - vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ + const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ choices: [{ text: 'legacy text response' }], }), - })); + }); + vi.stubGlobal('fetch', fetchMock); const result = await adapter.generate( ctx(), 'hello', { model: 'Qwen/Qwen3.5-9B' }, - { baseUrl: 'https://together.test/v1' }, + { baseUrl: 'https://together.test/v1/' }, ); + expect(fetchMock).toHaveBeenCalledOnce(); + expect(fetchMock.mock.calls[0]?.[0]).toBe('https://together.test/v1/chat/completions'); expect(result).toEqual({ text: 'legacy text response', model: 'Qwen/Qwen3.5-9B', }); }); + it.each([ + ['missing scheme', 'together.test/v1'], + ['ftp scheme', 'ftp://together.test/v1'], + ['credentials', 'https://user:pass@together.test/v1'], + ['query string', 'https://together.test/v1?token=secret'], + ['fragment', 'https://together.test/v1#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( + /Together AI 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/together/src/index.ts b/packages/ai/together/src/index.ts index dfc9dab8..cb7b5159 100644 --- a/packages/ai/together/src/index.ts +++ b/packages/ai/together/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}/chat/completions`, { + const baseUrl = cleanBaseUrl(config.baseUrl ?? DEFAULT_BASE); + const res = await fetch(`${baseUrl}/chat/completions`, { method: 'POST', headers: { authorization: `Bearer ${apiKey}`, @@ -87,3 +88,22 @@ interface TogetherChatResponse { completion_tokens?: number; }; } + +function cleanBaseUrl(value: string): string { + let url: URL; + try { + url = new URL(value); + } catch { + throw new Error('Together AI baseUrl must be a valid URL'); + } + + if (url.protocol !== 'https:' && url.protocol !== 'http:') { + throw new Error('Together AI baseUrl must use http or https'); + } + + if (url.username || url.password || url.search || url.hash) { + throw new Error('Together AI baseUrl must be a clean API base without credentials, query, or hash'); + } + + return url.toString().replace(/\/+$/, ''); +}