Repository navigation
[Fix] apply_diff auto-approves edits outside the workspace #2343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kanchinadd98
wants to merge
2
commits into
Zoo-Code-Org:main
Choose a base branch
from
kanchinadd98:fix/apply-diff-outside-workspace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+247
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 236 additions & 0 deletions
236
src/core/tools/__tests__/applyDiffTool.outsideWorkspace.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| // npx vitest run core/tools/__tests__/applyDiffTool.outsideWorkspace.spec.ts | ||
|
|
||
| import fs from "fs/promises" | ||
| import path from "path" | ||
|
|
||
| import type { ClineSayTool } from "@roo-code/types" | ||
|
|
||
| import type { ToolUse } from "../../../shared/tools" | ||
| import { isPathOutsideWorkspace } from "../../../utils/pathUtils" | ||
| import { checkAutoApproval } from "../../auto-approval" | ||
| import { baseState } from "../../auto-approval/__tests__/fixtures" | ||
| import type { Task } from "../../task/Task" | ||
| import type { ToolCallbacks } from "../BaseTool" | ||
| import { applyDiffTool } from "../ApplyDiffTool" | ||
|
|
||
| vi.mock("@roo-code/telemetry", () => ({ | ||
| TelemetryService: { instance: { captureDiffApplicationError: vi.fn() } }, | ||
| })) | ||
|
|
||
| vi.mock("fs/promises", () => ({ | ||
| default: { | ||
| readFile: vi.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock("../../../utils/fs", () => ({ | ||
| fileExistsAtPath: vi.fn().mockResolvedValue(true), | ||
| })) | ||
|
|
||
| vi.mock("../../../utils/path", () => ({ | ||
| getReadablePath: vi.fn((_cwd: string, relPath?: string) => relPath ?? ""), | ||
| })) | ||
|
|
||
| vi.mock("../../../utils/pathUtils", () => ({ | ||
| isPathOutsideWorkspace: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock("../../prompts/responses", () => ({ | ||
| formatResponse: { | ||
| toolError: vi.fn((message: string) => `Error: ${message}`), | ||
| rooIgnoreError: vi.fn((filePath: string) => `Access denied: ${filePath}`), | ||
| createPrettyPatch: vi.fn(() => "mock-diff"), | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock("../../diff/stats", () => ({ | ||
| sanitizeUnifiedDiff: vi.fn((diff: string) => diff), | ||
| computeDiffStats: vi.fn(() => ({ additions: 1, deletions: 1 })), | ||
| })) | ||
|
|
||
| vi.mock("vscode", () => ({ | ||
| window: { showWarningMessage: vi.fn() }, | ||
| env: { openExternal: vi.fn() }, | ||
| Uri: { parse: vi.fn() }, | ||
| workspace: { | ||
| get workspaceFolders() { | ||
| return [{ uri: { fsPath: path.resolve(CWD) } }] | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| const CWD = "/workspace" | ||
| const INSIDE_PATH = "src/index.ts" | ||
| const OUTSIDE_PATH = "/home/me/.zshrc" | ||
| const DIFF = "<<<<<<< SEARCH\nold\n=======\nnew\n>>>>>>> REPLACE" | ||
|
|
||
| const cases = [ | ||
| { name: "a file outside the workspace", relPath: OUTSIDE_PATH, isOutside: true }, | ||
| { name: "a file inside the workspace", relPath: INSIDE_PATH, isOutside: false }, | ||
| ] | ||
|
|
||
| const approvalPaths = [ | ||
| { name: "diff view", preventFocusDisruption: false }, | ||
| { name: "preventFocusDisruption", preventFocusDisruption: true }, | ||
| ] | ||
|
|
||
| describe("ApplyDiffTool isOutsideWorkspace", () => { | ||
| let askApproval: ReturnType<typeof vi.fn<ToolCallbacks["askApproval"]>> | ||
|
|
||
| function createTask(preventFocusDisruption = false): Task { | ||
| const task = { | ||
| cwd: CWD, | ||
| consecutiveMistakeCount: 0, | ||
| consecutiveMistakeCountForApplyDiff: new Map<string, number>(), | ||
| didEditFile: false, | ||
| api: { getModel: () => ({ id: "claude-test" }) }, | ||
| rooIgnoreController: { validateAccess: vi.fn().mockReturnValue(true) }, | ||
| rooProtectedController: { isWriteProtected: vi.fn().mockReturnValue(false) }, | ||
| providerRef: { | ||
| deref: vi.fn().mockReturnValue({ | ||
| getState: vi.fn().mockResolvedValue({ | ||
| diagnosticsEnabled: false, | ||
| writeDelayMs: 0, | ||
| experiments: { preventFocusDisruption }, | ||
| }), | ||
| }), | ||
| }, | ||
| diffStrategy: { | ||
| getName: () => "test", | ||
| applyDiff: vi.fn().mockResolvedValue({ success: true, content: "new\n" }), | ||
| }, | ||
| diffViewProvider: { | ||
| editType: undefined, | ||
| originalContent: "", | ||
| open: vi.fn().mockResolvedValue(undefined), | ||
| update: vi.fn().mockResolvedValue(undefined), | ||
| reset: vi.fn().mockResolvedValue(undefined), | ||
| revertChanges: vi.fn().mockResolvedValue(undefined), | ||
| saveChanges: vi.fn().mockResolvedValue(undefined), | ||
| saveDirectly: vi.fn().mockResolvedValue(undefined), | ||
| scrollToFirstDiff: vi.fn(), | ||
| pushToolWriteResult: vi.fn().mockResolvedValue("Changes successfully applied."), | ||
| }, | ||
| fileContextTracker: { trackFileContext: vi.fn().mockResolvedValue(undefined) }, | ||
| say: vi.fn().mockResolvedValue(undefined), | ||
| ask: vi.fn().mockResolvedValue(undefined), | ||
| recordToolError: vi.fn(), | ||
| processQueuedMessages: vi.fn(), | ||
| sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing param error"), | ||
| } | ||
| // The double provides only the Task members that ApplyDiffTool.execute and handlePartial read. | ||
| return task as unknown as Task | ||
| } | ||
|
|
||
| async function runExecute(relPath: string, isOutside: boolean, preventFocusDisruption = false): Promise<string> { | ||
| vi.mocked(isPathOutsideWorkspace).mockReturnValue(isOutside) | ||
|
|
||
| await applyDiffTool.execute({ path: relPath, diff: DIFF }, createTask(preventFocusDisruption), { | ||
| askApproval, | ||
| handleError: vi.fn().mockResolvedValue(undefined), | ||
| pushToolResult: vi.fn(), | ||
| toolCallId: "tool-call-1", | ||
| }) | ||
|
|
||
| expect(askApproval).toHaveBeenCalledTimes(1) | ||
| return String(askApproval.mock.calls[0][1]) | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| vi.mocked(isPathOutsideWorkspace).mockReset() | ||
| applyDiffTool.resetPartialState() | ||
| vi.mocked(fs.readFile).mockResolvedValue("old\n") | ||
| askApproval = vi.fn<ToolCallbacks["askApproval"]>().mockResolvedValue(true) | ||
| }) | ||
|
|
||
| describe.each(approvalPaths)("approval payload ($name)", ({ preventFocusDisruption }) => { | ||
| it.each(cases)("reports isOutsideWorkspace for $name", async ({ relPath, isOutside }) => { | ||
| const payload = JSON.parse(await runExecute(relPath, isOutside, preventFocusDisruption)) as ClineSayTool | ||
|
|
||
| expect(isPathOutsideWorkspace).toHaveBeenCalledWith(path.resolve(CWD, relPath)) | ||
| expect(payload.tool).toBe("appliedDiff") | ||
| expect(payload.isOutsideWorkspace).toBe(isOutside) | ||
| }) | ||
| }) | ||
|
|
||
| it.each(cases)("reports isOutsideWorkspace in the partial preview for $name", async ({ relPath, isOutside }) => { | ||
| vi.mocked(isPathOutsideWorkspace).mockReturnValue(isOutside) | ||
| const task = createTask() | ||
| const block: ToolUse<"apply_diff"> = { | ||
| type: "tool_use", | ||
| name: "apply_diff", | ||
| params: { path: relPath, diff: DIFF }, | ||
| partial: true, | ||
| } | ||
|
|
||
| // The first call records the path; the second call sees it stable and asks. | ||
| await applyDiffTool.handlePartial(task, block) | ||
| await applyDiffTool.handlePartial(task, block) | ||
|
|
||
| expect(task.ask).toHaveBeenCalledTimes(1) | ||
| const payload = JSON.parse(String(vi.mocked(task.ask).mock.calls[0][1])) as ClineSayTool | ||
| expect(isPathOutsideWorkspace).toHaveBeenCalledWith(path.resolve(CWD, relPath)) | ||
| expect(payload.isOutsideWorkspace).toBe(isOutside) | ||
| }) | ||
|
|
||
| describe("with the real workspace check", () => { | ||
| it.each([ | ||
| { | ||
| name: "a ../ path that resolves outside", | ||
| relPath: "../outside/config.json", | ||
| isOutside: true, | ||
| expected: "ask", | ||
| }, | ||
| { name: "a path inside", relPath: "src/index.ts", isOutside: false, expected: "approve" }, | ||
| ])("marks $name the workspace and returns $expected", async ({ relPath, isOutside, expected }) => { | ||
| const actual = await vi.importActual<typeof import("../../../utils/pathUtils")>("../../../utils/pathUtils") | ||
| vi.mocked(isPathOutsideWorkspace).mockImplementation(actual.isPathOutsideWorkspace) | ||
|
|
||
| await applyDiffTool.execute({ path: relPath, diff: DIFF }, createTask(), { | ||
| askApproval, | ||
| handleError: vi.fn().mockResolvedValue(undefined), | ||
| pushToolResult: vi.fn(), | ||
| toolCallId: "tool-call-1", | ||
| }) | ||
|
|
||
| const text = String(askApproval.mock.calls[0][1]) | ||
| const payload = JSON.parse(text) as ClineSayTool | ||
| expect(payload.isOutsideWorkspace).toBe(isOutside) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: { ...baseState, alwaysAllowWrite: true }, | ||
| cwd: CWD, | ||
| ask: "tool", | ||
| text, | ||
| }) | ||
| expect(result).toEqual({ decision: expected }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("auto-approval decision with alwaysAllowWrite on", () => { | ||
| it.each([ | ||
| { ...cases[0], allowOutside: false, expected: "ask" }, | ||
| { ...cases[0], allowOutside: true, expected: "approve" }, | ||
| { ...cases[1], allowOutside: false, expected: "approve" }, | ||
| ])( | ||
| "returns $expected for $name when alwaysAllowWriteOutsideWorkspace is $allowOutside", | ||
| async ({ relPath, isOutside, allowOutside, expected }) => { | ||
| const text = await runExecute(relPath, isOutside) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: { | ||
| ...baseState, | ||
| alwaysAllowWrite: true, | ||
| alwaysAllowWriteOutsideWorkspace: allowOutside, | ||
| }, | ||
| cwd: CWD, | ||
| ask: "tool", | ||
| text, | ||
| }) | ||
|
|
||
| expect(result).toEqual({ decision: expected }) | ||
| }, | ||
| ) | ||
| }) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.