|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "sort" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/BackendStack21/odek/internal/config" |
| 16 | + "github.com/BackendStack21/odek/internal/mcpclient" |
| 17 | + "golang.org/x/term" |
| 18 | +) |
| 19 | + |
| 20 | +// mcpApprovalsFile is the persistent store for user-approved project-level MCP |
| 21 | +// servers. It lives next to config.json under ~/.odek and is created 0600. |
| 22 | +const mcpApprovalsFile = "mcp_approvals.json" |
| 23 | + |
| 24 | +// mcpApprovalEnv returns true if the user has opted in globally via the |
| 25 | +// ODEK_APPROVE_MCP environment variable. |
| 26 | +func mcpApprovalEnv() bool { |
| 27 | + return os.Getenv("ODEK_APPROVE_MCP") == "1" |
| 28 | +} |
| 29 | + |
| 30 | +// approveMCPServers requires explicit user approval for any MCP servers that |
| 31 | +// were introduced by the project-level ./odek.json config. Global servers from |
| 32 | +// ~/.odek/config.json are considered operator-trusted and do not require |
| 33 | +// approval. |
| 34 | +// |
| 35 | +// Approval can be granted in three ways: |
| 36 | +// 1. Set ODEK_APPROVE_MCP=1 (useful for CI/non-interactive use). |
| 37 | +// 2. Answer the interactive y/N prompt when running on a TTY. |
| 38 | +// 3. A prior approval for the same project/server/command/args fingerprint is |
| 39 | +// persisted in ~/.odek/mcp_approvals.json. |
| 40 | +// |
| 41 | +// If approval is required and cannot be obtained, approveMCPServers returns an |
| 42 | +// error and the command should abort before spawning any MCP subprocess. |
| 43 | +func approveMCPServers(resolved config.ResolvedConfig, stdin io.Reader, stdout io.Writer) error { |
| 44 | + isTTY := stdin == os.Stdin && term.IsTerminal(int(os.Stdin.Fd())) |
| 45 | + return approveMCPServersWithTTY(resolved, stdin, stdout, isTTY) |
| 46 | +} |
| 47 | + |
| 48 | +// approveMCPServersWithTTY is the testable core of approveMCPServers. The tty |
| 49 | +// argument tells the function whether it may prompt interactively. |
| 50 | +func approveMCPServersWithTTY(resolved config.ResolvedConfig, stdin io.Reader, stdout io.Writer, tty bool) error { |
| 51 | + if len(resolved.ProjectMCPServerNames) == 0 { |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + if mcpApprovalEnv() { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + projectDir, err := os.Getwd() |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("mcp approval: get working directory: %w", err) |
| 62 | + } |
| 63 | + projectDir, err = filepath.Abs(projectDir) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("mcp approval: abs working directory: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + approved, err := loadMCPApprovals() |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("mcp approval: load approvals: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + reader := bufio.NewReader(stdin) |
| 74 | + |
| 75 | + for _, name := range resolved.ProjectMCPServerNames { |
| 76 | + cfg, ok := resolved.MCPServers[name] |
| 77 | + if !ok { |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + key := mcpApprovalKey(projectDir, name, cfg) |
| 82 | + if approved[key] { |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + if !tty { |
| 87 | + return fmt.Errorf( |
| 88 | + "project-level MCP server %q (%s %q) requires explicit approval\n"+ |
| 89 | + "set ODEK_APPROVE_MCP=1 to approve all project MCP servers, or run interactively", |
| 90 | + name, cfg.Command, strings.Join(cfg.Args, " "), |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + fmt.Fprintf(stdout, "\nProject-level MCP server %q wants to run:\n", name) |
| 95 | + fmt.Fprintf(stdout, " command: %s\n", cfg.Command) |
| 96 | + if len(cfg.Args) > 0 { |
| 97 | + fmt.Fprintf(stdout, " args: %s\n", strings.Join(cfg.Args, " ")) |
| 98 | + } |
| 99 | + if len(cfg.Env) > 0 { |
| 100 | + envKeys := make([]string, 0, len(cfg.Env)) |
| 101 | + for k := range cfg.Env { |
| 102 | + envKeys = append(envKeys, k) |
| 103 | + } |
| 104 | + sort.Strings(envKeys) |
| 105 | + fmt.Fprintf(stdout, " env: %s\n", strings.Join(envKeys, ", ")) |
| 106 | + } |
| 107 | + fmt.Fprintf(stdout, "Approve? [y/N] ") |
| 108 | + |
| 109 | + line, err := reader.ReadString('\n') |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("mcp approval: read prompt: %w", err) |
| 112 | + } |
| 113 | + line = strings.ToLower(strings.TrimSpace(line)) |
| 114 | + if line != "y" && line != "yes" { |
| 115 | + return fmt.Errorf("mcp approval: server %q was not approved", name) |
| 116 | + } |
| 117 | + |
| 118 | + approved[key] = true |
| 119 | + if err := saveMCPApprovals(approved); err != nil { |
| 120 | + return fmt.Errorf("mcp approval: save approvals: %w", err) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +// mcpApprovalKey returns a stable key for the persisted approval store. It |
| 128 | +// includes the project directory, server name, command, and arguments so a |
| 129 | +// change to any of those invalidates the prior approval. |
| 130 | +func mcpApprovalKey(projectDir, name string, cfg mcpclient.ServerConfig) string { |
| 131 | + h := sha256.New() |
| 132 | + fmt.Fprintf(h, "%s\x00%s\x00%s", projectDir, name, cfg.Command) |
| 133 | + for _, a := range cfg.Args { |
| 134 | + fmt.Fprintf(h, "\x00%s", a) |
| 135 | + } |
| 136 | + return hex.EncodeToString(h.Sum(nil)) |
| 137 | +} |
| 138 | + |
| 139 | +// loadMCPApprovals reads the persisted approval map. A missing file is treated |
| 140 | +// as an empty approval set. |
| 141 | +func loadMCPApprovals() (map[string]bool, error) { |
| 142 | + path := filepath.Join(expandHome("~/.odek"), mcpApprovalsFile) |
| 143 | + data, err := os.ReadFile(path) |
| 144 | + if err != nil { |
| 145 | + if os.IsNotExist(err) { |
| 146 | + return make(map[string]bool), nil |
| 147 | + } |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + |
| 151 | + var approvals map[string]bool |
| 152 | + if err := json.Unmarshal(data, &approvals); err != nil { |
| 153 | + return nil, fmt.Errorf("parse %s: %w", path, err) |
| 154 | + } |
| 155 | + if approvals == nil { |
| 156 | + approvals = make(map[string]bool) |
| 157 | + } |
| 158 | + return approvals, nil |
| 159 | +} |
| 160 | + |
| 161 | +// saveMCPApprovals writes the approval map to disk with 0600 permissions. |
| 162 | +func saveMCPApprovals(approvals map[string]bool) error { |
| 163 | + dir := expandHome("~/.odek") |
| 164 | + if err := os.MkdirAll(dir, 0700); err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + path := filepath.Join(dir, mcpApprovalsFile) |
| 168 | + data, err := json.MarshalIndent(approvals, "", " ") |
| 169 | + if err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + return os.WriteFile(path, data, 0600) |
| 173 | +} |
0 commit comments