import type { Snapshot } from "../shared/ipc"; import type { SandboxResult, SandboxStatus, Scenario } from "../shared/sandbox"; function escapeHtml(text: string): string { return text.replace(/[&<>"]/g, (char) => { if (char === "&") return "&"; if (char === "<") return "<"; if (char === ">") return ">"; return """; }); } const STATUS_CLASS: Record = { ok: "done", blocked: "error", timeout: "cancel", truncated: "tool", }; function scenarioButton(scenario: Scenario): string { return ``; } function resultPanel( result: SandboxResult | null, kind: "path" | "shell", ): string { if (!result) { return `

${kind === "path" ? "运行一个路径场景" : "运行一个命令场景"}后显示结果。

`; } return `
${result.status} ${escapeHtml(result.code)} ${result.durationMs}ms

${escapeHtml(result.input)}

${escapeHtml(result.detail)}

`; } function auditRows(audit: SandboxResult[]): string { if (audit.length === 0) { return `暂无审计记录`; } return [...audit] .reverse() .map( ( entry, ) => ` ${entry.seq} ${entry.kind} ${escapeHtml(entry.label)} ${entry.status} ${escapeHtml(entry.code)} ${entry.durationMs}ms ${escapeHtml(entry.detail)} `, ) .join(""); } function checkRows(snapshot: Snapshot): string { return snapshot.checks .map( (check) => `
  • ${check.pass ? "PASS" : "FAIL"} ${escapeHtml(check.label)} ${escapeHtml(check.detail)}
  • `, ) .join(""); } export function mountApp(root: HTMLElement): void { root.innerHTML = `

    工作区与 Shell 沙箱

    路径边界 · 符号链接 · 命令白名单 · 超时 · 审计(离线)
    offline /Users/demo/work —

    工作区边界

    符号链接

    Shell 策略

    白名单

    审计日志

    0 条
    #类型场景状态码耗时详情

    沙箱自检

    —
      `; const score = root.querySelector('[data-testid="score"]'); const rootPill = root.querySelector('[data-testid="root-pill"]'); const rootPath = root.querySelector('[data-testid="root-path"]'); const symlinksEl = root.querySelector( '[data-testid="symlinks"]', ); const allowlistEl = root.querySelector( '[data-testid="allowlist"]', ); const limitsEl = root.querySelector('[data-testid="limits"]'); const pathScenarios = root.querySelector( '[data-testid="path-scenarios"]', ); const shellScenarios = root.querySelector( '[data-testid="shell-scenarios"]', ); const pathResult = root.querySelector( '[data-testid="path-result"]', ); const shellResult = root.querySelector( '[data-testid="shell-result"]', ); const auditEl = root.querySelector('[data-testid="audit"]'); const auditCount = root.querySelector( '[data-testid="audit-count"]', ); const checksEl = root.querySelector('[data-testid="checks"]'); const checkCount = root.querySelector( '[data-testid="check-count"]', ); let state: Snapshot | null = null; let busy = false; function render(): void { if (!state) return; if (rootPill) rootPill.textContent = state.root; if (rootPath) rootPath.textContent = state.root; if (limitsEl) { limitsEl.textContent = `timeout ${state.timeoutMs}ms · cap ${state.outputCap}`; } if (symlinksEl) { symlinksEl.innerHTML = Object.entries(state.symlinks) .map( ([name, target]) => `
    • ${escapeHtml(name)} → ${escapeHtml(target)}
    • `, ) .join(""); } if (allowlistEl) { allowlistEl.innerHTML = state.allowlist .map((item) => `${escapeHtml(item)}`) .join(""); } if (pathScenarios) { pathScenarios.innerHTML = state.scenarios .filter((scenario) => scenario.kind === "path") .map(scenarioButton) .join(""); } if (shellScenarios) { shellScenarios.innerHTML = state.scenarios .filter((scenario) => scenario.kind === "shell") .map(scenarioButton) .join(""); } const lastPath = [...state.audit] .reverse() .find((entry) => entry.kind === "path"); const lastShell = [...state.audit] .reverse() .find((entry) => entry.kind === "shell"); if (pathResult) pathResult.innerHTML = resultPanel(lastPath ?? null, "path"); if (shellResult) shellResult.innerHTML = resultPanel(lastShell ?? null, "shell"); const stage = document.body.dataset.shot; const rows = stage === "workspace" ? state.audit.filter((entry) => entry.kind === "path") : stage === "shell" ? state.audit.filter((entry) => entry.kind === "shell") : state.audit; if (auditEl) auditEl.innerHTML = auditRows(rows); if (auditCount) auditCount.textContent = `${rows.length} 条`; if (checksEl) checksEl.innerHTML = checkRows(state); if (checkCount) { const passed = state.checks.filter((check) => check.pass).length; checkCount.textContent = `${passed}/${state.checks.length} 通过`; } if (score) { score.textContent = `${state.score} / 100`; score.className = `pill ${state.score === 100 ? "ok-pill" : "bad-pill"}`; } root .querySelectorAll("[data-scenario]") .forEach((button) => { button.disabled = busy; }); } async function refresh(): Promise { state = await window.api.sandbox.snapshot(); render(); } async function runOne(scenarioId: string): Promise { if (busy) return; busy = true; render(); try { const response = await window.api.sandbox.run({ scenarioId }); state = response.snapshot; } finally { busy = false; render(); } } async function runAll(): Promise { if (!state) return; for (const scenario of state.scenarios) { await runOne(scenario.id); } } void refresh(); window.api.sandbox.onChanged(() => { void refresh(); }); root.addEventListener("click", (event) => { const target = event.target as HTMLElement; const button = target.closest("[data-scenario]"); if (button?.dataset.scenario) { void runOne(button.dataset.scenario); } }); root .querySelector('[data-testid="reset"]') ?.addEventListener("click", async () => { state = await window.api.sandbox.reset(); render(); }); window.__prepareShots = async (stage) => { document.body.dataset.shot = stage; state = await window.api.sandbox.reset(); await runAll(); window.scrollTo(0, 0); render(); }; window.__demo = async () => { state = await window.api.sandbox.reset(); await runAll(); const find = (id: string) => state?.audit.find((entry) => entry.scenarioId === id); return { score: state?.score ?? 0, checks: state?.checks.length ?? 0, passed: state?.checks.filter((check) => check.pass).length ?? 0, scenarios: state?.scenarios.length ?? 0, audit: state?.audit.length ?? 0, inside: find("path-inside")?.code === "OK", outside: find("path-outside")?.code === "OUTSIDE", absolute: find("path-absolute")?.code === "ABSOLUTE", symlink: find("path-symlink")?.code === "SYMLINK_ESCAPE", blocked: find("shell-blocked")?.code === "COMMAND_NOT_ALLOWED", metachar: find("shell-metachar")?.code === "SHELL_METACHAR", timeout: find("shell-timeout")?.status === "timeout", truncated: find("shell-truncate")?.status === "truncated", }; }; }