





























@@ -1,14 +1,15 @@
11import fs from "node:fs/promises";
22import path from "node:path";
3-import { afterEach, describe, expect, it } from "vitest";
4-import { createTrackedTempDirs } from "../../../../src/test-utils/tracked-temp-dirs.js";
3+import { resolvePreferredOpenClawTmpDir, withTempWorkspace } from "openclaw/plugin-sdk/temp-path";
4+import { describe, expect, it } from "vitest";
55import { normalizeUrlPath, resolveFileWithinRoot } from "./file-resolver.js";
667-const tempDirs = createTrackedTempDirs();
8-9-afterEach(async () => {
10-await tempDirs.cleanup();
11-});
7+async function withCanvasTemp<T>(prefix: string, run: (dir: string) => Promise<T>): Promise<T> {
8+return await withTempWorkspace(
9+{ rootDir: resolvePreferredOpenClawTmpDir(), prefix },
10+async ({ dir }) => await run(dir),
11+);
12+}
12131314describe("resolveFileWithinRoot", () => {
1415it("normalizes URL paths", () => {
@@ -17,33 +18,36 @@ describe("resolveFileWithinRoot", () => {
1718});
18191920it("opens directory index files through the fs-safe root", async () => {
20-const root = await tempDirs.make("openclaw-canvas-resolver-");
21-await fs.mkdir(path.join(root, "docs"), { recursive: true });
22-await fs.writeFile(path.join(root, "docs", "index.html"), "<h1>docs</h1>");
23-24-const result = await resolveFileWithinRoot(root, "/docs");
25-expect(result).not.toBeNull();
26-try {
27-await expect(result?.handle.readFile({ encoding: "utf8" })).resolves.toBe("<h1>docs</h1>");
28-} finally {
29-await result?.handle.close().catch(() => {});
30-}
21+await withCanvasTemp("openclaw-canvas-resolver-", async (root) => {
22+await fs.mkdir(path.join(root, "docs"), { recursive: true });
23+await fs.writeFile(path.join(root, "docs", "index.html"), "<h1>docs</h1>");
24+25+const result = await resolveFileWithinRoot(root, "/docs");
26+expect(result).not.toBeNull();
27+try {
28+await expect(result?.handle.readFile({ encoding: "utf8" })).resolves.toBe("<h1>docs</h1>");
29+} finally {
30+await result?.handle.close().catch(() => {});
31+}
32+});
3133});
32343335it("rejects traversal paths", async () => {
34-const root = await tempDirs.make("openclaw-canvas-resolver-");
35-36-await expect(resolveFileWithinRoot(root, "/../outside.txt")).resolves.toBeNull();
36+await withCanvasTemp("openclaw-canvas-resolver-", async (root) => {
37+ await expect(resolveFileWithinRoot(root, "/../outside.txt")).resolves.toBeNull();
38+});
3739});
38403941it.runIf(process.platform !== "win32")("rejects symlink entries", async () => {
40-const root = await tempDirs.make("openclaw-canvas-resolver-");
41-const outside = await tempDirs.make("openclaw-canvas-resolver-outside-");
42-const target = path.join(outside, "outside.html");
43-const link = path.join(root, "link.html");
44-await fs.writeFile(target, "outside");
45-await fs.symlink(target, link);
46-47-await expect(resolveFileWithinRoot(root, "/link.html")).resolves.toBeNull();
42+await withCanvasTemp("openclaw-canvas-resolver-", async (root) => {
43+await withCanvasTemp("openclaw-canvas-resolver-outside-", async (outside) => {
44+const target = path.join(outside, "outside.html");
45+const link = path.join(root, "link.html");
46+await fs.writeFile(target, "outside");
47+await fs.symlink(target, link);
48+49+await expect(resolveFileWithinRoot(root, "/link.html")).resolves.toBeNull();
50+});
51+});
4852});
4953});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。