-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix: secure built-in filesystem MCP root handling #13294
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import fs from 'fs/promises' | ||
| import path from 'path' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { resolveFilesystemBaseDir } from '../config' | ||
| import { validatePath } from '../types' | ||
|
|
||
| describe('filesystem MCP security', () => { | ||
| const tempDirs: string[] = [] | ||
|
|
||
| async function createTempDir(prefix: string) { | ||
| const tempRoot = path.join(process.cwd(), '.context', 'vitest-temp') | ||
| await fs.mkdir(tempRoot, { recursive: true }) | ||
| const tempDir = await fs.mkdtemp(path.join(tempRoot, prefix)) | ||
| tempDirs.push(tempDir) | ||
| return tempDir | ||
| } | ||
|
|
||
| afterEach(async () => { | ||
| vi.restoreAllMocks() | ||
| await Promise.all(tempDirs.splice(0).map((tempDir) => fs.rm(tempDir, { recursive: true, force: true }))) | ||
| }) | ||
|
|
||
| it('prefers WORKSPACE_ROOT and falls back to args for filesystem root', () => { | ||
| expect(resolveFilesystemBaseDir(['C:/args-root'], {})).toBe('C:/args-root') | ||
| expect(resolveFilesystemBaseDir(['C:/args-root'], { WORKSPACE_ROOT: 'C:/env-root' })).toBe('C:/env-root') | ||
| expect(resolveFilesystemBaseDir([], {})).toBeUndefined() | ||
| }) | ||
EurFelux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('allows paths inside the configured root and rejects paths outside it', async () => { | ||
| const workspaceRoot = await createTempDir('filesystem-root-') | ||
| const outsideRoot = await createTempDir('filesystem-outside-') | ||
| const insideFile = path.join(workspaceRoot, 'inside.txt') | ||
| const outsideFile = path.join(outsideRoot, 'outside.txt') | ||
|
|
||
| await fs.writeFile(insideFile, 'inside') | ||
| await fs.writeFile(outsideFile, 'outside') | ||
|
|
||
| await expect(validatePath(insideFile, workspaceRoot)).resolves.toBe(insideFile) | ||
| await expect(validatePath(outsideFile, workspaceRoot)).rejects.toThrow('outside the configured workspace root') | ||
| }) | ||
|
|
||
| it('rejects symlink escapes outside the configured root', async () => { | ||
| const workspaceRoot = await createTempDir('filesystem-symlink-root-') | ||
| const outsideRoot = await createTempDir('filesystem-symlink-outside-') | ||
| const outsideFile = path.join(outsideRoot, 'secret.txt') | ||
| const symlinkPath = path.join(workspaceRoot, 'escape-link') | ||
|
|
||
| await fs.writeFile(outsideFile, 'top-secret') | ||
| await fs.symlink(outsideFile, symlinkPath) | ||
|
|
||
| await expect(validatePath(symlinkPath, workspaceRoot)).rejects.toThrow('outside the configured workspace root') | ||
| }) | ||
|
|
||
| it('rejects relative path traversal outside the configured root', async () => { | ||
| const workspaceRoot = await createTempDir('filesystem-relative-root-') | ||
| const outsideFile = path.join(path.dirname(workspaceRoot), 'outside.txt') | ||
|
|
||
| await fs.writeFile(outsideFile, 'outside') | ||
|
|
||
| await expect(validatePath('../outside.txt', workspaceRoot)).rejects.toThrow('outside the configured workspace root') | ||
| }) | ||
|
|
||
| it('rejects home expansion outside the configured root', async () => { | ||
| const workspaceRoot = await createTempDir('filesystem-home-root-') | ||
|
|
||
| await expect(validatePath('~/sensitive-file', workspaceRoot)).rejects.toThrow( | ||
| 'outside the configured workspace root' | ||
| ) | ||
| }) | ||
|
|
||
| it('falls back to process.cwd() when baseDir is omitted', async () => { | ||
| const workspaceRoot = await createTempDir('filesystem-cwd-root-') | ||
| const allowedFile = path.join(workspaceRoot, 'allowed.txt') | ||
| const outsideFile = path.join(path.dirname(workspaceRoot), 'outside.txt') | ||
|
|
||
| await fs.writeFile(allowedFile, 'allowed') | ||
| await fs.writeFile(outsideFile, 'outside') | ||
|
|
||
| vi.spyOn(process, 'cwd').mockReturnValue(workspaceRoot) | ||
|
|
||
| await expect(validatePath('allowed.txt')).resolves.toBe(allowedFile) | ||
| await expect(validatePath('../outside.txt')).rejects.toThrow('outside the configured workspace root') | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export function resolveFilesystemBaseDir(args: string[] = [], envs: Record<string, string> = {}): string | undefined { | ||
| const envWorkspaceRoot = envs.WORKSPACE_ROOT?.trim() | ||
| if (envWorkspaceRoot) { | ||
| return envWorkspaceRoot | ||
| } | ||
|
|
||
| return args.find((arg) => typeof arg === 'string' && arg.trim().length > 0)?.trim() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { BuiltinMCPServerNames, type MCPServer } from '@renderer/types' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { builtinMCPServers, initializeMCPServers, updateMCPServer } from '../mcp' | ||
|
|
||
| describe('MCP filesystem defaults', () => { | ||
| it('disables auto-approve for sensitive filesystem tools by default', () => { | ||
| const filesystemServer = builtinMCPServers.find((server) => server.name === BuiltinMCPServerNames.filesystem) | ||
|
|
||
| expect(filesystemServer?.disabledAutoApproveTools).toEqual(['write', 'edit', 'delete']) | ||
| }) | ||
|
|
||
| it('backfills manual approval defaults for existing filesystem servers', () => { | ||
| const dispatch = vi.fn() | ||
| const existingFilesystemServer: MCPServer = { | ||
| id: 'filesystem-server', | ||
| name: BuiltinMCPServerNames.filesystem, | ||
| type: 'inMemory', | ||
| args: ['/tmp/workspace'], | ||
| isActive: true | ||
| } | ||
|
|
||
| initializeMCPServers([existingFilesystemServer], dispatch) | ||
|
|
||
| expect(dispatch).toHaveBeenCalledWith( | ||
| updateMCPServer({ | ||
| ...existingFilesystemServer, | ||
| disabledAutoApproveTools: ['write', 'edit', 'delete'] | ||
| }) | ||
| ) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import { createSlice, nanoid, type PayloadAction } from '@reduxjs/toolkit' | |
| import { type BuiltinMCPServer, BuiltinMCPServerNames, type MCPConfig, type MCPServer } from '@renderer/types' | ||
|
|
||
| const logger = loggerService.withContext('Store:MCP') | ||
| const filesystemManualApprovalTools = ['write', 'edit', 'delete'] as const | ||
|
|
||
| export const initialState: MCPConfig = { | ||
| servers: [], | ||
|
|
@@ -170,7 +171,8 @@ export const builtinMCPServers: BuiltinMCPServer[] = [ | |
| id: nanoid(), | ||
| name: BuiltinMCPServerNames.filesystem, | ||
| type: 'inMemory', | ||
| args: ['/Users/username/Desktop', '/path/to/other/allowed/dir'], | ||
| args: ['/Users/username/Desktop'], | ||
| disabledAutoApproveTools: [...filesystemManualApprovalTools], | ||
| shouldConfig: true, | ||
| isActive: false, | ||
| provider: 'CherryAI', | ||
|
|
@@ -240,6 +242,16 @@ export const builtinMCPServers: BuiltinMCPServer[] = [ | |
| * @param dispatch Redux dispatch function | ||
| */ | ||
| export const initializeMCPServers = (existingServers: MCPServer[], dispatch: (action: any) => void): void => { | ||
| const existingFilesystemServer = existingServers.find((server) => server.name === BuiltinMCPServerNames.filesystem) | ||
| if (existingFilesystemServer && existingFilesystemServer.disabledAutoApproveTools === undefined) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: This backfill path appears to be unreachable in production. I couldn't find any runtime call site for , while the app reads MCP server configs directly from persisted Redux state. That means existing filesystem server entries keep the old auto-approve behavior after upgrade, so this only affects newly added servers. If that grandfathering is intentional, please document it explicitly. |
||
| dispatch( | ||
| updateMCPServer({ | ||
| ...existingFilesystemServer, | ||
| disabledAutoApproveTools: [...filesystemManualApprovalTools] | ||
| }) | ||
| ) | ||
EurFelux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Check if the existing servers already contain the built-in servers | ||
| const serverIds = new Set(existingServers.map((server) => server.name)) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.