Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(acp): try named absolute paths first and keep inbox/... paths inside the inbox

AdityaUmaled1514a35b825
diff --git a/server/drivers/acp/media-links.test.ts b/server/drivers/acp/media-links.test.ts
index 57cb5b0a68..0e08e9d31e 100644
--- a/server/drivers/acp/media-links.test.ts
+++ b/server/drivers/acp/media-links.test.ts
@@ -44,6 +44,19 @@ describe("findMediaPaths", () => {
expect(await findMediaPaths(`- not${join("inbox", "bot-a", "task one", "handed clip.mp4")}`, scratch)).toEqual([]);
});
+ it("prefers a named absolute path over an inbox/... file inside it, and never reads outside the inbox", async () => {
+ const turn = join(scratch, "turn");
+ const absolute = join(scratch, "archive", "a inbox", "bot-b", "clip.mp4");
+ const local = join(turn, "inbox", "bot-b", "clip.mp4");
+ for (const file of [absolute, local]) {
+ mkdirSync(dirname(file), { recursive: true });
+ writeFileSync(file, Buffer.alloc(100));
+ }
+ expect((await findMediaPaths(`see ${absolute}`, turn)).map((file) => file.path)).toEqual([absolute]);
+ writeFileSync(join(scratch, "secret.pdf"), Buffer.alloc(100));
+ expect(await findMediaPaths(`- ${join("inbox", "..", "..", "secret.pdf")}`, turn)).toEqual([]);
+ });
+
it("ignores text without media", async () => {
expect(await findMediaPaths("see notes.md and https://example.com/a.png")).toEqual([]);
});
diff --git a/server/drivers/acp/media-links.ts b/server/drivers/acp/media-links.ts
index 4d49dd2434..f247ebbd5c 100644
--- a/server/drivers/acp/media-links.ts
+++ b/server/drivers/acp/media-links.ts
@@ -6,7 +6,7 @@ import { stat } from "node:fs/promises";
import { resolve } from "node:path";
import { productName } from "../../brand.ts";
-import { INBOX_FOLDER } from "../../host/handoff-inbox.ts";
+import { INBOX_FOLDER, withinPath } from "../../host/handoff-inbox.ts";
export interface AcpResourceLink {
type: "resource_link";
@@ -50,11 +50,9 @@ export async function findMediaPaths(text: string, cwd?: string): Promise<Array<
for (const match of text.matchAll(MEDIA_END)) {
const end = match.index + match[0].length;
const line = text.slice(text.lastIndexOf("\n", match.index) + 1, end);
- const inbox = cwd === undefined ? [] : [...line.matchAll(INBOX_START)].map((start) => start.index);
- const starts = [...line.matchAll(PATH_START)].map((start) => start.index).concat(inbox).sort((a, b) => b - a);
- for (const start of starts) {
- const named = line.slice(start).trim();
- const path = cwd !== undefined && inbox.includes(start) ? resolve(cwd, named) : named;
+ // Absolute names first, so an absolute path with an `inbox/` folder inside it is never read from `cwd` instead.
+ const absolute = [...line.matchAll(PATH_START)].reverse().map((start) => line.slice(start.index).trim());
+ for (const path of absolute.concat(inboxPaths(line, cwd))) {
const info = await stat(path, { bigint: true }).catch(() => null);
if (!info?.isFile()) continue;
const key = `${info.dev}:${info.ino}`;
@@ -68,6 +66,16 @@ export async function findMediaPaths(text: string, cwd?: string): Promise<Array<
return found;
}
+/** The `inbox/...` names on `line`, nearest first, read from `cwd` and kept only when they stay inside its inbox. */
+function inboxPaths(line: string, cwd: string | undefined): string[] {
+ if (cwd === undefined) return [];
+ const root = resolve(cwd, INBOX_FOLDER);
+ return [...line.matchAll(INBOX_START)]
+ .reverse()
+ .map((start) => resolve(cwd, line.slice(start.index).trim()))
+ .filter((path) => withinPath(path, root));
+}
+
/** Prompt blocks for the media named in `text`, within one prompt's budget, and a note saying what was and was
* not attached. `usedBytes` is media the prompt already carries (composer images): it counts against the same
* budget, so when it alone fills the budget every named file is left out and named in the note. */