Skip to content

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

fix(files): accept a canonicalized path under an aliased root

vastsa3936d0986053
diff --git a/apps/desktop/electron/main/ipc/workspace-ipc.ts b/apps/desktop/electron/main/ipc/workspace-ipc.ts
index 12ea34e45..d332e091e 100644
--- a/apps/desktop/electron/main/ipc/workspace-ipc.ts
+++ b/apps/desktop/electron/main/ipc/workspace-ipc.ts
@@ -2,6 +2,7 @@ import { BrowserWindow, dialog, shell, type OpenDialogOptions } from "electron";
import { dirname } from "node:path";
import { homedir } from "node:os";
import { existsSync, statSync } from "node:fs";
+import { realpath } from "node:fs/promises";
import { isAbsolute, join, resolve } from "node:path";
import {
ErrorCodes,
@@ -737,12 +738,36 @@ export function registerWorkspaceIpc({
* registered group root a valid containment base, so a chat reference that
* resolves in a sibling folder still opens instead of failing containment —
* which is what a user without the file view would otherwise see.
+ *
+ * Both the spelling of the root and its `realpath` are returned: a producer
+ * that canonicalizes what it records (image generation realpaths its output
+ * directory) would otherwise write a path that no listed root contains when
+ * `dataDir` or a project folder sits behind a link (`/var` on macOS, a linked
+ * or synced folder). The two entries name the same directory, and every
+ * candidate is still re-checked through `realpath` before a read is allowed.
*/
- const fsExtraRoots = async (workspaceRoot: string | null): Promise<string[]> => [
- join(dataDir, "scratch"),
- join(dataDir, "attachments"),
- ...projectFolderPaths(workspaceRoot).filter((path) => path !== workspaceRoot),
- ];
+ const fsExtraRoots = async (workspaceRoot: string | null): Promise<string[]> => {
+ const roots = [
+ join(dataDir, "scratch"),
+ join(dataDir, "attachments"),
+ ...projectFolderPaths(workspaceRoot).filter((path) => path !== workspaceRoot),
+ ];
+ const canonical = await Promise.all(
+ roots.map(async (root) => {
+ try {
+ return await realpath(root);
+ } catch {
+ return null;
+ }
+ }),
+ );
+ return [
+ ...new Set([
+ ...roots,
+ ...canonical.filter((root): root is string => typeof root === "string"),
+ ]),
+ ];
+ };
/**
* The session's own scratch directory (ADR 0124), or null when the session
diff --git a/packages/host-runtime/src/workspace-files.test.ts b/packages/host-runtime/src/workspace-files.test.ts
new file mode 100644
index 000000000..e399c412e
--- /dev/null
+++ b/packages/host-runtime/src/workspace-files.test.ts
@@ -0,0 +1,163 @@
+import { mkdir, mkdtemp, realpath, rm, symlink, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterAll, expect, it } from "vitest";
+import {
+ readOpenableImage,
+ resolveOpenablePath,
+ resolveRealOpenablePath,
+} from "./workspace-files.js";
+
+const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
+
+const temps: string[] = [];
+async function tempDir(prefix: string): Promise<string> {
+ const dir = await mkdtemp(join(tmpdir(), prefix));
+ temps.push(dir);
+ return dir;
+}
+
+afterAll(async () => {
+ await Promise.all(temps.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
+});
+
+/** Creating links is not always permitted; callers skip then. */
+async function linkOrSkip(target: string, path: string): Promise<boolean> {
+ try {
+ await symlink(target, path);
+ return true;
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ if (code === "EPERM" || code === "EACCES" || code === "ENOSYS") return false;
+ throw error;
+ }
+}
+
+it("opens a canonicalized path under a root that is still an alias", async (ctx) => {
+ if (process.platform === "win32") ctx.skip();
+ const base = await tempDir("pi-ws-alias-");
+ const realRoot = join(base, "real");
+ const aliasRoot = join(base, "alias");
+ await mkdir(realRoot);
+ await writeFile(join(realRoot, "generated.png"), PNG);
+ await writeFile(join(realRoot, "notes.txt"), "hello");
+ if (!(await linkOrSkip(realRoot, aliasRoot))) {
+ ctx.skip();
+ return;
+ }
+
+ // Writers canonicalize before recording the path (`image-generation-service`
+ // realpaths its output dir), so the transcript can carry `/private/var/...`
+ // while the allowed root is still spelled `/var/...`.
+ const canonicalFile = await realpath(join(realRoot, "generated.png"));
+ expect(resolveOpenablePath(canonicalFile, aliasRoot, [])).toBeNull();
+ expect(await resolveRealOpenablePath(canonicalFile, aliasRoot, [])).toBe(canonicalFile);
+ expect(await resolveRealOpenablePath(canonicalFile, null, [aliasRoot])).toBe(canonicalFile);
+ expect(await resolveRealOpenablePath(canonicalFile, aliasRoot, [realRoot])).toBe(
+ canonicalFile,
+ );
+
+ // Absolute paths and attachment blobs keep working through the alias too.
+ const canonicalAlias = await realpath(join(aliasRoot, "notes.txt"));
+ expect(await resolveRealOpenablePath(canonicalAlias, aliasRoot, [])).toBe(
+ canonicalAlias,
+ );
+});
+
+it("stores a generated image reference that the reader can open", async (ctx) => {
+ if (process.platform === "win32") ctx.skip();
+ const base = await tempDir("pi-ws-scratch-");
+ const dataDir = join(base, "data");
+ const scratch = join(dataDir, "scratch");
+ const workspace = join(base, "workspace");
+ await mkdir(scratch, { recursive: true });
+ await mkdir(workspace);
+ await writeFile(join(scratch, "generated-1.png"), PNG);
+ if (!(await linkOrSkip(dataDir, join(base, "data-alias")))) {
+ ctx.skip();
+ return;
+ }
+
+ // Exactly what `image-generation-service` writes into the transcript.
+ const stored = await realpath(join(scratch, "generated-1.png"));
+ const result = await readOpenableImage(stored, workspace, [scratch]);
+ expect(result).toMatchObject({ kind: "image", size: PNG.length });
+ expect(String((result as { dataUrl?: string }).dataUrl)).toMatch(
+ /^data:image\/png;base64,/,
+ );
+
+ // The alias spelling was always accepted and must keep working.
+ const alias = await readOpenableImage(join(scratch, "generated-1.png"), workspace, [
+ scratch,
+ ]);
+ expect(alias.kind).toBe("image");
+});
+
+it("still refuses a link inside an allowed root that points outside it", async (ctx) => {
+ if (process.platform === "win32") ctx.skip();
+ const base = await tempDir("pi-ws-escape-");
+ const root = join(base, "root");
+ const outside = join(base, "outside");
+ const other = join(base, "other");
+ await mkdir(root);
+ await mkdir(outside);
+ await mkdir(other);
+ await writeFile(join(outside, "leak.png"), PNG);
+ const link = join(root, "leak.png");
+ if (!(await linkOrSkip(join(outside, "leak.png"), link))) {
+ ctx.skip();
+ return;
+ }
+
+ const canonicalOutside = await realpath(join(outside, "leak.png"));
+ expect(await resolveRealOpenablePath(link, root, [])).toBeNull();
+ expect(await resolveRealOpenablePath(canonicalOutside, root, [])).toBeNull();
+ expect(await resolveRealOpenablePath(link, other, [root])).toBeNull();
+ expect(await resolveRealOpenablePath(canonicalOutside, other, [root])).toBeNull();
+
+ const result = await readOpenableImage(link, root, [], "image/png");
+ expect(result).toMatchObject({
+ kind: "missing",
+ errorCode: "PATH_OUTSIDE_ALLOWED_ROOT",
+ });
+});
+
+it("keeps relative, absolute and workspace-boundary behavior", async () => {
+ const base = await tempDir("pi-ws-bounds-");
+ const root = join(base, "workspace");
+ const scratch = join(base, "scratch");
+ const attachments = join(base, "attachments");
+ const hash = "a".repeat(64);
+ await mkdir(join(root, "src"), { recursive: true });
+ await mkdir(scratch);
+ await mkdir(attachments);
+ await writeFile(join(root, "src", "a.ts"), "x");
+ await writeFile(join(scratch, "pasted.png"), PNG);
+ await writeFile(join(attachments, hash), PNG);
+ await writeFile(join(base, "outside.png"), PNG);
+
+ const workspaceFile = join(root, "src", "a.ts");
+ expect(await resolveRealOpenablePath("src/a.ts", root, [scratch])).toBe(
+ await realpath(workspaceFile),
+ );
+ expect(await resolveRealOpenablePath(workspaceFile, root, [scratch])).toBe(
+ await realpath(workspaceFile),
+ );
+ expect(await resolveRealOpenablePath(join(scratch, "pasted.png"), root, [scratch])).toBe(
+ await realpath(join(scratch, "pasted.png")),
+ );
+ expect(await resolveRealOpenablePath(`attachments/${hash}`, root, [attachments])).toBe(
+ await realpath(join(attachments, hash)),
+ );
+
+ expect(await resolveRealOpenablePath("../outside.png", root, [scratch])).toBeNull();
+ expect(await resolveRealOpenablePath("src/a.ts", null, [scratch])).toBeNull();
+ expect(await resolveRealOpenablePath("~/secret.png", root, [scratch])).toBeNull();
+ expect(await resolveRealOpenablePath(join(base, "outside.png"), root, [scratch])).toBeNull();
+ expect(await resolveRealOpenablePath(`attachments/notes.png`, root, [attachments])).toBeNull();
+ expect(await resolveRealOpenablePath(`attachments/${hash}`, root, [])).toBeNull();
+ expect(await resolveRealOpenablePath("", root, [scratch])).toBeNull();
+ if (process.platform !== "win32") {
+ expect(await resolveRealOpenablePath("/etc/passwd", root, [scratch])).toBeNull();
+ }
+});
diff --git a/packages/host-runtime/src/workspace-files.ts b/packages/host-runtime/src/workspace-files.ts
index e2f58e3aa..a07ae03bc 100644
--- a/packages/host-runtime/src/workspace-files.ts
+++ b/packages/host-runtime/src/workspace-files.ts
@@ -114,40 +114,63 @@ export function resolveOpenablePath(
return allowed.some((root) => pathIsWithin(root, candidate)) ? candidate : null;
}
+/** `realpath(root)`, or null when it cannot be resolved (missing/denied). */
+async function realRootOrNull(root: string): Promise<string | null> {
+ try {
+ return await realpath(root);
+ } catch {
+ return null;
+ }
+}
+
/**
* Same containment as `resolveOpenablePath`, then `realpath` so a symlink
* inside an allowed root cannot be used to read a file outside it.
+ *
+ * Both the written spelling and the canonical spelling of every root are
+ * accepted, because writers may canonicalize: `image-generation-service`
+ * stores `realpath`d output paths, while the allowed roots here can still be
+ * aliases (`/var` vs `/private/var` on macOS). The `realpath` comparison
+ * against the canonical roots below stays the security gate, so accepting a
+ * canonical candidate cannot widen what is readable.
*/
export async function resolveRealOpenablePath(
path: string,
workspaceRoot: string | null | undefined,
extraRoots: readonly string[] = [],
): Promise<string | null> {
- const lexical = resolveOpenablePath(path, workspaceRoot, extraRoots);
- if (!lexical) return null;
+ const extra = extraRoots.filter(
+ (root) => typeof root === "string" && root.trim(),
+ );
const allowed = [
...(workspaceRoot ? [resolve(workspaceRoot)] : []),
- ...extraRoots
- .filter((root) => typeof root === "string" && root.trim())
- .map((root) => resolve(root)),
+ ...extra.map((root) => resolve(root)),
];
- try {
- const targetReal = await realpath(lexical);
- const realRoots = await Promise.all(
- allowed.map(async (root) => {
- try {
- return await realpath(root);
- } catch {
- return null;
- }
- }),
- );
- return realRoots.some((root) => root && pathIsWithin(root, targetReal))
- ? targetReal
- : null;
- } catch {
- return null;
+ if (allowed.length === 0) return null;
+
+ const [realWorkspaceRoot, ...realExtraRoots] = await Promise.all([
+ workspaceRoot ? realRootOrNull(resolve(workspaceRoot)) : Promise.resolve(null),
+ ...extra.map((root) => realRootOrNull(resolve(root))),
+ ]);
+ const realExtras = realExtraRoots.filter(
+ (root): root is string => Boolean(root),
+ );
+ const realRoots = [...(realWorkspaceRoot ? [realWorkspaceRoot] : []), ...realExtras];
+
+ const candidates = [
+ resolveOpenablePath(path, workspaceRoot, extraRoots),
+ resolveOpenablePath(path, realWorkspaceRoot, realExtras),
+ ];
+ for (const candidate of candidates) {
+ if (!candidate) continue;
+ try {
+ const targetReal = await realpath(candidate);
+ if (realRoots.some((root) => pathIsWithin(root, targetReal))) return targetReal;
+ } catch {
+ // Missing target: try the other spelling before giving up.
+ }
}
+ return null;
}
/** Resolve an existing path and its root through links before containment. */