|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { createOpenClawCodingTools } from "./pi-tools.js"; |
| 6 | + |
| 7 | +describe("FS tools with workspaceOnly=false", () => { |
| 8 | + let tmpDir: string; |
| 9 | + let workspaceDir: string; |
| 10 | + let outsideFile: string; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-test-")); |
| 14 | + workspaceDir = path.join(tmpDir, "workspace"); |
| 15 | + await fs.mkdir(workspaceDir); |
| 16 | + outsideFile = path.join(tmpDir, "outside.txt"); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(async () => { |
| 20 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should allow write outside workspace when workspaceOnly=false", async () => { |
| 24 | + const tools = createOpenClawCodingTools({ |
| 25 | + workspaceDir, |
| 26 | + config: { |
| 27 | + tools: { |
| 28 | + fs: { |
| 29 | + workspaceOnly: false, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + const writeTool = tools.find((t) => t.name === "write"); |
| 36 | + expect(writeTool).toBeDefined(); |
| 37 | + |
| 38 | + const result = await writeTool!.execute("test-call-1", { |
| 39 | + path: outsideFile, |
| 40 | + content: "test content", |
| 41 | + }); |
| 42 | + |
| 43 | + // Check if the operation succeeded (no error in content) |
| 44 | + const hasError = result.content.some( |
| 45 | + (c) => c.type === "text" && c.text.toLowerCase().includes("error"), |
| 46 | + ); |
| 47 | + expect(hasError).toBe(false); |
| 48 | + const content = await fs.readFile(outsideFile, "utf-8"); |
| 49 | + expect(content).toBe("test content"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should allow write outside workspace via ../ path when workspaceOnly=false", async () => { |
| 53 | + const relativeOutsidePath = path.join("..", "outside-relative-write.txt"); |
| 54 | + const outsideRelativeFile = path.join(tmpDir, "outside-relative-write.txt"); |
| 55 | + |
| 56 | + const tools = createOpenClawCodingTools({ |
| 57 | + workspaceDir, |
| 58 | + config: { |
| 59 | + tools: { |
| 60 | + fs: { |
| 61 | + workspaceOnly: false, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const writeTool = tools.find((t) => t.name === "write"); |
| 68 | + expect(writeTool).toBeDefined(); |
| 69 | + |
| 70 | + const result = await writeTool!.execute("test-call-1b", { |
| 71 | + path: relativeOutsidePath, |
| 72 | + content: "relative test content", |
| 73 | + }); |
| 74 | + |
| 75 | + const hasError = result.content.some( |
| 76 | + (c) => c.type === "text" && c.text.toLowerCase().includes("error"), |
| 77 | + ); |
| 78 | + expect(hasError).toBe(false); |
| 79 | + const content = await fs.readFile(outsideRelativeFile, "utf-8"); |
| 80 | + expect(content).toBe("relative test content"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should allow edit outside workspace when workspaceOnly=false", async () => { |
| 84 | + await fs.writeFile(outsideFile, "old content"); |
| 85 | + |
| 86 | + const tools = createOpenClawCodingTools({ |
| 87 | + workspaceDir, |
| 88 | + config: { |
| 89 | + tools: { |
| 90 | + fs: { |
| 91 | + workspaceOnly: false, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + const editTool = tools.find((t) => t.name === "edit"); |
| 98 | + expect(editTool).toBeDefined(); |
| 99 | + |
| 100 | + const result = await editTool!.execute("test-call-2", { |
| 101 | + path: outsideFile, |
| 102 | + oldText: "old content", |
| 103 | + newText: "new content", |
| 104 | + }); |
| 105 | + |
| 106 | + // Check if the operation succeeded (no error in content) |
| 107 | + const hasError = result.content.some( |
| 108 | + (c) => c.type === "text" && c.text.toLowerCase().includes("error"), |
| 109 | + ); |
| 110 | + expect(hasError).toBe(false); |
| 111 | + const content = await fs.readFile(outsideFile, "utf-8"); |
| 112 | + expect(content).toBe("new content"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should allow edit outside workspace via ../ path when workspaceOnly=false", async () => { |
| 116 | + const relativeOutsidePath = path.join("..", "outside-relative-edit.txt"); |
| 117 | + const outsideRelativeFile = path.join(tmpDir, "outside-relative-edit.txt"); |
| 118 | + await fs.writeFile(outsideRelativeFile, "old relative content"); |
| 119 | + |
| 120 | + const tools = createOpenClawCodingTools({ |
| 121 | + workspaceDir, |
| 122 | + config: { |
| 123 | + tools: { |
| 124 | + fs: { |
| 125 | + workspaceOnly: false, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + const editTool = tools.find((t) => t.name === "edit"); |
| 132 | + expect(editTool).toBeDefined(); |
| 133 | + |
| 134 | + const result = await editTool!.execute("test-call-2b", { |
| 135 | + path: relativeOutsidePath, |
| 136 | + oldText: "old relative content", |
| 137 | + newText: "new relative content", |
| 138 | + }); |
| 139 | + |
| 140 | + const hasError = result.content.some( |
| 141 | + (c) => c.type === "text" && c.text.toLowerCase().includes("error"), |
| 142 | + ); |
| 143 | + expect(hasError).toBe(false); |
| 144 | + const content = await fs.readFile(outsideRelativeFile, "utf-8"); |
| 145 | + expect(content).toBe("new relative content"); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should allow read outside workspace when workspaceOnly=false", async () => { |
| 149 | + await fs.writeFile(outsideFile, "test read content"); |
| 150 | + |
| 151 | + const tools = createOpenClawCodingTools({ |
| 152 | + workspaceDir, |
| 153 | + config: { |
| 154 | + tools: { |
| 155 | + fs: { |
| 156 | + workspaceOnly: false, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const readTool = tools.find((t) => t.name === "read"); |
| 163 | + expect(readTool).toBeDefined(); |
| 164 | + |
| 165 | + const result = await readTool!.execute("test-call-3", { |
| 166 | + path: outsideFile, |
| 167 | + }); |
| 168 | + |
| 169 | + // Check if the operation succeeded (no error in content) |
| 170 | + const hasError = result.content.some( |
| 171 | + (c) => c.type === "text" && c.text.toLowerCase().includes("error"), |
| 172 | + ); |
| 173 | + expect(hasError).toBe(false); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should block write outside workspace when workspaceOnly=true", async () => { |
| 177 | + const tools = createOpenClawCodingTools({ |
| 178 | + workspaceDir, |
| 179 | + config: { |
| 180 | + tools: { |
| 181 | + fs: { |
| 182 | + workspaceOnly: true, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }); |
| 187 | + |
| 188 | + const writeTool = tools.find((t) => t.name === "write"); |
| 189 | + expect(writeTool).toBeDefined(); |
| 190 | + |
| 191 | + // When workspaceOnly=true, the guard throws an error |
| 192 | + await expect( |
| 193 | + writeTool!.execute("test-call-4", { |
| 194 | + path: outsideFile, |
| 195 | + content: "test content", |
| 196 | + }), |
| 197 | + ).rejects.toThrow(/Path escapes (workspace|sandbox) root/); |
| 198 | + }); |
| 199 | +}); |
0 commit comments