Skip to content

Commit 8ef359f

Browse files
fix: allowed commands import/export (#5110)
Co-authored-by: Daniel Riccio <[email protected]>
1 parent 3598e3c commit 8ef359f

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,38 @@ export class ClineProvider
13031303
return await fileExistsAtPath(promptFilePath)
13041304
}
13051305

1306+
/**
1307+
* Merges allowed commands from global state and workspace configuration
1308+
* with proper validation and deduplication
1309+
*/
1310+
private mergeAllowedCommands(globalStateCommands?: string[]): string[] {
1311+
try {
1312+
// Validate and sanitize global state commands
1313+
const validGlobalCommands = Array.isArray(globalStateCommands)
1314+
? globalStateCommands.filter((cmd) => typeof cmd === "string" && cmd.trim().length > 0)
1315+
: []
1316+
1317+
// Get workspace configuration commands
1318+
const workspaceCommands =
1319+
vscode.workspace.getConfiguration(Package.name).get<string[]>("allowedCommands") || []
1320+
1321+
// Validate and sanitize workspace commands
1322+
const validWorkspaceCommands = Array.isArray(workspaceCommands)
1323+
? workspaceCommands.filter((cmd) => typeof cmd === "string" && cmd.trim().length > 0)
1324+
: []
1325+
1326+
// Combine and deduplicate commands
1327+
// Global state takes precedence over workspace configuration
1328+
const mergedCommands = [...new Set([...validGlobalCommands, ...validWorkspaceCommands])]
1329+
1330+
return mergedCommands
1331+
} catch (error) {
1332+
console.error("Error merging allowed commands:", error)
1333+
// Return empty array as fallback to prevent crashes
1334+
return []
1335+
}
1336+
}
1337+
13061338
async getStateToPostToWebview() {
13071339
const {
13081340
apiConfiguration,
@@ -1314,6 +1346,7 @@ export class ClineProvider
13141346
alwaysAllowWriteOutsideWorkspace,
13151347
alwaysAllowWriteProtected,
13161348
alwaysAllowExecute,
1349+
allowedCommands,
13171350
alwaysAllowBrowser,
13181351
alwaysAllowMcp,
13191352
alwaysAllowModeSwitch,
@@ -1381,7 +1414,7 @@ export class ClineProvider
13811414

13821415
const telemetryKey = process.env.POSTHOG_API_KEY
13831416
const machineId = vscode.env.machineId
1384-
const allowedCommands = vscode.workspace.getConfiguration(Package.name).get<string[]>("allowedCommands") || []
1417+
const mergedAllowedCommands = this.mergeAllowedCommands(allowedCommands)
13851418
const cwd = this.cwd
13861419

13871420
// Check if there's a system prompt override for the current mode
@@ -1420,7 +1453,7 @@ export class ClineProvider
14201453
enableCheckpoints: enableCheckpoints ?? true,
14211454
shouldShowAnnouncement:
14221455
telemetrySetting !== "unset" && lastShownAnnouncementId !== this.latestAnnouncementId,
1423-
allowedCommands,
1456+
allowedCommands: mergedAllowedCommands,
14241457
soundVolume: soundVolume ?? 0.5,
14251458
browserViewportSize: browserViewportSize ?? "900x600",
14261459
screenshotQuality: screenshotQuality ?? 75,

src/core/webview/webviewMessageHandler.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,22 @@ export const webviewMessageHandler = async (
566566
case "cancelTask":
567567
await provider.cancelTask()
568568
break
569-
case "allowedCommands":
570-
await provider.context.globalState.update("allowedCommands", message.commands)
569+
case "allowedCommands": {
570+
// Validate and sanitize the commands array
571+
const commands = message.commands ?? []
572+
const validCommands = Array.isArray(commands)
573+
? commands.filter((cmd) => typeof cmd === "string" && cmd.trim().length > 0)
574+
: []
575+
576+
await updateGlobalState("allowedCommands", validCommands)
571577

572578
// Also update workspace settings.
573579
await vscode.workspace
574580
.getConfiguration(Package.name)
575-
.update("allowedCommands", message.commands, vscode.ConfigurationTarget.Global)
581+
.update("allowedCommands", validCommands, vscode.ConfigurationTarget.Global)
576582

577583
break
584+
}
578585
case "openCustomModesSettings": {
579586
const customModesFilePath = await provider.customModesManager.getCustomModesFilePath()
580587

0 commit comments

Comments
 (0)