import { execFile } from "node:child_process"; import { link, mkdir, mkdtemp, realpath, rm, symlink, writeFile } from "node:fs/promises"; import { createServer, type Server } from "node:http"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; import { promisify } from "node:util"; import { TID_BROWSER_ADDRESS_INPUT, TID_BROWSER_WEBVIEW } from "@zcode/shared/test-ids"; import { openFirstBrowserTab } from "#e2e/helpers/browser-side-pane"; import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation"; async function runInGuest(script: string): Promise { return browser.execute( async (testId, code) => { const guest = document.querySelector(`[data-testid="${testId}"]`) as | (HTMLElement & { executeJavaScript(code: string): Promise }) | null; if (!guest) throw new Error("Browser guest is missing"); return guest.executeJavaScript(code); }, TID_BROWSER_WEBVIEW, script, ); } describe("本地 HTML 预览的文件权限边界", () => { let directory: string; let fileUrl: string; let outsideUrl: string; let canonicalFileUrl: string; let server: Server; let httpUrl: string; before(async () => { server = createServer((_request, response) => { response.writeHead(200, { "content-type": "text/html" }); response.end("HTTP control

HTTP control

"); }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); const address = server.address(); if (!address || typeof address === "string") throw new Error("HTTP fixture did not start"); httpUrl = `http://127.0.0.1:${address.port}/`; directory = await mkdtemp(join(tmpdir(), "zcode-html-preview-")); const preview = join(directory, "预览 空格"); const outside = join(directory, "outside"); await Promise.all([mkdir(preview), mkdir(outside)]); fileUrl = pathToFileURL(join(preview, "页面.html")).href; outsideUrl = pathToFileURL(join(outside, "secret.txt")).href; await Promise.all([ writeFile(join(outside, "secret.txt"), "SYNTHETIC_OUTSIDE_SECRET"), writeFile(join(preview, ".env"), "SYNTHETIC_HIDDEN_SECRET"), writeFile(join(preview, "data.json"), '{"ok":true}'), writeFile(join(preview, "style.css"), "body { color: rgb(12, 34, 56); }"), writeFile(join(preview, "classic.js"), "window.classicLoaded = true;"), writeFile(join(preview, "module.js"), "window.moduleLoaded = true;"), writeFile( join(preview, "image.svg"), '', ), writeFile(join(preview, "next.html"), "next

Next page

"), writeFile( join(preview, "页面.html"), ` Local HTML boundary `, ), symlink(outside, join(preview, "linked"), process.platform === "win32" ? "junction" : "dir"), ]); canonicalFileUrl = pathToFileURL(await realpath(join(preview, "页面.html"))).href; // 硬链接别名(ZP-20261105-09):子树内的名字指向子树外同一 inode,realpath 无法识别,必须靠 nlink 拒绝。 await link(join(outside, "secret.txt"), join(preview, "hardlink-secret.txt")); // 句柄方案的边界夹具:目录与 FIFO 必须被拒绝;FIFO 若阻塞 open,下面的 fetch 会挂起直到用例超时。 await Promise.all([ mkdir(join(preview, "subdir")), writeFile(join(preview, "notes.md"), "# notes"), writeFile(join(preview, "empty.txt"), ""), process.platform === "win32" ? Promise.resolve() : promisify(execFile)("mkfifo", [join(preview, "pipe")]), ]); }); after(async () => { await rm(directory, { recursive: true, force: true }); server.closeAllConnections(); await new Promise((resolve) => server.close(() => resolve())); }); it("真实浏览器入口加载相对资源,同时拒绝越界文件并支持刷新和后退", async function () { this.timeout(240_000); await prepareV4ConversationE2E({ skipProvider: true }); await openFirstBrowserTab(); const input = await $(`[data-testid="${TID_BROWSER_ADDRESS_INPUT}"]`); await input.setValue(fileUrl); await browser.keys("Enter"); await browser.waitUntil( async () => (await runInGuest("document.title")) === "Local HTML boundary", { timeout: 30_000, timeoutMsg: "本地 HTML 未加载" }, ); expect(await runInGuest("location.protocol")).toBe("zcode-html:"); expect(await input.getValue()).toBe(canonicalFileUrl); await browser.waitUntil( async () => Boolean( await runInGuest( "window.classicLoaded && window.moduleLoaded && document.querySelector('img').naturalWidth === 2", ), ), { timeout: 10_000, timeoutMsg: "相对脚本或图片未加载" }, ); expect(await runInGuest("getComputedStyle(document.body).color")).toBe("rgb(12, 34, 56)"); expect(await runInGuest("fetch('data.json').then(r => r.json())")).toEqual({ ok: true }); const results = await runInGuest(`(async () => { const forged = new URL(location.href); forged.hostname = '0'.repeat(64); const urls = [${JSON.stringify(outsideUrl)}, 'linked/secret.txt', '.env', '../outside/secret.txt', '%2e%2e%2foutside%2fsecret.txt', 'hardlink-secret.txt', forged.href]; return Promise.all(urls.map(async url => { try { const r = await fetch(url); return r.ok ? await r.text() : null; } catch { return null; } })); })()`); expect(results).toEqual([null, null, null, null, null, null, null]); // 资源改由已校验的句柄读取后,HEAD 只返回头、空文件返回空 body、目录与 FIFO 被拒绝且不挂起 handler、表内扩展名带正确类型。 // Windows 没有 FIFO 夹具,请求得到 404,同样不 ok。 expect( await runInGuest(`(async () => { const head = await fetch('data.json', { method: 'HEAD' }); const md = await fetch('notes.md'); const empty = await fetch('empty.txt'); const rejected = await Promise.all(['subdir', 'pipe'].map(async url => { try { return (await fetch(url)).ok; } catch { return false; } })); return { headStatus: head.status, headLength: head.headers.get('content-length'), headBody: (await head.text()).length, mdType: md.headers.get('content-type'), emptyStatus: empty.status, emptyBody: (await empty.text()).length, rejected }; })()`), ).toEqual({ headStatus: 200, headLength: "11", headBody: 0, mdType: "text/markdown", emptyStatus: 200, emptyBody: 0, rejected: [false, false], }); // 等待加载结果后读取,避免 iframe 仍在 about:blank 时产生假通过;拒绝加载可能没有 load 事件。 await runInGuest(`new Promise(resolve => { const frame = document.createElement('iframe'); frame.id = 'outside'; const timeout = setTimeout(resolve, 2000); frame.onload = frame.onerror = () => { clearTimeout(timeout); resolve(); }; frame.src = ${JSON.stringify(outsideUrl)}; document.body.append(frame); })`); expect( await runInGuest(`(() => { try { return document.querySelector('#outside').contentDocument?.body?.textContent?.includes('SYNTHETIC_OUTSIDE_SECRET') ?? false; } catch { return false; } })()`), ).toBe(false); const previewUrl = await runInGuest("location.href"); const previousBoot = await runInGuest("performance.timeOrigin"); await runInGuest("void location.reload()"); await browser.waitUntil( async () => Boolean( await runInGuest(`window.moduleLoaded && performance.timeOrigin !== ${previousBoot}`), ), { timeout: 15_000, }, ); await runInGuest("document.querySelector('#next').click()"); await browser.waitUntil(async () => (await runInGuest("document.title")) === "next", { timeout: 15_000, }); await runInGuest("history.back()"); await browser.waitUntil(async () => (await runInGuest("location.href")) === previewUrl, { timeout: 15_000, }); const isolation = await browser.electron.execute(async (electron, url) => { const control = new electron.BrowserWindow({ show: false, webPreferences: { sandbox: true }, }); const denied = new electron.BrowserWindow({ show: false, webPreferences: { sandbox: true, partition: "persist:zcode-embedded-browser", }, }); try { await control.loadURL(url); let blocked = false; try { await denied.loadURL(url); } catch { blocked = true; } return { defaultLoaded: await control.webContents.executeJavaScript("document.title"), blocked, }; } finally { control.destroy(); denied.destroy(); } }, fileUrl); expect(isolation).toEqual({ defaultLoaded: "Local HTML boundary", blocked: true }); // 同一轮 renderer 调用先发本地文件、再发 HTTP,文件校验的异步结果不能覆盖后者。 await browser.execute( async (testId, localUrl, remoteUrl) => { const guest = document.querySelector(`[data-testid="${testId}"]`) as HTMLElement & { getWebContentsId(): number; }; const platform = ( window as Window & { zcode?: { browserViewLoadUrl(payload: { webContentsId: number; url: string }): Promise; }; } ).zcode; if (!platform) throw new Error("Desktop platform bridge is missing"); const webContentsId = guest.getWebContentsId(); await Promise.allSettled([ platform.browserViewLoadUrl({ webContentsId, url: localUrl }), platform.browserViewLoadUrl({ webContentsId, url: remoteUrl }), ]); }, TID_BROWSER_WEBVIEW, fileUrl, httpUrl, ); expect(await runInGuest("location.href")).toBe(httpUrl); expect(await runInGuest("document.title")).toBe("HTTP control"); }); });