fix: 禁用 stdio 传输以防止命令注入风险

This commit is contained in:
wizardchen
2026-01-15 14:49:19 +08:00
parent 48a0f9b508
commit 57d6fea8bc
4 changed files with 29 additions and 368 deletions
+6 -33
View File
@@ -31,20 +31,9 @@ func NewMCPServiceService(
// CreateMCPService creates a new MCP service
func (s *mcpServiceService) CreateMCPService(ctx context.Context, service *types.MCPService) error {
// Security validation for stdio transport type
// Stdio transport is disabled for security reasons
if service.TransportType == types.MCPTransportStdio {
if service.StdioConfig == nil {
return fmt.Errorf("stdio_config is required for stdio transport")
}
// Validate stdio configuration to prevent command injection (CWE-78)
if err := secutils.ValidateStdioConfig(
service.StdioConfig.Command,
service.StdioConfig.Args,
service.EnvVars,
); err != nil {
logger.GetLogger(ctx).Warnf("MCP service creation blocked due to security validation: %v", err)
return fmt.Errorf("security validation failed: %w", err)
}
return fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
}
// Set default advanced config if not provided
@@ -129,31 +118,15 @@ func (s *mcpServiceService) UpdateMCPService(ctx context.Context, service *types
return fmt.Errorf("MCP service not found")
}
// Security validation for stdio transport type when updating stdio config
// Determine the final transport type and stdio config after merge
// Determine the final transport type after merge
finalTransportType := existing.TransportType
if service.TransportType != "" {
finalTransportType = service.TransportType
}
finalStdioConfig := existing.StdioConfig
if service.StdioConfig != nil {
finalStdioConfig = service.StdioConfig
}
finalEnvVars := existing.EnvVars
if service.EnvVars != nil {
finalEnvVars = service.EnvVars
}
// Validate if the final configuration uses stdio transport
if finalTransportType == types.MCPTransportStdio && finalStdioConfig != nil {
if err := secutils.ValidateStdioConfig(
finalStdioConfig.Command,
finalStdioConfig.Args,
finalEnvVars,
); err != nil {
logger.GetLogger(ctx).Warnf("MCP service update blocked due to security validation: %v", err)
return fmt.Errorf("security validation failed: %w", err)
}
// Stdio transport is disabled for security reasons
if finalTransportType == types.MCPTransportStdio {
return fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
}
// Store old enabled state BEFORE any updates
+2 -31
View File
@@ -9,7 +9,6 @@ import (
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
secutils "github.com/Tencent/WeKnora/internal/utils"
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/client/transport"
"github.com/mark3labs/mcp-go/mcp"
@@ -119,36 +118,8 @@ func NewMCPClient(config *ClientConfig) (MCPClient, error) {
return nil, fmt.Errorf("failed to create HTTP streamable client: %w", err)
}
case types.MCPTransportStdio:
if config.Service.StdioConfig == nil {
return nil, fmt.Errorf("stdio_config is required for stdio transport")
}
// Security validation: validate command, args, and env vars before execution
// This prevents command injection attacks (CWE-78)
if err := secutils.ValidateStdioConfig(
config.Service.StdioConfig.Command,
config.Service.StdioConfig.Args,
config.Service.EnvVars,
); err != nil {
return nil, fmt.Errorf("stdio configuration validation failed: %w", err)
}
// Convert env vars map to []string format (KEY=value)
envVars := make([]string, 0, len(config.Service.EnvVars))
for key, value := range config.Service.EnvVars {
envVars = append(envVars, fmt.Sprintf("%s=%s", key, value))
}
// Create stdio client with options
// NewStdioMCPClientWithOptions(command string, env []string, args []string, opts ...transport.StdioOption)
mcpClient, err = client.NewStdioMCPClientWithOptions(
config.Service.StdioConfig.Command,
envVars,
config.Service.StdioConfig.Args,
)
if err != nil {
return nil, fmt.Errorf("failed to create stdio client: %w", err)
}
// Stdio transport is disabled for security reasons (potential command injection vulnerabilities)
return nil, fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
default:
return nil, ErrUnsupportedTransport
}
+4 -30
View File
@@ -35,17 +35,17 @@ func NewMCPManager() *MCPManager {
}
// GetOrCreateClient gets an existing client or creates a new one
// For stdio transport, always creates a new client (not cached)
// For SSE/HTTP Streamable, caches and reuses existing connections
// Caches and reuses existing connections for SSE/HTTP Streamable
// Note: Stdio transport is disabled for security reasons
func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, error) {
// Check if service is enabled
if !service.Enabled {
return nil, fmt.Errorf("MCP service %s is not enabled", service.Name)
}
// For stdio transport, always create a new client (don't cache)
// Stdio transport is disabled for security reasons
if service.TransportType == types.MCPTransportStdio {
return m.createStdioClient(service)
return nil, fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
}
// For SSE/HTTP Streamable, check if client already exists and reuse
@@ -95,32 +95,6 @@ func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, er
return client, nil
}
// createStdioClient creates a new stdio client (not cached)
func (m *MCPManager) createStdioClient(service *types.MCPService) (MCPClient, error) {
// Create new client
config := &ClientConfig{
Service: service,
}
client, err := NewMCPClient(config)
if err != nil {
return nil, fmt.Errorf("failed to create stdio MCP client: %w", err)
}
// For stdio, Connect() starts the subprocess
// Use manager's context for the connection lifecycle
if err := client.Connect(m.ctx); err != nil {
return nil, fmt.Errorf("failed to connect to stdio MCP service: %w", err)
}
if err := m.initializeClient(service, client, "failed to initialize stdio MCP client"); err != nil {
return nil, err
}
logger.GetLogger(m.ctx).Infof("MCP stdio client created and initialized for service: %s", service.Name)
return client, nil
}
// initializeClient handles the shared initialization flow with timeout enforcement.
func (m *MCPManager) initializeClient(service *types.MCPService, client MCPClient, errPrefix string) error {
initTimeout := 30 * time.Second