Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(plugins): allow relative paths inside a drive-root workspace

Frank-zhu2000ccded1b18a58
diff --git a/apps/desktop/electron/main/browser-view.ts b/apps/desktop/electron/main/browser-view.ts
index 64e060c86..53a7700c2 100644
--- a/apps/desktop/electron/main/browser-view.ts
+++ b/apps/desktop/electron/main/browser-view.ts
@@ -42,15 +42,19 @@ export function normalizeUrl(raw: string): string | null {
}
}
+function rootBoundary(root: string): string {
+ return root.endsWith(sep) ? root : root + sep;
+}
+
function isWithinRoot(path: string, root: string): boolean {
try {
const realRoot = realpathSync(resolve(root));
const realPath = realpathSync(resolve(path));
- return realPath === realRoot || realPath.startsWith(realRoot + sep);
+ return realPath === realRoot || realPath.startsWith(rootBoundary(realRoot));
} catch {
const resolvedRoot = resolve(root);
const resolvedPath = resolve(path);
- return resolvedPath === resolvedRoot || resolvedPath.startsWith(resolvedRoot + sep);
+ return resolvedPath === resolvedRoot || resolvedPath.startsWith(rootBoundary(resolvedRoot));
}
}
diff --git a/apps/desktop/test/drive-root-path-containment.test.mjs b/apps/desktop/test/drive-root-path-containment.test.mjs
new file mode 100644
index 000000000..4798cfcaa
--- /dev/null
+++ b/apps/desktop/test/drive-root-path-containment.test.mjs
@@ -0,0 +1,69 @@
+import assert from "node:assert/strict";
+import { posix, win32 } from "node:path";
+import test from "node:test";
+import { isWithinRoot } from "../../../packages/agent-runtime/src/project-instructions.ts";
+import { resolveWithinRoot } from "../../../packages/host-runtime/src/workspace-files.ts";
+
+// Windows drive and UNC roots already end in a separator. Containment must
+// use path.win32 here so the regression runs on Linux, where the process
+// path module would not keep that trailing separator.
+const REL = ".workflow/knowhow/x.md";
+
+test("windows drive roots contain a relative plugin path", () => {
+ for (const root of ["C:/", "C:\\", "E:/", "E:\\"]) {
+ const resolved = win32.resolve(root);
+ assert.equal(resolveWithinRoot(root, REL, win32), win32.resolve(resolved, REL), root);
+ assert.equal(
+ resolveWithinRoot(root, ".workflow/knowledge-inbox/.gitignore", win32),
+ win32.join(resolved, ".workflow", "knowledge-inbox", ".gitignore"),
+ root,
+ );
+ }
+});
+
+test("a windows UNC share root contains a relative plugin path", () => {
+ const root = "\\\\server\\share";
+ assert.equal(
+ resolveWithinRoot(root, REL, win32),
+ win32.resolve(win32.resolve(root), REL),
+ );
+});
+
+test("windows roots still reject a path that leaves them", () => {
+ assert.equal(resolveWithinRoot("C:\\code", "../code-evil/x", win32), null);
+ assert.equal(resolveWithinRoot("E:\\code", "E:\\code-evil\\x", win32), null);
+ assert.equal(resolveWithinRoot("C:/Users/su", "C:/Windows/x", win32), null);
+ assert.equal(resolveWithinRoot("C:/project", "../project-evil/x", win32), null);
+ assert.equal(resolveWithinRoot("", REL, win32), null);
+});
+
+test("windows directories that are not roots stay contained", () => {
+ assert.equal(
+ resolveWithinRoot("C:/Users/su", REL, win32),
+ "C:\\Users\\su\\.workflow\\knowhow\\x.md",
+ );
+ assert.equal(resolveWithinRoot("E:\\code", REL, win32), "E:\\code\\.workflow\\knowhow\\x.md");
+ assert.equal(resolveWithinRoot("C:/Users/su", "", win32), "C:\\Users\\su");
+ // `..` cannot climb above a drive, so it stays on that drive.
+ assert.equal(resolveWithinRoot("C:/", "..", win32), "C:\\");
+ assert.equal(resolveWithinRoot("C:/", "", win32), "C:\\");
+});
+
+test("a posix filesystem root contains a relative path", () => {
+ assert.equal(resolveWithinRoot("/", REL), `/${REL}`);
+ assert.equal(resolveWithinRoot("/", REL, posix), `/${REL}`);
+ assert.equal(resolveWithinRoot("/srv/app", "../app-evil/x", posix), null);
+});
+
+test("project instruction containment accepts a windows drive root", () => {
+ const root = win32.resolve("C:/");
+ assert.equal(isWithinRoot(root, win32.join(root, ".workflow", "knowhow", "x.md"), win32), true);
+ assert.equal(isWithinRoot(root, root, win32), true);
+ const project = win32.resolve("C:/Users/su");
+ assert.equal(isWithinRoot(project, win32.join(project, "AGENTS.md"), win32), true);
+ assert.equal(
+ isWithinRoot(project, win32.join(win32.resolve("C:/Users/su-evil"), "AGENTS.md"), win32),
+ false,
+ );
+ assert.equal(isWithinRoot(posix.resolve("/"), posix.join(posix.resolve("/"), "AGENTS.md"), posix), true);
+});
diff --git a/packages/agent-runtime/src/index.ts b/packages/agent-runtime/src/index.ts
index 0933e3868..2116aee7c 100644
--- a/packages/agent-runtime/src/index.ts
+++ b/packages/agent-runtime/src/index.ts
@@ -10,7 +10,12 @@ export * from "./plugin-session-context.js";
export * from "./one-shot-complete.js";
export * from "./prompt-templates.js";
export * from "./provider-headers.js";
-export * from "./project-instructions.js";
+export {
+ globalInstructionPath,
+ loadInstructionChain,
+ loadProjectInstructions,
+} from "./project-instructions.js";
+export type { ProjectInstruction, ProjectInstructions } from "./project-instructions.js";
export * from "./project-instructions-prompt.js";
export * from "./project-memory-prompt.js";
export * from "./subagent.js";
diff --git a/packages/agent-runtime/src/project-instructions.ts b/packages/agent-runtime/src/project-instructions.ts
index 912151bcb..6e823f542 100644
--- a/packages/agent-runtime/src/project-instructions.ts
+++ b/packages/agent-runtime/src/project-instructions.ts
@@ -1,6 +1,6 @@
import { readFile, realpath } from "node:fs/promises";
import { homedir } from "node:os";
-import { dirname, join, relative, resolve, sep } from "node:path";
+import nodePath, { type PlatformPath, dirname, join, relative, resolve } from "node:path";
const INSTRUCTION_FILE_NAMES = [
"AGENTS.override.md",
@@ -28,8 +28,13 @@ export function globalInstructionPath(): string {
return GLOBAL_INSTRUCTION_PATH;
}
-function isWithinRoot(root: string, path: string): boolean {
- return path === root || path.startsWith(`${root}${sep}`);
+export function isWithinRoot(
+ root: string,
+ candidate: string,
+ paths: Pick<PlatformPath, "sep"> = nodePath,
+): boolean {
+ const boundary = root.endsWith(paths.sep) ? root : `${root}${paths.sep}`;
+ return candidate === root || candidate.startsWith(boundary);
}
function normalizeStablePath(path: string): string {
diff --git a/packages/host-runtime/src/workspace-files.ts b/packages/host-runtime/src/workspace-files.ts
index a07ae03bc..4b23ff000 100644
--- a/packages/host-runtime/src/workspace-files.ts
+++ b/packages/host-runtime/src/workspace-files.ts
@@ -1,5 +1,5 @@
import { readFile, readdir, realpath, stat } from "node:fs/promises";
-import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
+import nodePath, { type PlatformPath, basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
import type { FsEntry, FsImageDataUrlResult, FsReadResult } from "@pi-desktop/shared";
/**
@@ -52,13 +52,18 @@ export function isAttachmentBlobRef(path: string): boolean {
* Resolve `rel` inside `root`, rejecting absolute inputs and `..` escapes.
* Returns the absolute path or null when the input leaves the root.
*/
-export function resolveWithinRoot(root: string, rel: string): string | null {
+export function resolveWithinRoot(
+ root: string,
+ rel: string,
+ paths: Pick<PlatformPath, "resolve" | "sep"> = nodePath,
+): string | null {
if (!root) return null;
const cleanRel = String(rel ?? "").replace(/^[/\\]+/, "");
- const rootAbs = resolve(root);
- const target = resolve(rootAbs, cleanRel);
+ const rootAbs = paths.resolve(root);
+ const target = paths.resolve(rootAbs, cleanRel);
if (target === rootAbs) return rootAbs;
- if (!target.startsWith(rootAbs + sep)) return null;
+ const boundary = rootAbs.endsWith(paths.sep) ? rootAbs : rootAbs + paths.sep;
+ if (!target.startsWith(boundary)) return null;
return target;
}