|
|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
import fs from "node:fs/promises";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
import { withRealpathSymlinkRebindRace } from "../test-utils/symlink-rebind-race.js";
|
|
|
|
|
import {
|
|
|
|
|
createRebindableDirectoryAlias,
|
|
|
|
|
withRealpathSymlinkRebindRace,
|
|
|
|
|
} from "../test-utils/symlink-rebind-race.js";
|
|
|
|
|
import { createTrackedTempDirs } from "../test-utils/tracked-temp-dirs.js";
|
|
|
|
|
import {
|
|
|
|
|
copyFileWithinRoot,
|
|
|
|
|
@ -269,100 +272,27 @@ describe("fs-safe", () => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.runIf(process.platform !== "win32")(
|
|
|
|
|
"does not truncate out-of-root file when symlink retarget races write open",
|
|
|
|
|
async () => {
|
|
|
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
|
|
|
const inside = path.join(root, "inside");
|
|
|
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
|
|
|
await fs.mkdir(inside, { recursive: true });
|
|
|
|
|
const insideTarget = path.join(inside, "target.txt");
|
|
|
|
|
const outsideTarget = path.join(outside, "target.txt");
|
|
|
|
|
await fs.writeFile(insideTarget, "inside");
|
|
|
|
|
await fs.writeFile(outsideTarget, "X".repeat(4096));
|
|
|
|
|
const slot = path.join(root, "slot");
|
|
|
|
|
await fs.symlink(inside, slot);
|
|
|
|
|
|
|
|
|
|
await withRealpathSymlinkRebindRace({
|
|
|
|
|
shouldFlip: (realpathInput) => realpathInput.endsWith(path.join("slot", "target.txt")),
|
|
|
|
|
symlinkPath: slot,
|
|
|
|
|
symlinkTarget: outside,
|
|
|
|
|
timing: "before-realpath",
|
|
|
|
|
run: async () => {
|
|
|
|
|
await expect(
|
|
|
|
|
writeFileWithinRoot({
|
|
|
|
|
rootDir: root,
|
|
|
|
|
relativePath: path.join("slot", "target.txt"),
|
|
|
|
|
data: "new-content",
|
|
|
|
|
mkdir: false,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toMatchObject({ code: "outside-workspace" });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(fs.readFile(outsideTarget, "utf8")).resolves.toBe("X".repeat(4096));
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it.runIf(process.platform !== "win32")(
|
|
|
|
|
"does not clobber out-of-root file when symlink retarget races write-from-path open",
|
|
|
|
|
async () => {
|
|
|
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
|
|
|
const inside = path.join(root, "inside");
|
|
|
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
|
|
|
const sourceDir = await tempDirs.make("openclaw-fs-safe-source-");
|
|
|
|
|
const sourcePath = path.join(sourceDir, "source.txt");
|
|
|
|
|
await fs.writeFile(sourcePath, "new-content");
|
|
|
|
|
await fs.mkdir(inside, { recursive: true });
|
|
|
|
|
const outsideTarget = path.join(outside, "target.txt");
|
|
|
|
|
await fs.writeFile(outsideTarget, "X".repeat(4096));
|
|
|
|
|
const slot = path.join(root, "slot");
|
|
|
|
|
await fs.symlink(inside, slot);
|
|
|
|
|
|
|
|
|
|
await withRealpathSymlinkRebindRace({
|
|
|
|
|
shouldFlip: (realpathInput) => realpathInput.endsWith(path.join("slot", "target.txt")),
|
|
|
|
|
symlinkPath: slot,
|
|
|
|
|
symlinkTarget: outside,
|
|
|
|
|
timing: "before-realpath",
|
|
|
|
|
run: async () => {
|
|
|
|
|
await expect(
|
|
|
|
|
writeFileFromPathWithinRoot({
|
|
|
|
|
rootDir: root,
|
|
|
|
|
relativePath: path.join("slot", "target.txt"),
|
|
|
|
|
sourcePath,
|
|
|
|
|
mkdir: false,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toMatchObject({ code: "outside-workspace" });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
it("does not truncate out-of-root file when symlink retarget races write open", async () => {
|
|
|
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
|
|
|
const inside = path.join(root, "inside");
|
|
|
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
|
|
|
await fs.mkdir(inside, { recursive: true });
|
|
|
|
|
const insideTarget = path.join(inside, "target.txt");
|
|
|
|
|
const outsideTarget = path.join(outside, "target.txt");
|
|
|
|
|
await fs.writeFile(insideTarget, "inside");
|
|
|
|
|
await fs.writeFile(outsideTarget, "X".repeat(4096));
|
|
|
|
|
const slot = path.join(root, "slot");
|
|
|
|
|
await createRebindableDirectoryAlias({
|
|
|
|
|
aliasPath: slot,
|
|
|
|
|
targetPath: inside,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(fs.readFile(outsideTarget, "utf8")).resolves.toBe("X".repeat(4096));
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it.runIf(process.platform !== "win32")(
|
|
|
|
|
"cleans up created out-of-root file when symlink retarget races create path",
|
|
|
|
|
async () => {
|
|
|
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
|
|
|
const inside = path.join(root, "inside");
|
|
|
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
|
|
|
await fs.mkdir(inside, { recursive: true });
|
|
|
|
|
const outsideTarget = path.join(outside, "target.txt");
|
|
|
|
|
const slot = path.join(root, "slot");
|
|
|
|
|
await fs.symlink(inside, slot);
|
|
|
|
|
|
|
|
|
|
const realOpen = fs.open.bind(fs);
|
|
|
|
|
let flipped = false;
|
|
|
|
|
const openSpy = vi.spyOn(fs, "open").mockImplementation(async (...args) => {
|
|
|
|
|
const [filePath] = args;
|
|
|
|
|
if (!flipped && String(filePath).endsWith(path.join("slot", "target.txt"))) {
|
|
|
|
|
flipped = true;
|
|
|
|
|
await fs.rm(slot, { recursive: true, force: true });
|
|
|
|
|
await fs.symlink(outside, slot);
|
|
|
|
|
}
|
|
|
|
|
return await realOpen(...args);
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
await withRealpathSymlinkRebindRace({
|
|
|
|
|
shouldFlip: (realpathInput) => realpathInput.endsWith(path.join("slot", "target.txt")),
|
|
|
|
|
symlinkPath: slot,
|
|
|
|
|
symlinkTarget: outside,
|
|
|
|
|
timing: "before-realpath",
|
|
|
|
|
run: async () => {
|
|
|
|
|
await expect(
|
|
|
|
|
writeFileWithinRoot({
|
|
|
|
|
rootDir: root,
|
|
|
|
|
@ -371,13 +301,88 @@ describe("fs-safe", () => {
|
|
|
|
|
mkdir: false,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toMatchObject({ code: "outside-workspace" });
|
|
|
|
|
} finally {
|
|
|
|
|
openSpy.mockRestore();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(fs.readFile(outsideTarget, "utf8")).resolves.toBe("X".repeat(4096));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not clobber out-of-root file when symlink retarget races write-from-path open", async () => {
|
|
|
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
|
|
|
const inside = path.join(root, "inside");
|
|
|
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
|
|
|
const sourceDir = await tempDirs.make("openclaw-fs-safe-source-");
|
|
|
|
|
const sourcePath = path.join(sourceDir, "source.txt");
|
|
|
|
|
await fs.writeFile(sourcePath, "new-content");
|
|
|
|
|
await fs.mkdir(inside, { recursive: true });
|
|
|
|
|
const outsideTarget = path.join(outside, "target.txt");
|
|
|
|
|
await fs.writeFile(outsideTarget, "X".repeat(4096));
|
|
|
|
|
const slot = path.join(root, "slot");
|
|
|
|
|
await createRebindableDirectoryAlias({
|
|
|
|
|
aliasPath: slot,
|
|
|
|
|
targetPath: inside,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await withRealpathSymlinkRebindRace({
|
|
|
|
|
shouldFlip: (realpathInput) => realpathInput.endsWith(path.join("slot", "target.txt")),
|
|
|
|
|
symlinkPath: slot,
|
|
|
|
|
symlinkTarget: outside,
|
|
|
|
|
timing: "before-realpath",
|
|
|
|
|
run: async () => {
|
|
|
|
|
await expect(
|
|
|
|
|
writeFileFromPathWithinRoot({
|
|
|
|
|
rootDir: root,
|
|
|
|
|
relativePath: path.join("slot", "target.txt"),
|
|
|
|
|
sourcePath,
|
|
|
|
|
mkdir: false,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toMatchObject({ code: "outside-workspace" });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(fs.readFile(outsideTarget, "utf8")).resolves.toBe("X".repeat(4096));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("cleans up created out-of-root file when symlink retarget races create path", async () => {
|
|
|
|
|
const root = await tempDirs.make("openclaw-fs-safe-root-");
|
|
|
|
|
const inside = path.join(root, "inside");
|
|
|
|
|
const outside = await tempDirs.make("openclaw-fs-safe-outside-");
|
|
|
|
|
await fs.mkdir(inside, { recursive: true });
|
|
|
|
|
const outsideTarget = path.join(outside, "target.txt");
|
|
|
|
|
const slot = path.join(root, "slot");
|
|
|
|
|
await createRebindableDirectoryAlias({
|
|
|
|
|
aliasPath: slot,
|
|
|
|
|
targetPath: inside,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const realOpen = fs.open.bind(fs);
|
|
|
|
|
let flipped = false;
|
|
|
|
|
const openSpy = vi.spyOn(fs, "open").mockImplementation(async (...args) => {
|
|
|
|
|
const [filePath] = args;
|
|
|
|
|
if (!flipped && String(filePath).endsWith(path.join("slot", "target.txt"))) {
|
|
|
|
|
flipped = true;
|
|
|
|
|
await createRebindableDirectoryAlias({
|
|
|
|
|
aliasPath: slot,
|
|
|
|
|
targetPath: outside,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return await realOpen(...args);
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
await expect(
|
|
|
|
|
writeFileWithinRoot({
|
|
|
|
|
rootDir: root,
|
|
|
|
|
relativePath: path.join("slot", "target.txt"),
|
|
|
|
|
data: "new-content",
|
|
|
|
|
mkdir: false,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toMatchObject({ code: "outside-workspace" });
|
|
|
|
|
} finally {
|
|
|
|
|
openSpy.mockRestore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await expect(fs.stat(outsideTarget)).rejects.toMatchObject({ code: "ENOENT" });
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
await expect(fs.stat(outsideTarget)).rejects.toMatchObject({ code: "ENOENT" });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns not-found for missing files", async () => {
|
|
|
|
|
const dir = await tempDirs.make("openclaw-fs-safe-");
|
|
|
|
|
|