Skip to content

Commit a4b2014

Browse files
test(tools): cover apply_diff ../ traversal with the real workspace check
Run isPathOutsideWorkspace against a mocked workspace folder. A ../ path that resolves outside the workspace sets isOutsideWorkspace and asks for approval. A path inside the workspace is still auto-approved. Co-authored-by: Cursor <[email protected]>
1 parent 8a8483e commit a4b2014

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

‎src/core/tools/__tests__/applyDiffTool.outsideWorkspace.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ vi.mock("vscode", () => ({
5252
window: { showWarningMessage: vi.fn() },
5353
env: { openExternal: vi.fn() },
5454
Uri: { parse: vi.fn() },
55+
workspace: {
56+
get workspaceFolders() {
57+
return [{ uri: { fsPath: path.resolve(CWD) } }]
58+
},
59+
},
5560
}))
5661

5762
const CWD = "/workspace"
@@ -133,6 +138,7 @@ describe("ApplyDiffTool isOutsideWorkspace", () => {
133138

134139
beforeEach(() => {
135140
vi.clearAllMocks()
141+
vi.mocked(isPathOutsideWorkspace).mockReset()
136142
applyDiffTool.resetPartialState()
137143
vi.mocked(fs.readFile).mockResolvedValue("old\n")
138144
askApproval = vi.fn<ToolCallbacks["askApproval"]>().mockResolvedValue(true)
@@ -168,6 +174,40 @@ describe("ApplyDiffTool isOutsideWorkspace", () => {
168174
expect(payload.isOutsideWorkspace).toBe(isOutside)
169175
})
170176

177+
describe("with the real workspace check", () => {
178+
it.each([
179+
{
180+
name: "a ../ path that resolves outside",
181+
relPath: "../outside/config.json",
182+
isOutside: true,
183+
expected: "ask",
184+
},
185+
{ name: "a path inside", relPath: "src/index.ts", isOutside: false, expected: "approve" },
186+
])("marks $name the workspace and returns $expected", async ({ relPath, isOutside, expected }) => {
187+
const actual = await vi.importActual<typeof import("../../../utils/pathUtils")>("../../../utils/pathUtils")
188+
vi.mocked(isPathOutsideWorkspace).mockImplementation(actual.isPathOutsideWorkspace)
189+
190+
await applyDiffTool.execute({ path: relPath, diff: DIFF }, createTask(), {
191+
askApproval,
192+
handleError: vi.fn().mockResolvedValue(undefined),
193+
pushToolResult: vi.fn(),
194+
toolCallId: "tool-call-1",
195+
})
196+
197+
const text = String(askApproval.mock.calls[0][1])
198+
const payload = JSON.parse(text) as ClineSayTool
199+
expect(payload.isOutsideWorkspace).toBe(isOutside)
200+
201+
const result = await checkAutoApproval({
202+
state: { ...baseState, alwaysAllowWrite: true },
203+
cwd: CWD,
204+
ask: "tool",
205+
text,
206+
})
207+
expect(result).toEqual({ decision: expected })
208+
})
209+
})
210+
171211
describe("auto-approval decision with alwaysAllowWrite on", () => {
172212
it.each([
173213
{ ...cases[0], allowOutside: false, expected: "ask" },

0 commit comments

Comments
 (0)