Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

fix(policy): expand ~ in file tools to the workplace home, like commands

leen.zhou456c25ff2d71
diff --git a/packages/brain/src/runtime/tools/local-prepare.ts b/packages/brain/src/runtime/tools/local-prepare.ts
index a1da63d..351103d 100644
--- a/packages/brain/src/runtime/tools/local-prepare.ts
+++ b/packages/brain/src/runtime/tools/local-prepare.ts
@@ -189,6 +189,9 @@ async function prepareToolCurrent(
// An external agent's request is judged with the HOME the agent really uses, so `~` on
// its approval card names the user's folder, not the Bot's home inside Pi Bot's data.
const workplace = context.delegatedHome ? { ...resolved, home: context.delegatedHome } : resolved;
+ // Every tool expands `~` against this home, as a command's HOME does: policy, the brain-side
+ // checks below and the UI then name the same place.
+ const policyWorkplace = { ...workplace.value, home: workplace.home };
if (input.tool === "run_command")
assertCommandWritePaths(
input.args,
@@ -579,8 +582,7 @@ async function prepareToolCurrent(
runGrants: context.runGrants,
conversationWebDomains: local.approvals.webDomains(context),
taskWebReadAllowed: local.approvals.taskWebAllowed(context),
- workplace:
- input.tool === "run_command" ? { ...workplace.value, home: workplace.home } : workplace.value,
+ workplace: policyWorkplace,
boundary: workplace.boundary,
cwd: workplace.cwd,
...(workplace.boundary.location?.target !== "local"
@@ -851,7 +853,7 @@ async function prepareToolCurrent(
...(botData ? { botData } : {}),
grants: local.grants(context.botId, context),
runGrants: context.runGrants,
- workplace: workplace.value,
+ workplace: policyWorkplace,
boundary: workplace.boundary,
cwd: workplace.cwd,
yolo,
diff --git a/packages/execution/src/files/apply-patch.ts b/packages/execution/src/files/apply-patch.ts
index affd9e2..b490693 100644
--- a/packages/execution/src/files/apply-patch.ts
+++ b/packages/execution/src/files/apply-patch.ts
@@ -1,7 +1,7 @@
import { randomUUID } from "node:crypto";
import { type BigIntStats, constants } from "node:fs";
import { link, lstat, open, realpath, unlink } from "node:fs/promises";
-import { dirname, join } from "node:path";
+import { dirname, isAbsolute, join, normalize, sep } from "node:path";
import { sameFile } from "../core/file-identity.ts";
import { missing, writablePath } from "../core/paths.ts";
import { ExecError, type FileContext } from "../core/types.ts";
@@ -21,6 +21,26 @@ export interface PatchOptions extends FileContext {
assertCurrent(): void;
}
+/**
+ * `~` names the workplace home, which only the brain knows. Such a header must spell the tail of
+ * the path policy approved for it; that approved path is then checked like any other target.
+ */
+function patchTarget(raw: string, expected: string | undefined) {
+ if (!/^~(?:[\\/]|$)/.test(raw)) return raw;
+ const rest = normalize(raw.slice(2));
+ const fold = (value: string) => (process.platform === "win32" ? value.toLowerCase() : value);
+ if (
+ !expected ||
+ rest === "." ||
+ isAbsolute(rest) ||
+ rest === ".." ||
+ rest.startsWith(`..${sep}`) ||
+ !fold(expected).endsWith(fold(`${sep}${rest}`))
+ )
+ throw new ExecError("invalid_input");
+ return expected;
+}
+
async function content(file: PatchFile, before: string | null, signal: AbortSignal) {
if (file.kind === "add") {
if (before !== null) throw new ExecError("file_conflict");
@@ -89,8 +109,13 @@ export async function applyPatch(options: PatchOptions) {
if (patch.length !== options.expectedPaths.length) throw new ExecError("invalid_input");
await verifyWorkspace(context.rootIdentities, context.signal);
const paths = await Promise.all(
- patch.map((file) =>
- writablePath(file.path, context.cwd, context.writeRoots, context.denyWriteRoots),
+ patch.map((file, i) =>
+ writablePath(
+ patchTarget(file.path, options.expectedPaths[i]),
+ context.cwd,
+ context.writeRoots,
+ context.denyWriteRoots,
+ ),
),
);
// Canonical aliases and ancestor/descendant targets are ambiguous transactions.
diff --git a/packages/policy/src/core/prepare-facts.ts b/packages/policy/src/core/prepare-facts.ts
index 180439f..8f30ed3 100644
--- a/packages/policy/src/core/prepare-facts.ts
+++ b/packages/policy/src/core/prepare-facts.ts
@@ -26,11 +26,11 @@ export async function preparePolicyFacts(
};
const context: PathContext = {
platform,
- // Command HOME is injected from the Bot home by execution, including on local targets.
- home:
- remoteTarget || input.request.category === "run_command"
- ? input.workplace.home
- : options.userHome,
+ // `~` is the workplace home for every tool, as for commands (execution injects it as their
+ // HOME) and the UI. A file tool that expanded it to the computer user's home wrote outside
+ // the workplace, where no file or changes card follows; that home stays reachable only by an
+ // absolute path, and its credential folders stay protected through protectionHome.
+ home: input.workplace.home,
protectionHome: remoteTarget ? input.workplace.home : options.userHome,
cwd: input.workplace.home,
roots: [],
diff --git a/packages/policy/src/core/types.ts b/packages/policy/src/core/types.ts
index 9d9344a..7d0011f 100644
--- a/packages/policy/src/core/types.ts
+++ b/packages/policy/src/core/types.ts
@@ -163,7 +163,7 @@ export interface FileSystem {
export interface PathContext {
platform: Platform;
home: string;
- /** Host credentials/home protection is independent of the command's expansion base. */
+ /** Host credentials/home protection is independent of the `~` expansion base. */
protectionHome?: string;
cwd: string;
roots: string[];