import "./styles.css"; import type { Entry, Envelope } from "../shared/ipc"; type TabId = "browser" | "security"; declare global { interface Window { api: { tree(): Promise>; read(path: string): Promise< Envelope<{ path: string; content: string; sizeBytes: number; updatedAt: number; }> >; save( path: string, content: string, ): Promise< Envelope<{ path: string; sizeBytes: number; updatedAt: number }> >; reset(): Promise>; }; __setTab?: (tab: TabId) => void; __snapshot?: () => unknown; __smoke?: () => Promise; __demoSave?: () => Promise; } } const state: { tab: TabId; entries: Entry[]; current: string; content: string; status: string; blocked: Record; busy: boolean; } = { tab: "browser", entries: [], current: "", content: "", status: "", blocked: {}, busy: false, }; const tabs: Array<{ id: TabId; label: string }> = [ { id: "browser", label: "文件浏览器" }, { id: "security", label: "越权测试" }, ]; const probes = ["../outside-secret.txt", "/etc/passwd", "escape-link.md"]; function escapeHtml(value: string): string { return value .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """); } function time(at: number): string { return new Date(at).toLocaleTimeString("zh-CN", { hour12: false }); } function browserTab(): string { return `
${escapeHtml(state.status || "未修改")}

所有路径先 resolve、再 relative、最后 realpath;越界返回 PATH_OUTSIDE_WORKSPACE。

`; } function securityTab(): string { return `

越权测试

下面三条都在工作区外:相对上跳、绝对路径、指向外部的符号链接。点击后应全部被拒绝。

${probes .map( (probe) => ``, ) .join("")}
尝试路径结果码操作
${escapeHtml(probe)} ${escapeHtml(state.blocked[probe] ?? "—")}
为什么符号链接也要查

路径字符串完全在工作区内,但 realpath 后指向外部;只做字符串校验会被绕过。

`; } function render(): void { const root = document.querySelector("#app"); if (!root) return; const body = state.tab === "browser" ? browserTab() : securityTab(); root.innerHTML = `

工作区文件浏览器

目录树 · 预览 · 保存 · 路径限制

${body}
`; for (const button of root.querySelectorAll("[data-tab]")) { button.addEventListener("click", () => { const tab = button.dataset.tab; if (tab === "browser" || tab === "security") { state.tab = tab; render(); } }); } for (const button of root.querySelectorAll( "[data-path]", )) { button.addEventListener("click", () => { void open(button.dataset.path ?? ""); }); } for (const button of root.querySelectorAll( "[data-probe]", )) { button.addEventListener("click", () => { void probe(button.dataset.probe ?? ""); }); } root.querySelector("#save-button")?.addEventListener("click", () => { state.current = root.querySelector("#path-input")?.value ?? state.current; state.content = root.querySelector("#content-input")?.value ?? state.content; void save(); }); } async function refreshTree(): Promise { const response = await window.api.tree(); if (response.ok) state.entries = response.value; } async function open(path: string): Promise { if (state.busy || !path) return; state.busy = true; try { const response = await window.api.read(path); if (response.ok) { state.current = response.value.path; state.content = response.value.content; state.status = `已读取 ${response.value.sizeBytes} B · ${time(response.value.updatedAt)}`; } else { state.status = response.error.code; } } finally { state.busy = false; render(); } } async function save(): Promise { if (state.busy || !state.current) return; state.busy = true; try { const response = await window.api.save(state.current, state.content); state.status = response.ok ? `已保存 ${response.value.sizeBytes} B · ${time(response.value.updatedAt)}` : response.error.code; if (response.ok) await refreshTree(); } finally { state.busy = false; render(); } } async function probe(path: string): Promise { const response = await window.api.read(path); state.blocked = { ...state.blocked, [path]: response.ok ? "OK(不应出现)" : response.error.code, }; render(); } function boot(): void { render(); window.__setTab = (tab) => { state.tab = tab; render(); }; window.__snapshot = () => ({ tab: state.tab, entries: state.entries.length, current: state.current, blocked: state.blocked, }); window.__smoke = async () => { await window.api.reset(); const tree = await window.api.tree(); const saved = await window.api.save( "notes/welcome.md", "# 已保存\n\n这一行由 smoke 写入。\n", ); const reread = await window.api.read("notes/welcome.md"); const traversal = await window.api.read("../outside-secret.txt"); const absolute = await window.api.read("/etc/passwd"); const symlink = await window.api.read("escape-link.md"); state.entries = tree.ok ? tree.value : []; state.current = "notes/welcome.md"; state.content = reread.ok ? reread.value.content : ""; state.status = saved.ok ? "已保存" : saved.error.code; state.blocked = { "../outside-secret.txt": traversal.ok ? "OK(不应出现)" : traversal.error.code, "/etc/passwd": absolute.ok ? "OK(不应出现)" : absolute.error.code, "escape-link.md": symlink.ok ? "OK(不应出现)" : symlink.error.code, }; state.tab = "browser"; render(); return { entries: state.entries.length, readBytes: state.content.length, saved: saved.ok, traversal: state.blocked["../outside-secret.txt"], absolute: state.blocked["/etc/passwd"], symlink: state.blocked["escape-link.md"], }; }; window.__demoSave = async () => { state.tab = "browser"; state.current = "notes/welcome.md"; state.content = "# 已保存\n\n修改后的内容会原子写回工作区。\n"; await save(); }; } boot();