240 lines
7.9 KiB
TypeScript
240 lines
7.9 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { createTrackedTempDirs } from "../test-utils/tracked-temp-dirs.js";
|
|
import {
|
|
SafeOpenError,
|
|
openFileWithinRoot,
|
|
readLocalFileSafely,
|
|
writeFileWithinRoot,
|
|
} from "./fs-safe.js";
|
|
|
|
const tempDirs = createTrackedTempDirs();
|
|
|
|
afterEach(async () => {
|
|
await tempDirs.cleanup();
|
|
});
|
|
|
|
describe("fs-safe", () => {
|
|
it("reads a local file safely", async () => {
|
|
const dir = await tempDirs.make("openclaw-fs-safe-");
|
|
const file = path.join(dir, "payload.txt");
|
|
await fs.writeFile(file, "hello");
|
|
|
|
const result = await readLocalFileSafely({ filePath: file });
|
|
expect(result.buffer.toString("utf8")).toBe("hello");
|
|
expect(result.stat.size).toBe(5);
|
|
expect(result.realPath).toContain("payload.txt");
|
|
});
|
|
|
|
it("rejects directories", async () => {
|
|
const dir = await tempDirs.make("openclaw-fs-safe-");
|
|
await expect(readLocalFileSafely({ filePath: dir })).rejects.toMatchObject({
|
|
code: "not-file",
|
|
});
|
|
});
|
|
|
|
it("enforces maxBytes", async () => {
|
|
const dir = await tempDirs.make("openclaw-fs-safe-");
|
|
const file = path.join(dir, "big.bin");
|
|
await fs.writeFile(file, Buffer.alloc(8));
|
|
|
|
await expect(readLocalFileSafely({ filePath: file, maxBytes: 4 })).rejects.toMatchObject({
|
|
code: "too-large",
|
|
});
|
|
});
|
|
|
|
it.runIf(process.platform !== "win32")("rejects symlinks", async () => {
|
|
const dir = await tempDirs.make("openclaw-fs-safe-");
|
|
const target = path.join(dir, "target.txt");
|
|
const link = path.join(dir, "link.txt");
|
|
await fs.writeFile(target, "target");
|
|
await fs.symlink(target, link);
|
|
|
|
await expect(readLocalFileSafely({ filePath: link })).rejects.toMatchObject({
|
|
code: "symlink",
|
|
});
|
|
});
|
|
|
|
it("blocks traversal outside root", async () => {
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
const file = path.join(outside, "outside.txt");
|
|
await fs.writeFile(file, "outside");
|
|
|
|
await expect(
|
|
openFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: path.join("..", path.basename(outside), "outside.txt"),
|
|
}),
|
|
).rejects.toMatchObject({ code: "outside-workspace" });
|
|
});
|
|
|
|
it.runIf(process.platform !== "win32")("blocks symlink escapes under root", async () => {
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
const target = path.join(outside, "outside.txt");
|
|
const link = path.join(root, "link.txt");
|
|
await fs.writeFile(target, "outside");
|
|
await fs.symlink(target, link);
|
|
|
|
await expect(
|
|
openFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "link.txt",
|
|
}),
|
|
).rejects.toMatchObject({ code: "invalid-path" });
|
|
});
|
|
|
|
it.runIf(process.platform !== "win32")("blocks hardlink aliases under root", async () => {
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
const outsideFile = path.join(outside, "outside.txt");
|
|
const hardlinkPath = path.join(root, "link.txt");
|
|
await fs.writeFile(outsideFile, "outside");
|
|
try {
|
|
try {
|
|
await fs.link(outsideFile, hardlinkPath);
|
|
} catch (err) {
|
|
if ((err as NodeJS.ErrnoException).code === "EXDEV") {
|
|
return;
|
|
}
|
|
throw err;
|
|
}
|
|
await expect(
|
|
openFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "link.txt",
|
|
}),
|
|
).rejects.toMatchObject({ code: "invalid-path" });
|
|
} finally {
|
|
await fs.rm(hardlinkPath, { force: true });
|
|
await fs.rm(outsideFile, { force: true });
|
|
}
|
|
});
|
|
|
|
it("writes a file within root safely", async () => {
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
await writeFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "nested/out.txt",
|
|
data: "hello",
|
|
});
|
|
await expect(fs.readFile(path.join(root, "nested", "out.txt"), "utf8")).resolves.toBe("hello");
|
|
});
|
|
|
|
it("rejects write traversal outside root", async () => {
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
await expect(
|
|
writeFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "../escape.txt",
|
|
data: "x",
|
|
}),
|
|
).rejects.toMatchObject({ code: "outside-workspace" });
|
|
});
|
|
|
|
it.runIf(process.platform !== "win32")("rejects writing through hardlink aliases", async () => {
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
const outsideFile = path.join(outside, "outside.txt");
|
|
const hardlinkPath = path.join(root, "alias.txt");
|
|
await fs.writeFile(outsideFile, "outside");
|
|
try {
|
|
try {
|
|
await fs.link(outsideFile, hardlinkPath);
|
|
} catch (err) {
|
|
if ((err as NodeJS.ErrnoException).code === "EXDEV") {
|
|
return;
|
|
}
|
|
throw err;
|
|
}
|
|
await expect(
|
|
writeFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "alias.txt",
|
|
data: "pwned",
|
|
}),
|
|
).rejects.toMatchObject({ code: "invalid-path" });
|
|
await expect(fs.readFile(outsideFile, "utf8")).resolves.toBe("outside");
|
|
} finally {
|
|
await fs.rm(hardlinkPath, { force: true });
|
|
await fs.rm(outsideFile, { force: true });
|
|
}
|
|
});
|
|
|
|
it("returns not-found for missing files", async () => {
|
|
const dir = await tempDirs.make("openclaw-fs-safe-");
|
|
const missing = path.join(dir, "missing.txt");
|
|
|
|
await expect(readLocalFileSafely({ filePath: missing })).rejects.toBeInstanceOf(SafeOpenError);
|
|
await expect(readLocalFileSafely({ filePath: missing })).rejects.toMatchObject({
|
|
code: "not-found",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("tilde expansion in file tools", () => {
|
|
it("expandHomePrefix respects process.env.HOME changes", async () => {
|
|
const { expandHomePrefix } = await import("./home-dir.js");
|
|
const originalHome = process.env.HOME;
|
|
const fakeHome = "/tmp/fake-home-test";
|
|
process.env.HOME = fakeHome;
|
|
try {
|
|
const result = expandHomePrefix("~/file.txt");
|
|
expect(path.normalize(result)).toBe(path.join(path.resolve(fakeHome), "file.txt"));
|
|
} finally {
|
|
process.env.HOME = originalHome;
|
|
}
|
|
});
|
|
|
|
it("reads a file via ~/path after HOME override", async () => {
|
|
const root = await tempDirs.make("openclaw-tilde-test-");
|
|
const originalHome = process.env.HOME;
|
|
process.env.HOME = root;
|
|
try {
|
|
await fs.writeFile(path.join(root, "hello.txt"), "tilde-works");
|
|
const result = await openFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "~/hello.txt",
|
|
});
|
|
const buf = Buffer.alloc(result.stat.size);
|
|
await result.handle.read(buf, 0, buf.length, 0);
|
|
await result.handle.close();
|
|
expect(buf.toString("utf8")).toBe("tilde-works");
|
|
} finally {
|
|
process.env.HOME = originalHome;
|
|
}
|
|
});
|
|
|
|
it("writes a file via ~/path after HOME override", async () => {
|
|
const root = await tempDirs.make("openclaw-tilde-test-");
|
|
const originalHome = process.env.HOME;
|
|
process.env.HOME = root;
|
|
try {
|
|
await writeFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "~/output.txt",
|
|
data: "tilde-write-works",
|
|
});
|
|
const content = await fs.readFile(path.join(root, "output.txt"), "utf8");
|
|
expect(content).toBe("tilde-write-works");
|
|
} finally {
|
|
process.env.HOME = originalHome;
|
|
}
|
|
});
|
|
|
|
it("rejects ~/path that resolves outside root", async () => {
|
|
const root = await tempDirs.make("openclaw-tilde-outside-");
|
|
// HOME points to real home, ~/file goes to /home/dev/file which is outside root
|
|
await expect(
|
|
openFileWithinRoot({
|
|
rootDir: root,
|
|
relativePath: "~/escape.txt",
|
|
}),
|
|
).rejects.toMatchObject({
|
|
code: expect.stringMatching(/outside-workspace|not-found|invalid-path/),
|
|
});
|
|
});
|
|
});
|