Skip to content

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

fix(security): enforce symlink root confinement, sha256 digests, and bootstrap isolation

Totopo273b0a331ab5df
diff --git a/.gitignore b/.gitignore
index fe94fa8bb..29b08f653 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,6 +33,10 @@ docs/image/pi_logo.webp
*.cer
*.p8
*.mobileprovision
+*.pem
+*.key
+!scripts/e2e/fixtures/certificates/localhost-cert.pem
+!scripts/e2e/fixtures/certificates/localhost-key.pem
# IDE / OS
.DS_Store
diff --git a/apps/desktop/electron/main/browser-view.ts b/apps/desktop/electron/main/browser-view.ts
index 50174ac01..6aaf8cc7c 100644
--- a/apps/desktop/electron/main/browser-view.ts
+++ b/apps/desktop/electron/main/browser-view.ts
@@ -1,5 +1,5 @@
import { shell, WebContentsView, type BrowserWindow } from "electron";
-import { statSync, watch, type FSWatcher } from "node:fs";
+import { realpathSync, statSync, watch, type FSWatcher } from "node:fs";
import { dirname, isAbsolute, resolve, sep } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import type { BrowserState } from "@pi-desktop/shared";
@@ -38,8 +38,15 @@ export function normalizeUrl(raw: string): string | null {
}
function isWithinRoot(path: string, root: string): boolean {
- const resolvedRoot = resolve(root);
- return path === resolvedRoot || path.startsWith(resolvedRoot + sep);
+ try {
+ const realRoot = realpathSync(resolve(root));
+ const realPath = realpathSync(resolve(path));
+ return realPath === realRoot || realPath.startsWith(realRoot + sep);
+ } catch {
+ const resolvedRoot = resolve(root);
+ const resolvedPath = resolve(path);
+ return resolvedPath === resolvedRoot || resolvedPath.startsWith(resolvedRoot + sep);
+ }
}
/**
@@ -68,11 +75,14 @@ export function resolveLocalFile(raw: string, root: string | null): string | nul
const resolved = resolve(candidate);
if (!isWithinRoot(resolved, root)) return null;
try {
- if (!statSync(resolved).isFile()) return null;
+ const real = realpathSync(resolved);
+ const realRoot = realpathSync(resolve(root));
+ if (real !== realRoot && !real.startsWith(realRoot + sep)) return null;
+ if (!statSync(real).isFile()) return null;
+ return real;
} catch {
return null;
}
- return resolved;
}
export class BrowserPane {
diff --git a/apps/desktop/electron/main/chat-ref-resolve.ts b/apps/desktop/electron/main/chat-ref-resolve.ts
index 1f9d9965d..037cef8a9 100644
--- a/apps/desktop/electron/main/chat-ref-resolve.ts
+++ b/apps/desktop/electron/main/chat-ref-resolve.ts
@@ -50,6 +50,7 @@ type ChatRefRootEntry = {
};
const MAX_REF_LENGTH = 512;
+const ATTACHMENT_HASH_PATTERN = /^[0-9a-f]{64}$/i;
/** A tail longer than this is a user quoting a full path, not a shorthand. */
const MAX_FUZZY_TAIL_SEGMENTS = 6;
/** Bound for the scratch/attachment walk; those trees are small by design. */
@@ -302,13 +303,17 @@ export async function resolveChatFileRef(
if (isAttachmentBlobRef(ref)) {
const attachmentsRoot = roots.attachments;
if (!attachmentsRoot) return null;
- const digest = segmentsOf(cleanRef(ref)).at(-1);
- if (!digest) return null;
- const absolutePath = join(resolve(attachmentsRoot), digest);
+ const blobHash = segmentsOf(cleanRef(ref)).at(-1);
+ if (!blobHash || !ATTACHMENT_HASH_PATTERN.test(blobHash)) return null;
+ const resolvedRoot = resolve(attachmentsRoot);
+ const absolutePath = join(resolvedRoot, blobHash.toLowerCase());
+ if (!absolutePath.startsWith(resolvedRoot + sep) && absolutePath !== resolvedRoot) {
+ return null;
+ }
if (!(await isRegularFile(absolutePath))) return null;
return {
root: "attachments",
- relativePath: digest,
+ relativePath: blobHash.toLowerCase(),
absolutePath,
matchedBy: "exact-relative",
};
diff --git a/apps/desktop/electron/main/remote/pi-host-bootstrap-script.ts b/apps/desktop/electron/main/remote/pi-host-bootstrap-script.ts
index 208965294..29ee8daa3 100644
--- a/apps/desktop/electron/main/remote/pi-host-bootstrap-script.ts
+++ b/apps/desktop/electron/main/remote/pi-host-bootstrap-script.ts
@@ -105,6 +105,7 @@ node_major=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || printf
work="$HOME/.pi-desktop/pi-host/.bootstrap"
mkdir -p "$work"
+chmod 700 "$work" 2>/dev/null || true
log="$work/pi-host.log"
err="$work/pi-host.err"
pidfile="$work/pi-host.pid"
@@ -126,6 +127,7 @@ http_get() {
tarball="$work/$ARTIFACT_NAME"
rm -f "$tarball"
http_get "$ARTIFACT_URL" "$tarball" || die "download-failed"
+chmod 600 "$tarball" 2>/dev/null || true
# --- verify the published digest ------------------------------------------
step "verify"
diff --git a/apps/desktop/electron/main/remote/pi-host-release.ts b/apps/desktop/electron/main/remote/pi-host-release.ts
index 06f2da4d4..e29c995e9 100644
--- a/apps/desktop/electron/main/remote/pi-host-release.ts
+++ b/apps/desktop/electron/main/remote/pi-host-release.ts
@@ -113,9 +113,8 @@ export function parseChecksumFile(text: string, artifactName: string): string |
for (const line of text.split("\n")) {
const trimmed = line.trim();
if (!trimmed) continue;
- const [digest, ...rest] = trimmed.split(/\s+/);
- if (!digest) continue;
- const normalized = normalizeChecksum(digest);
+ const [rawChecksum, ...rest] = trimmed.split(/\s+/);
+ const normalized = normalizeChecksum(rawChecksum ?? "");
if (!normalized) continue;
// `sha256sum` writes the bare name; some tools prefix `*` or a path.
const named = rest.join(" ").replace(/^\*/, "").split("/").pop() ?? "";
diff --git a/apps/desktop/test/chat-ref-resolve.test.mjs b/apps/desktop/test/chat-ref-resolve.test.mjs
index bff59dba7..21aa5bada 100644
--- a/apps/desktop/test/chat-ref-resolve.test.mjs
+++ b/apps/desktop/test/chat-ref-resolve.test.mjs
@@ -181,6 +181,22 @@ test("a content-addressed attachment blob resolves against the attachment store"
);
});
+test("an attachment blob reference rejects invalid hash formats or non-hex characters", async () => {
+ const attachments = tempTree("attachments", ["not-a-valid-sha256"]);
+ assert.equal(
+ await resolve("attachments/not-a-valid-sha256", { attachments }),
+ null,
+ );
+ assert.equal(
+ await resolve(`attachments/${"z".repeat(64)}`, { attachments }),
+ null,
+ );
+ assert.equal(
+ await resolve("attachments/..%2F..%2Fsecrets", { attachments }),
+ null,
+ );
+});
+
test("line and column references are stripped before matching", async () => {
const workspace = tempTree("ws", ["src/a.ts"]);
const match = await resolve("src/a.ts:12:4", { workspace });
diff --git a/scripts/e2e-electron-boot.mjs b/scripts/e2e-electron-boot.mjs
index 92e919f83..ea11b468e 100644
--- a/scripts/e2e-electron-boot.mjs
+++ b/scripts/e2e-electron-boot.mjs
@@ -33,11 +33,13 @@ for (const preloadPath of [
join(appDir, "out/preload/index.cjs"),
join(appDir, "out/preload/plugin-panel.js"),
]) {
- if (!existsSync(preloadPath)) {
- console.error("preload output missing:", preloadPath);
+ let source;
+ try {
+ source = readFileSync(preloadPath, "utf8");
+ } catch {
+ console.error("preload output missing or unreadable:", preloadPath);
process.exit(1);
}
- const source = readFileSync(preloadPath, "utf8");
if (/require\(["']\.\//.test(source)) {
console.error("sandbox preload must not require a local runtime chunk:", preloadPath);
process.exit(1);