Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(browser): reject outside absolute paths before URL normalization
yuxinoa9d754496db6
diff --git a/apps/desktop/electron/main/browser-view.ts b/apps/desktop/electron/main/browser-view.tsindex 4b5e109d3..28192c3cd 100644--- a/apps/desktop/electron/main/browser-view.ts+++ b/apps/desktop/electron/main/browser-view.ts@@ -172,12 +172,12 @@ export class BrowserPane {): Promise<BrowserState | null> {if (fileRoot) this.fileRoot = fileRoot;const localPath = resolveLocalFile(raw, this.fileRoot);+ const localInput = /^file:/i.test(raw.trim()) || isAbsolute(raw.trim());const target = localPath? pathToFileURL(localPath).toString()- : normalizeUrl(raw);+ : localInput ? null : normalizeUrl(raw);if (!target) {this.beginManagedNavigation();- const localInput = /^file:/i.test(raw.trim()) || isAbsolute(raw.trim());this.loadError = { url: raw, message: localInput ? "LOCAL_FILE_NOT_ALLOWED" : "INVALID_URL" };const state = this.getState();if (state) this.onState(state);diff --git a/apps/desktop/test/browser-local-file-feedback.test.mjs b/apps/desktop/test/browser-local-file-feedback.test.mjsindex 286e9a60c..f77779274 100644--- a/apps/desktop/test/browser-local-file-feedback.test.mjs+++ b/apps/desktop/test/browser-local-file-feedback.test.mjs@@ -43,17 +43,18 @@ for (const [locale, expected] of [["en", "Only existing files inside this project's workspace can be opened."],["zh-CN", "只能打开此项目工作区内已存在的文件。"],]) {- test(`address bar explains denied local files in ${locale}`, async () => {- const view = render(locale);- await new Promise(setImmediate);- const url = "file:///tmp/demo.html";- view.element("url").value = url;- view.element("form").handlers.get("submit")({ preventDefault() {} });- assert.equal(view.calls.findLast((call) => call.name === "browser.navigate")?.payload.url, url);- view.callbacks.get("browser:state")({ url, loadError: "LOCAL_FILE_NOT_ALLOWED" });- assert.equal(view.element("empty").hidden, false);- assert.equal(view.element("empty-body").textContent, expected);- assert.equal(view.element("url").value, url);- assert.equal(view.calls.at(-1).payload.visible, false);- });+ for (const url of ["file:///tmp/demo.html", "/tmp/demo.html"]) {+ test(`address bar explains ${url} in ${locale}`, async () => {+ const view = render(locale);+ await new Promise(setImmediate);+ view.element("url").value = url;+ view.element("form").handlers.get("submit")({ preventDefault() {} });+ assert.equal(view.calls.findLast((call) => call.name === "browser.navigate")?.payload.url, url);+ view.callbacks.get("browser:state")({ url, loadError: "LOCAL_FILE_NOT_ALLOWED" });+ assert.equal(view.element("empty").hidden, false);+ assert.equal(view.element("empty-body").textContent, expected);+ assert.equal(view.element("url").value, url);+ assert.equal(view.calls.at(-1).payload.visible, false);+ });+ }}diff --git a/apps/desktop/test/browser-pane-navigation.test.mjs b/apps/desktop/test/browser-pane-navigation.test.mjsindex 41faf9a3b..07122abe2 100644--- a/apps/desktop/test/browser-pane-navigation.test.mjs+++ b/apps/desktop/test/browser-pane-navigation.test.mjs@@ -94,7 +94,7 @@ test("an invalid target cannot mark the previous document ready", async (t) => {await settled();});-test("an outside file URL explains the workspace boundary without loading it", async (t) => {+test("outside file URLs and absolute paths explain the workspace boundary without loading", async (t) => {const root = mkdtempSync(join(tmpdir(), "pi-browser-root-"));const outside = mkdtempSync(join(tmpdir(), "pi-browser-outside-"));t.after(() => { pane.dispose(); rmSync(root, { recursive: true }); rmSync(outside, { recursive: true }); });@@ -113,12 +113,22 @@ test("an outside file URL explains the workspace boundary without loading it", awc.pendingLoads.shift().resolve();await allowed;+ const allowedAbsolute = pane.navigateAndWait(insideFile, root);+ assert.equal(wc.pendingLoads.at(-1).url, pathToFileURL(realpathSync(insideFile)).href);+ wc.pendingLoads.shift().resolve();+ await allowedAbsolute;+const deniedUrl = pathToFileURL(outsideFile).href;assert.equal(await pane.navigateAndWait(deniedUrl, root), null);assert.equal(wc.pendingLoads.length, 0, "outside file must never reach Electron");assert.equal(published.at(-1).url, deniedUrl);assert.equal(published.at(-1).loadError, "LOCAL_FILE_NOT_ALLOWED");assert.equal(published.at(-1).isLoading, false);++ assert.equal(await pane.navigateAndWait(outsideFile, root, 10), null);+ assert.equal(wc.pendingLoads.at(-1)?.url, undefined, "outside absolute path must never reach Electron");+ assert.equal(published.at(-1).url, outsideFile);+ assert.equal(published.at(-1).loadError, "LOCAL_FILE_NOT_ALLOWED");});test("a denied local file on a blank tab publishes an error without creating a guest", async () => {@@ -131,6 +141,12 @@ test("a denied local file on a blank tab publishes an error without creating a gurl: "file:///tmp/demo.html", title: "", isLoading: false,loadError: "LOCAL_FILE_NOT_ALLOWED", canGoBack: false, canGoForward: false,});+ assert.equal(await pane.navigateAndWait("/tmp/demo.html", "/projects/demo"), null);+ assert.equal(WebContentsView.instances.length, count);+ assert.deepEqual(published.at(-1), {+ url: "/tmp/demo.html", title: "", isLoading: false,+ loadError: "LOCAL_FILE_NOT_ALLOWED", canGoBack: false, canGoForward: false,+ });});test("late native navigation events cannot publish after the session is invalidated", async () => {diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex 6313fc1f3..ca3693a93 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -1065,6 +1065,7 @@ entirely inside the plugin's isolated page:address or absolute path that does not resolve to an existing file insidethe session workspace is rejected before loading and shows a specificworkspace-boundary message in the browser chrome, including on a blank tab.+ Existing files inside the workspace remain previewable by absolute path.- Opening an HTTP(S) link in the work panel creates an additional Browserresource tab instead of replacing the previous URL. Each tab owns a host-retained WebContents, address, title and navigationdiff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex 253d9b649..803937e11 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -3744,8 +3744,9 @@ identify the platform validation still needed.close it and open Settings. Return to chatand trigger an inline tool permission card. 5) Switch to another panel taband back; close the panel. 6) Use open-external. 7) From both an existing page- and a blank Browser tab, enter an existing HTML file inside the workspace,- then `file:///tmp/demo.html` pointing to an existing file outside it.+ and a blank Browser tab, enter an existing HTML file inside the workspace by+ `file:` URL and absolute path. Then enter both `file:///tmp/demo.html` and+ `/tmp/demo.html`, pointing to an existing file outside the workspace.- **Expected**: Scheme-less input normalizes to http; nav state (URL bar,back/forward enablement, load spinner) mirrors the page. Popups open inthe default browser (never in-app) only when the URL parses as http(s) or@@ -3760,9 +3761,10 @@ identify the platform validation still needed.its full surface rect and the plugin body does not shift down.Open-external launches an http(s) page in the default browser and an in-rootfile preview via `openPath`. The view uses an isolated persist partition- (no session bleed from the app shell). The in-root file opens; the outside- file never loads, the submitted address remains visible, and the browser- explains in the active locale that only existing workspace files can open.+ (no session bleed from the app shell). Both in-root forms open; neither+ outside form reaches the guest load, the submitted address remains visible,+ and the browser explains in the active locale that only existing workspace+ files can open.- **Specs linked**: `03-runtime/01-ipc-protocol.md` §13a, ADR 0019, ADR 0168- **Acceptance**: Quality, Security- **Milestone**: M5diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.mdindex c70ad4e8d..3b07fabd3 100644--- a/docs/zh-CN/spec/04-ux/08-component-spec.md+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md@@ -771,6 +771,9 @@ vendor 进来的 `pi.file-manager` 视图在插件自己的隔离页面内完成当前主框架导航提交后即可显示页面,不再等待图片或子框架完成加载。导航失败或主框架等待超过 15 秒时显示可重试的错误,并隐藏旧页面;被替代或取消的请求不得覆盖新导航。+ `file:` 地址或绝对路径若不能解析为当前会话工作区内已存在的文件,+ 必须在加载前拒绝,并在浏览器栏显示明确的工作区范围提示;空白标签页也一样。+ 工作区内已存在文件的绝对路径仍可预览。- 在工作面板中打开 HTTP(S) 链接会新增浏览器资源标签,不覆盖原网址。每个标签保留自己的 WebContents、地址、标题及浏览历史。切换只隐藏/显示页面,diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.mdindex 561f282e4..eb7304c31 100644--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md@@ -2257,7 +2257,9 @@ MainChat 弥补了缺口。 Maximized/fullscreen 调用保留最新的`window.open` 弹出窗口和权限请求页面(例如通知提示)。 4) 打开全局搜索,然后打开设置。返回聊天并触发内联工具权限卡。 5) 切换到另一个面板选项卡- 然后回来;关闭面板。 6) 使用开放式外部。+ 然后回来;关闭面板。 6) 使用开放式外部。7) 分别从已有页面和空白浏览器标签页,+ 使用 `file:` 地址和绝对路径打开工作区内已存在的 HTML 文件;再输入指向工作区外+ 已存在文件的 `file:///tmp/demo.html` 和 `/tmp/demo.html`。- **预期**:无方案输入标准化为 http;导航状态(URL 栏,back/forward 启用、加载微调器)镜像页面。弹出窗口仅在 URL 解析为http(s) 或 mailto 时打开默认浏览器(绝不在应用程序内);@@ -2268,7 +2270,9 @@ MainChat 弥补了缺口。 Maximized/fullscreen 调用保留最新的并与占位符矩形对齐,不再闪出黑色面板底色。打开工作面板上下文下拉框时,原生视图在不透明菜单边界下方保持可见,菜单关闭后恢复完整表面矩形。Open-external 对 http(s) 页走系统浏览器,对根内文件预览走 `openPath`。- 视图使用隔离的持久分区(应用程序外壳中没有会话流失)。+ 视图使用隔离的持久分区(应用程序外壳中没有会话流失)。工作区内的两种地址+ 都能打开;工作区外的两种地址都不会进入 guest 加载,地址栏保留提交的地址,+ 浏览器用当前语言说明只能打开工作区内已存在的文件。- **链接规格**:`03-runtime/01-ipc-protocol.md` §13a、ADR 0019、ADR 0168- **验收**:质量、安全- **里程碑**:M5