Skip to content

Commit bab4c96

Browse files
PierrunoYTclaude
andcommitted
fix(dcg): name the assumed shell in blocks and enable Windows packs
DCG parses every command as POSIX, so on cmd.exe/PowerShell a block can mean the command was unreadable rather than destructive. Append a note saying so, and enable DCG's windows.filesystem/windows.misc packs on Windows so cmd-only destructive forms (rd /s, format) are matched. Refs #2082 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 24d6398 commit bab4c96

23 files changed

Lines changed: 112 additions & 33 deletions

File tree

‎src/core/tools/ExecuteCommandTool.ts‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import { OutputInterceptor } from "../../integrations/terminal/OutputInterceptor
3131
import { Package } from "../../shared/package"
3232
import { t } from "../../i18n"
3333
import { getTaskDirectoryPath } from "../../utils/storage"
34+
import { getShell } from "../../utils/shell"
35+
import { describeShellDialect, getShellName } from "../../utils/shellDialect"
3436
import { BaseTool, ToolCallbacks } from "./BaseTool"
3537

3638
export { ShellIntegrationError } from "../../integrations/terminal/types"
@@ -55,20 +57,35 @@ interface ExecuteCommandParams {
5557
timeout?: number | null
5658
}
5759

58-
export function formatDcgBlockedMessage(reason?: string, ruleId?: string): string {
59-
if (reason && ruleId) {
60-
return t("tools:executeCommand.destructiveCommandGuard.blockedWithReasonAndRule", { reason, ruleId })
61-
}
60+
/**
61+
* @param shellPath The shell the command will run in. DCG parses every command as POSIX,
62+
* so for cmd.exe/PowerShell the message says so, letting the user tell "dangerous" from "unreadable".
63+
*/
64+
export function formatDcgBlockedMessage(reason?: string, ruleId?: string, shellPath?: string): string {
65+
const message = (() => {
66+
if (reason && ruleId) {
67+
return t("tools:executeCommand.destructiveCommandGuard.blockedWithReasonAndRule", { reason, ruleId })
68+
}
6269

63-
if (reason) {
64-
return t("tools:executeCommand.destructiveCommandGuard.blockedWithReason", { reason })
65-
}
70+
if (reason) {
71+
return t("tools:executeCommand.destructiveCommandGuard.blockedWithReason", { reason })
72+
}
73+
74+
if (ruleId) {
75+
return t("tools:executeCommand.destructiveCommandGuard.blockedWithRule", { ruleId })
76+
}
6677

67-
if (ruleId) {
68-
return t("tools:executeCommand.destructiveCommandGuard.blockedWithRule", { ruleId })
78+
return t("tools:executeCommand.destructiveCommandGuard.blocked")
79+
})()
80+
81+
if (shellPath && describeShellDialect(shellPath)) {
82+
const note = t("tools:executeCommand.destructiveCommandGuard.assumedPosixNote", {
83+
shell: getShellName(shellPath),
84+
})
85+
return `${message} ${note}`
6986
}
7087

71-
return t("tools:executeCommand.destructiveCommandGuard.blocked")
88+
return message
7289
}
7390

7491
export function resolveAgentTimeoutMs(timeoutSeconds: number | null | undefined): number {
@@ -182,7 +199,7 @@ export class ExecuteCommandTool extends BaseTool<"execute_command"> {
182199
// retryable tool_error via the outer catch — never as a policy denial.
183200
dcgDecision = await runDcg(binaryPath, canonicalCommand, workingDirectory)
184201
if (dcgDecision.decision === "deny") {
185-
await task.say("error", formatDcgBlockedMessage(dcgDecision.reason, dcgDecision.ruleId))
202+
await task.say("error", formatDcgBlockedMessage(dcgDecision.reason, dcgDecision.ruleId, getShell()))
186203
}
187204
}
188205

‎src/core/tools/__tests__/executeCommandTool.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ vitest.mock("../../../services/destructive-command-guard", () => ({
7171

7272
// Import the module
7373
import * as executeCommandModule from "../ExecuteCommandTool"
74+
75+
// The DCG block message names the executing shell; pin it so tests do not depend on the host.
76+
vi.mock("../../../utils/shell", () => ({ getShell: vi.fn(() => "/bin/bash") }))
7477
const { executeCommandTool } = executeCommandModule
7578

7679
// Spies installed on the auto-approval module are not restored between tests,
@@ -316,6 +319,17 @@ describe("executeCommandTool", () => {
316319
expect(executeCommandModule.formatDcgBlockedMessage(reason, ruleId)).toBe(expected)
317320
})
318321

322+
it.each([
323+
["C:\\Windows\\System32\\cmd.exe", true],
324+
["C:\\Program Files\\PowerShell\\7\\pwsh.exe", true],
325+
["/bin/bash", false],
326+
[undefined, false],
327+
])("appends the POSIX-assumption note only for non-POSIX shell %s", (shellPath, expectNote) => {
328+
const message = executeCommandModule.formatDcgBlockedMessage(undefined, undefined, shellPath)
329+
330+
expect(message.includes("executeCommand.destructiveCommandGuard.assumedPosixNote")).toBe(expectNote)
331+
})
332+
319333
it("shows a DCG block message as an error before requesting explicit approval", async () => {
320334
const provider = await mockCline.providerRef.deref()
321335
provider.context = { globalStorageUri: { fsPath: "/test/storage" } }

‎src/i18n/locales/ca/tools.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/de/tools.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/en/tools.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"blocked": "Destructive Command Guard blocked this command.",
2020
"blockedWithReason": "Destructive Command Guard blocked this command. Message from DCG: {{reason}}",
2121
"blockedWithRule": "Destructive Command Guard blocked this command. (Rule: {{ruleId}})",
22-
"blockedWithReasonAndRule": "Destructive Command Guard blocked this command. Message from DCG: {{reason}} (Rule: {{ruleId}})"
22+
"blockedWithReasonAndRule": "Destructive Command Guard blocked this command. Message from DCG: {{reason}} (Rule: {{ruleId}})",
23+
"assumedPosixNote": "Note: DCG analyzes commands as POSIX shell syntax, but this command runs in {{shell}}, so a block may mean DCG could not read the command rather than that it is destructive."
2324
}
2425
},
2526
"newTask": {

‎src/i18n/locales/es/tools.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/fr/tools.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/hi/tools.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/id/tools.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/it/tools.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)