import { afterEach, describe, expect, it } from 'vitest'; import fs from 'fs/promises'; import os from 'os'; import path from 'path'; import app from '../src/app'; import { CONFIG } from '../src/config'; import { MAX_REQUEST_BODY_BYTES } from '../src/utils/request-body'; const originalWorkspaceEnabled = CONFIG.WORKSPACE.ENABLED; const originalContainersEnabled = CONFIG.CONTAINERS.ENABLED; const originalWorkspaceMountPath = CONFIG.WORKSPACE.MOUNT_PATH; const originalExecEnabled = CONFIG.ENABLE_EXEC; afterEach(() => { CONFIG.WORKSPACE.ENABLED = originalWorkspaceEnabled; CONFIG.CONTAINERS.ENABLED = originalContainersEnabled; CONFIG.WORKSPACE.MOUNT_PATH = originalWorkspaceMountPath; CONFIG.ENABLE_EXEC = originalExecEnabled; }); describe('substrate app security boundaries', () => { it('rejects authenticated routes without the substrate API key', async () => { const response = await app.fetch(new Request('http://substrate.local/services')); expect(response.status).toBe(401); await expect(response.json()).resolves.toMatchObject({ error: 'Unauthorized substrate request', }); }); it('serves files inside the workspace blob root and rejects symlink escapes', async () => { const workspaceRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'substrate-workspace-')); const outsideRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'substrate-outside-')); const workspaceFile = path.join(workspaceRoot, 'hello.txt'); const outsideFile = path.join(outsideRoot, 'secret.txt'); const symlinkPath = path.join(workspaceRoot, 'secret-link.txt'); await fs.writeFile(workspaceFile, 'hello world', 'utf8'); await fs.writeFile(outsideFile, 'outside secret', 'utf8'); await fs.symlink(outsideFile, symlinkPath); CONFIG.WORKSPACE.ENABLED = true; CONFIG.CONTAINERS.ENABLED = true; CONFIG.WORKSPACE.MOUNT_PATH = workspaceRoot; const allowedResponse = await app.fetch(new Request('http://substrate.local/workspace/files/blob?path=hello.txt', { headers: { 'x-substrate-key': CONFIG.API_KEY }, })); expect(allowedResponse.status).toBe(200); expect(await allowedResponse.text()).toBe('hello world'); const escapedResponse = await app.fetch(new Request('http://substrate.local/workspace/files/blob?path=secret-link.txt', { headers: { 'x-substrate-key': CONFIG.API_KEY }, })); expect(escapedResponse.status).toBe(400); await expect(escapedResponse.json()).resolves.toMatchObject({ error: 'Invalid path' }); }); it('rejects legacy exec attempts to read arbitrary system paths', async () => { CONFIG.ENABLE_EXEC = true; const response = await app.fetch(new Request('http://substrate.local/exec', { method: 'POST', headers: { 'content-type': 'application/json', 'x-substrate-key': CONFIG.API_KEY, }, body: JSON.stringify({ command: 'cat', args: ['/etc/hosts'], }), })); expect(response.status).toBe(403); await expect(response.json()).resolves.toMatchObject({ error: 'Access to this path not allowed', }); }); it('rejects unsafe system file reads through the route layer', async () => { const tempDir = await fs.mkdtemp(path.join('/tmp', 'substrate-system-')); const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'substrate-system-outside-')); const blockedFile = path.join(tempDir, '.env'); const outsideFile = path.join(outsideDir, 'hosts'); const escapeLink = path.join(tempDir, 'hosts-link'); await fs.writeFile(blockedFile, 'SECRET=1\n', 'utf8'); await fs.writeFile(outsideFile, 'outside\n', 'utf8'); await fs.symlink(outsideFile, escapeLink); const blockedResponse = await app.fetch(new Request( `http://substrate.local/fs/read?path=${encodeURIComponent(blockedFile)}`, { headers: { 'x-substrate-key': CONFIG.API_KEY } }, )); expect(blockedResponse.status).toBe(403); await expect(blockedResponse.json()).resolves.toMatchObject({ error: 'Access to this file type not allowed', }); const escapedResponse = await app.fetch(new Request( `http://substrate.local/fs/read?path=${encodeURIComponent(escapeLink)}`, { headers: { 'x-substrate-key': CONFIG.API_KEY } }, )); expect(escapedResponse.status).toBe(403); await expect(escapedResponse.json()).resolves.toMatchObject({ error: 'Access to this path not allowed', }); }); it('rejects oversized streamed request bodies without relying on Content-Length', async () => { const encoder = new TextEncoder(); const payload = JSON.stringify({ data: 'x'.repeat(MAX_REQUEST_BODY_BYTES + 128), }); const stream = new ReadableStream({ start(controller) { controller.enqueue(encoder.encode(payload)); controller.close(); }, }); const response = await app.fetch(new Request('http://substrate.local/health', { method: 'POST', headers: { 'content-type': 'application/json', }, body: stream, duplex: 'half', })); expect(response.status).toBe(413); await expect(response.json()).resolves.toMatchObject({ error: 'Request body too large', }); }); it('rejects malformed Content-Length headers before request parsing', async () => { const response = await app.fetch(new Request('http://substrate.local/exec', { method: 'POST', headers: { 'content-type': 'application/json', 'content-length': 'not-a-number', 'x-substrate-key': CONFIG.API_KEY, }, body: JSON.stringify({ command: 'pwd', args: [] }), })); expect(response.status).toBe(400); await expect(response.json()).resolves.toMatchObject({ error: 'Invalid Content-Length header', }); }); });