import { Hono } from 'hono'; import { CONFIG } from '../config'; import { serviceManager } from '../services/service-manager'; import { execFile } from 'child_process'; import { promises as fs } from 'fs'; import { errMessage } from '../services/errors'; import { resolveReadableSystemPath } from '../utils/path-security'; import { validateLegacyExecRequest } from '../utils/legacy-exec'; import { getMimeType } from '../utils/mime'; const app = new Hono(); const MAX_READ_BYTES = 5 * 1024 * 1024; // 5MB // /health is defined as a public (unauthenticated) route in app.ts // Aggregate service health; keep the legacy tap response shape. app.get('/services', async (c) => { const healths = await serviceManager.healthAll(); const result: Record = {}; for (const [name, health] of Object.entries(healths)) { result[name] = { status: health.status, details: health.details, }; } // Enrich tap entry with URL for backward compatibility if (result.tap) { result.tap.url = CONFIG.TAP.URL; result.tap.stats = result.tap.details; delete result.tap.details; } return c.json(result); }); app.get('/fs/read', async (c) => { try { const filePath = String(c.req.query('path') || ''); if (!filePath) { return c.json({ error: 'path query parameter required' }, 400); } const realPath = await resolveReadableSystemPath(filePath); const fileStat = await fs.stat(realPath); if (!fileStat.isFile()) { return c.json({ error: 'path must point to a file' }, 400); } if (fileStat.size > MAX_READ_BYTES) { return c.json({ error: `File too large (${fileStat.size} bytes). Max supported is ${MAX_READ_BYTES} bytes.` }, 413); } const content = await fs.readFile(realPath); return c.json({ path: realPath, size: fileStat.size, mimeType: getMimeType(realPath), encoding: 'base64', data: content.toString('base64'), }); } catch (error: unknown) { const status = typeof error === 'object' && error && 'statusCode' in error ? Number((error as { statusCode?: unknown }).statusCode) || 500 : 500; console.error('[system] File read error:', error instanceof Error ? error.message : error); return c.json({ error: status < 500 ? errMessage(error) : 'Failed to read file' }, status as 400 | 403 | 404 | 500); } }); // Legacy host-side exec. Gated behind SUBSTRATE_ENABLE_EXEC and restricted to a small // allowlist. New work should route through /containers exec so commands run sandboxed. app.post('/exec', async (c) => { if (!CONFIG.ENABLE_EXEC) { return c.json({ error: "Command execution is disabled. Set SUBSTRATE_ENABLE_EXEC=true to enable." }, 403); } const { command, args = [] } = await c.req.json(); // Whitelist of allowed commands for security (echo excluded — can interpret shell syntax) const allowedCommands = ['tap', 'goat', 'ls', 'cat', 'pwd', 'whoami', 'uname']; if (!allowedCommands.includes(command)) { return c.json({ error: `Command '${command}' not allowed` }, 403); } if (!Array.isArray(args) || !args.every((arg) => typeof arg === 'string')) { return c.json({ error: 'args must be an array of strings' }, 400); } if (args.length > 20) { return c.json({ error: 'Too many arguments (max 20)' }, 400); } if (args.some((arg: string) => arg.length > 1024)) { return c.json({ error: 'Argument too long (max 1024 chars)' }, 400); } await validateLegacyExecRequest(command, args); const result = await new Promise((resolve) => { execFile(command, args, { timeout: 30000, maxBuffer: 5 * 1024 * 1024 }, (error, stdout, stderr) => { resolve({ success: !error, stdout: stdout.toString(), stderr: stderr.toString(), code: error ? error.code : 0 }); }); }); return c.json(result); }); export default app;