安全設定 Claude Code、Codex 與 Cursor:別再按「全部允許」
更新於 · Bologu 團隊
Claude Code、Codex 與 Cursor 每次執行指令前都會詢問,久了令人厭煩,於是很多人直接按「全部允許」或開啟完全不詢問的模式,還把那個設定檔提交進 repo。 結果 AI 能在你的電腦上執行任何指令,包括它從網頁、套件的 README,或陌生人寫的 issue 裡讀到的指令。本文整理哪些設定很危險,以及應該改用什麼。
為什麼要在意
- 提示詞注入:藏在網頁、檔案或 issue 中的文字可以命令 agent 執行指令。如果 agent 不必先問你,指令就會立刻執行。
- 你的電腦裡有重要的東西:
.env檔、雲端金鑰、GitHub token 與瀏覽器登入狀態,全都和 agent 在同一台電腦上。 - 惡意程式專找 AI 的設定:今年的 npm 惡意程式就藏在 Claude 與 VS Code 的資料夾裡。讓 agent 為所欲為只會讓門開得更大。
- repo 裡的檔案影響所有人:提交進去的
.claude/settings.json或.codex/config.toml會套用到每個 clone 的人,包括 CI。
危險的設定
Claude Code "defaultMode": "bypassPermissions" --dangerously-skip-permissions
"allow": ["Bash"] or "Bash(*)"
Codex approval_policy = "never" sandbox_mode = "danger-full-access"
--dangerously-bypass-approvals-and-sandbox --yolo
Cursor Run Mode: "Run Everything"
MCP "command": "npx", "args": ["some-mcp-server"] ← no version pinned這些全都代表「任何指令都不必詢問就執行」。只能在用完即丟、沒有真實金鑰與重要資料的電腦或容器中使用。
較安全的設定
Claude Code
只允許專案真正用到的指令、禁止讀取機密檔案,並關閉略過權限詢問的模式。Claude Code 會先檢查 deny 規則,再檢查 ask 與 allow,因此 deny 規則一定勝過 allow 規則。
// .claude/settings.json
{
"permissions": {
"defaultMode": "default",
"allow": ["Bash(npm run test *)", "Bash(npm run lint)", "Bash(git status)", "Bash(git diff *)"],
"deny": ["Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"],
"disableBypassPermissionsMode": "disable"
}
}Codex
需要時才詢問,且只能寫入專案資料夾。workspace-write 模式預設關閉網路存取,非必要不要開啟。
# .codex/config.toml
approval_policy = "on-request"
sandbox_mode = "workspace-write"
[sandbox_workspace_write]
network_access = falseCursor
到 Settings → Agents → Approvals & Execution,選擇 Allowlist 或 Auto-review,不要選 Run Everything。若使用 permissions.json,只列出需要的指令:
// ~/.cursor/permissions.json
{
"terminalAllowlist": ["npm run test", "npm run lint", "git status", "git diff"],
"mcpAllowlist": ["github:search_code"]
}不要只靠禁止清單:逐一禁止 curl 或 rm 總有辦法繞過。請改用「只允許已知的指令」的思維。
CI 與 GitHub Actions 中的 agent
- 不要讓 workflow 中的 agent 使用略過權限的設定,只給它該工作需要的工具。
- 把 workflow 的
permissions:設到最低(例如contents: read),不要傳入該步驟用不到的 secret。 - 絕不要在
pull_request_target上 checkout PR 的程式碼並執行 agent:任何人都能開一個藏有指令的 PR。
MCP server
- 鎖定確切版本,例如
[email protected],不要用latest或不寫版本。套件遭劫持時,新版本就不會被自動拉進來。 - 透過環境變數傳入 token,絕不要把真實值寫進
.mcp.json。 - 只安裝實際使用、且知道由誰維護的 server。
給 AI 的修正提示詞
Review the AI agent settings in this repo: .claude/settings.json, .claude/settings.local.json,
.codex/config.toml, .cursor/, .vscode/settings.json, .mcp.json and every workflow in .github/workflows.
1. List every place that lets an agent run commands or edit files without asking
(bypassPermissions, a bare "Bash" or "Bash(*)", approval_policy "never", danger-full-access,
--dangerously-skip-permissions, --yolo, autoApprove, "Run Everything").
2. Replace each with a short allowlist of the exact commands this project needs (test, lint, build).
3. Add deny rules so agents cannot read .env files or secrets.
4. Pin every MCP server to an exact version.
Show me the diff before changing anything. Do not print any secret values.讓 Bologu 幫你檢查
GitHub 程式碼檢測會讀取提交進 repo 的 AI 設定檔與 .github/workflows 中的 workflow, 在發現讓 agent 不經詢問就執行指令的設定、CI 中不受限制的 agent,以及未鎖定版本的 MCP server 時提出警告。 只存在你電腦上的檔案(例如 ~/.claude/settings.json)Bologu 看不到,請依照本文自行檢查。
