Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import { afterEach, describe, expect, it } from 'vitest';import fs from 'fs/promises';import os from 'os';import path from 'path';import { resolveExistingPathWithinRoot, resolveReadableSystemPath,} from '../src/utils/path-security';import { validateLegacyExecRequest } from '../src/utils/legacy-exec';import { signProxyToken, verifyProxyToken } from '../src/utils/proxy-token';import { isPrivateUrl } from '../src/utils/ssrf';import { assertProviderBaseUrlAllowed, resolveProviderBaseUrl, validateProviderBaseUrl,} from '../src/utils/llm-provider-security';import { analyzeExecCommand, maybePersistLargeOutput, readPersistedExecOutput } from '../src/services/execution-core';
const tempDirs: string[] = [];
afterEach(async () => { await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));});
async function makeTempDir(prefix: string): Promise<string> { const dir = await fs.mkdtemp(path.join('/tmp', prefix)); tempDirs.push(dir); return dir;}
describe('path security helpers', () => { it('allows readable system files under approved prefixes', async () => { const tempDir = await makeTempDir('substrate-path-'); const filePath = path.join(tempDir, 'allowed.txt'); await fs.writeFile(filePath, 'ok', 'utf8'); const realPath = await fs.realpath(filePath);
await expect(resolveReadableSystemPath(filePath)).resolves.toBe(realPath); });
it('rejects workspace symlink escapes after realpath resolution', async () => { const rootDir = await makeTempDir('substrate-root-'); const outsideDir = await makeTempDir('substrate-outside-'); const outsideFile = path.join(outsideDir, 'secret.txt'); const symlinkFile = path.join(rootDir, 'secret.txt'); await fs.writeFile(outsideFile, 'secret', 'utf8'); await fs.symlink(outsideFile, symlinkFile);
await expect(resolveExistingPathWithinRoot(rootDir, 'secret.txt')).rejects.toMatchObject({ message: 'Invalid path', statusCode: 400, }); });});
describe('legacy exec validation', () => { it('allows cat on approved readable paths', async () => { const tempDir = await makeTempDir('substrate-exec-'); const filePath = path.join(tempDir, 'hello.txt'); await fs.writeFile(filePath, 'hello', 'utf8');
await expect(validateLegacyExecRequest('cat', [filePath])).resolves.toBeUndefined(); });
it('rejects cat on non-approved paths', async () => { await expect(validateLegacyExecRequest('cat', ['/etc/hosts'])).rejects.toMatchObject({ message: 'Access to this path not allowed', statusCode: 403, }); });});
describe('proxy tokens', () => { it('verifies subject-bound tokens only for the matching subject', () => { const token = signProxyToken('abc123', Date.now() + 10_000, 'secret', 'user-1');
expect(verifyProxyToken(token, 'abc123', 'secret', 'user-1')).toBe(true); expect(verifyProxyToken(token, 'abc123', 'secret', 'user-2')).toBe(false); expect(verifyProxyToken(token, 'abc123', 'secret', '')).toBe(false); expect(verifyProxyToken(token, 'wrong', 'secret', 'user-1')).toBe(false); });
it('rejects expired tokens', () => { const token = signProxyToken('abc123', Date.now() - 1000, 'secret', 'user-1'); expect(verifyProxyToken(token, 'abc123', 'secret', 'user-1')).toBe(false); });});
describe('URL and provider validation', () => { it('flags private and internal URLs as private', () => { expect(isPrivateUrl(new URL('http://127.0.0.1:8080'))).toBe(true); expect(isPrivateUrl(new URL('https://metadata.google.internal/path'))).toBe(true); expect(isPrivateUrl(new URL('https://example.com'))).toBe(false); });
it('normalizes and resolves safe local providers for container access', async () => { const normalized = validateProviderBaseUrl('http://localhost:11434/v1/', 'local'); expect(normalized).toBe('http://localhost:11434/v1'); expect(resolveProviderBaseUrl(normalized, 'local')).toBe('http://host.docker.internal:11434/v1'); await expect(assertProviderBaseUrlAllowed(normalized, 'local')).resolves.toBeUndefined(); });
it('rejects unsafe hosted providers', async () => { await expect(assertProviderBaseUrlAllowed('https://127.0.0.1/v1', 'hosted')).rejects.toMatchObject({ message: 'Hosted providers must not target local machine addresses', statusCode: 400, }); });});
describe('execution analysis', () => { it('classifies inspection-only commands as low-risk reads', () => { const analysis = analyzeExecCommand('argv', ['cat', '/tmp/file.txt']); expect(analysis.kind).toBe('read'); expect(analysis.risk).toBe('low'); expect(analysis.semanticSuccess).toBe(true); });
it('persists oversized exec output and returns a retrievable reference', async () => { const result = await maybePersistLargeOutput({ mode: 'shell', command: 'cat huge.log', stdout: 'a'.repeat(512), stderr: '', exitCode: 0, maxBytes: 64, workdir: '/workspace', });
expect(result.truncated).toBe(true); expect(result.persistedOutputRef).toBeTruthy();
const persisted = await readPersistedExecOutput(result.persistedOutputRef!); expect(persisted.command).toBe('cat huge.log'); expect(persisted.workdir).toBe('/workspace'); expect(persisted.stdout).toHaveLength(512); });});