Skip to content

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

fix(skill-burger): block symlink escapes in file executor and fix Windows symlink test

mercwang04336f519cca
diff --git a/server/skill-burger/executors.test.ts b/server/skill-burger/executors.test.ts
index 3896e49f85..0f551f07fc 100644
--- a/server/skill-burger/executors.test.ts
+++ b/server/skill-burger/executors.test.ts
@@ -1,4 +1,4 @@
-import { mkdtempSync, readFileSync, rmSync, symlinkSync } from "node:fs";
+import { existsSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
@@ -29,12 +29,37 @@ describe("where a Skill Burger's steps run", () => {
it("writes a file only inside the conversation's folder", async () => {
const cwd = folder();
- const exec = createExecutors({ botId: "b", cwd, internal: async () => ({ ok: false, body: {} }) });
- expect(await exec.file({ path: "notes/a.md", content: "hi", signal: signal() })).toMatchObject({ ok: true });
- expect(readFileSync(join(cwd, "notes/a.md"), "utf8")).toBe("hi");
- expect(await exec.file({ path: "../escape.md", content: "x", signal: signal() })).toMatchObject({ ok: false });
- symlinkSync(tmpdir(), join(cwd, "out"));
- expect(await exec.file({ path: "out/escape.md", content: "x", signal: signal() })).toMatchObject({ ok: false });
+ const outside = folder();
+ try {
+ const exec = createExecutors({ botId: "b", cwd, internal: async () => ({ ok: false, body: {} }) });
+ expect(await exec.file({ path: "notes/a.md", content: "hi", signal: signal() })).toMatchObject({ ok: true });
+ expect(readFileSync(join(cwd, "notes/a.md"), "utf8")).toBe("hi");
+ expect(await exec.file({ path: "..leading.md", content: "ok", signal: signal() })).toMatchObject({ ok: true });
+ expect(await exec.file({ path: "../escape.md", content: "x", signal: signal() })).toMatchObject({ ok: false });
+
+ symlinkSync(outside, join(cwd, "out"), process.platform === "win32" ? "junction" : "dir");
+ expect(await exec.file({ path: "out/escape.md", content: "x", signal: signal() })).toMatchObject({ ok: false });
+ expect(existsSync(join(outside, "escape.md"))).toBe(false);
+ expect(await exec.file({ path: "out/nested/escape.md", content: "x", signal: signal() })).toMatchObject({ ok: false });
+ expect(existsSync(join(outside, "nested"))).toBe(false);
+
+ const outsideFile = join(outside, "secret.md");
+ writeFileSync(outsideFile, "keep");
+ let fileLinked = false;
+ try {
+ symlinkSync(outsideFile, join(cwd, "linked.md"), "file");
+ fileLinked = true;
+ } catch (error) {
+ if (process.platform !== "win32" || (error as NodeJS.ErrnoException).code !== "EPERM") throw error;
+ }
+ if (fileLinked) {
+ expect(await exec.file({ path: "linked.md", content: "overwritten", signal: signal() })).toMatchObject({ ok: false });
+ expect(readFileSync(outsideFile, "utf8")).toBe("keep");
+ }
+ } finally {
+ rmSync(cwd, { recursive: true, force: true });
+ rmSync(outside, { recursive: true, force: true });
+ }
});
it.skipIf(process.platform === "win32")("runs a command as the bot, reading its inputs from OMB_ARG_ variables, and stops one that says too much", async () => {
diff --git a/server/skill-burger/executors.ts b/server/skill-burger/executors.ts
index 517dcf54ef..be32b7bb33 100644
--- a/server/skill-burger/executors.ts
+++ b/server/skill-burger/executors.ts
@@ -3,8 +3,8 @@
// box (spawnCli as "bot"); one on its cloud computer goes through that computer's own route; an app action goes through
// the connectors gate (grants, read or write scope, team rules) with a capability for this turn; a page opens in the
// bot's own browser through its route; a file is written only inside the conversation's folder.
-import { mkdirSync, realpathSync, writeFileSync } from "node:fs";
-import { dirname, isAbsolute, relative, resolve as resolvePath } from "node:path";
+import { closeSync, constants, lstatSync, mkdirSync, openSync, realpathSync, writeFileSync } from "node:fs";
+import { dirname, isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
import { appToolCall } from "../connector-app-tools.ts";
import { toolData } from "../connected-reads.ts";
@@ -12,6 +12,59 @@ import { killCliTree, spawnCli } from "../procs.ts";
import type { ExecResult, StepExecutors } from "./runner.ts";
const MULTI_EXECUTE = "COMPOSIO_MULTI_EXECUTE_TOOL";
+const FILE_OUTSIDE = "a saved step writes files only inside this conversation's folder";
+const NO_FOLLOW = typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0;
+
+function isInside(root: string, candidate: string): boolean {
+ const rel = relative(root, candidate);
+ return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !rel.startsWith("../") && !isAbsolute(rel));
+}
+
+function writeInside(cwd: string, path: string, content: string): ExecResult {
+ const root = realpathSync(cwd);
+ const target = resolvePath(root, path);
+ const inside = relative(root, target);
+ if (!inside || !isInside(root, target)) return { ok: false, error: FILE_OUTSIDE };
+ const dir = dirname(target);
+ // Check the deepest existing ancestor before creating folders so a symlinked directory cannot escape mkdirSync.
+ for (let cur = dir; cur !== root;) {
+ try {
+ lstatSync(cur);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") return { ok: false, error: FILE_OUTSIDE };
+ const parent = dirname(cur);
+ if (parent === cur) return { ok: false, error: FILE_OUTSIDE };
+ cur = parent;
+ continue;
+ }
+ try {
+ if (!isInside(root, realpathSync(cur))) return { ok: false, error: FILE_OUTSIDE };
+ } catch {
+ return { ok: false, error: FILE_OUTSIDE };
+ }
+ break;
+ }
+ mkdirSync(dir, { recursive: true });
+ if (!isInside(root, realpathSync(dir))) return { ok: false, error: FILE_OUTSIDE };
+ try {
+ if (lstatSync(target).isSymbolicLink()) return { ok: false, error: FILE_OUTSIDE };
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") return { ok: false, error: FILE_OUTSIDE };
+ }
+ let fd: number;
+ try {
+ fd = openSync(target, constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | NO_FOLLOW);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ELOOP") return { ok: false, error: FILE_OUTSIDE };
+ throw error;
+ }
+ try {
+ writeFileSync(fd, content);
+ } finally {
+ closeSync(fd);
+ }
+ return { ok: true, output: inside };
+}
export interface ExecutorContext {
botId: string;
@@ -183,14 +236,7 @@ export function createExecutors(context: ExecutorContext): StepExecutors {
return answer.ok ? callText(answer.body) : { ok: false, error: String(answer.body.error ?? "the browser isn't available") };
},
async file(input) {
- const root = realpathSync(context.cwd);
- const target = resolvePath(root, input.path);
- const inside = relative(root, target);
- if (!inside || inside.startsWith("..") || isAbsolute(inside)) return { ok: false, error: "a saved step writes files only inside this conversation's folder" };
- mkdirSync(dirname(target), { recursive: true });
- if (relative(root, realpathSync(dirname(target))).startsWith("..")) return { ok: false, error: "a saved step writes files only inside this conversation's folder" };
- writeFileSync(target, input.content);
- return { ok: true, output: inside };
+ return writeInside(context.cwd, input.path, input.content);
},
};
}