mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 05:27:39 +00:00
refactor(media): unify safe local file reads
This commit is contained in:
99
src/infra/fs-safe.test.ts
Normal file
99
src/infra/fs-safe.test.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { SafeOpenError, openFileWithinRoot, readLocalFileSafely } from "./fs-safe.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
async function makeTempDir(prefix: string): Promise<string> {
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
|
||||
});
|
||||
|
||||
describe("fs-safe", () => {
|
||||
it("reads a local file safely", async () => {
|
||||
const dir = await makeTempDir("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 makeTempDir("openclaw-fs-safe-");
|
||||
await expect(readLocalFileSafely({ filePath: dir })).rejects.toMatchObject({
|
||||
code: "not-file",
|
||||
});
|
||||
});
|
||||
|
||||
it("enforces maxBytes", async () => {
|
||||
const dir = await makeTempDir("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 makeTempDir("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 makeTempDir("openclaw-fs-safe-root-");
|
||||
const outside = await makeTempDir("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: "invalid-path" });
|
||||
});
|
||||
|
||||
it.runIf(process.platform !== "win32")("blocks symlink escapes under root", async () => {
|
||||
const root = await makeTempDir("openclaw-fs-safe-root-");
|
||||
const outside = await makeTempDir("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("returns not-found for missing files", async () => {
|
||||
const dir = await makeTempDir("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",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user