Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import { afterEach, describe, expect, it } from 'vitest';import fs from 'fs/promises';import os from 'os';import path from 'path';import { LlmService } from '../src/services/llm';import { CONFIG } from '../src/config';import { resolveProviderBaseUrl } from '../src/utils/llm-provider-security';
const originalConfigPath = CONFIG.LLM.CONFIG_PATH;
afterEach(() => { CONFIG.LLM.CONFIG_PATH = originalConfigPath;});
describe('LLM provider hardening', () => { it('rejects unsafe hosted provider base URLs on update', async () => { const service = new LlmService(); const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'substrate-llm-')); CONFIG.LLM.CONFIG_PATH = path.join(tempDir, 'providers.json');
await expect(service.upsertProvider({ id: 'bad-hosted', name: 'Bad Hosted', type: 'hosted', baseUrl: 'https://127.0.0.1:11434/v1', defaultModel: 'test-model', enabled: true, })).rejects.toThrow('Hosted providers must not target local machine addresses'); });
it('allows loopback local providers and rewrites them for in-container access', async () => { const service = new LlmService(); const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'substrate-llm-')); CONFIG.LLM.CONFIG_PATH = path.join(tempDir, 'providers.json');
const provider = await service.upsertProvider({ id: 'ollama', name: 'Ollama', type: 'local', baseUrl: 'http://localhost:11434/v1/', defaultModel: 'llama3.2', enabled: true, });
expect(provider.baseUrl).toBe('http://localhost:11434/v1'); expect(resolveProviderBaseUrl(provider.baseUrl, provider.type)).toBe('http://host.docker.internal:11434/v1'); });
it('blocks persisted invalid hosted providers at request time', async () => { const service = new LlmService(); (service as any).config = { defaultProviderId: 'bad', providers: [ { id: 'bad', name: 'Bad', type: 'hosted', baseUrl: 'https://192.168.1.25/v1', defaultModel: 'test-model', enabled: true, }, ], };
const result = await service.listModels(); expect(result.models).toEqual([]); expect(result.error).toContain('private or internal addresses'); });});