Skip to content

权限系统

Permission System

Claude Code 的权限系统控制 AI 可以执行的操作范围,确保安全性和可控性。理解权限机制是安全使用 Claude Code 的基础。


权限规则优先级

Permission Rule Priority

Claude Code 的权限遵循一个简单但重要的原则:

deny > ask > allow

优先级解释

优先级行为说明
deny (最高)直接拒绝无论其他规则如何,deny 的操作永远被阻止
ask (中间)请求确认未明确 allow 或 deny 的操作,需要用户确认
allow (最低)自动允许只有在没有 deny 规则匹配时才生效

规则评估流程

用户请求执行操作


  匹配 deny 规则? ─── 是 ──→ 拒绝执行




  匹配 allow 规则? ─── 是 ──→ 允许执行




    请求用户确认 (ask)

       ├─ y (单次允许)
       ├─ n (单次拒绝)
       └─ always (永久允许,写入配置)

示例

json
{
  "permissions": {
    "allow": [
      "Bash(git *)"
    ],
    "deny": [
      "Bash(git push --force *)"
    ]
  }
}

在这个配置中:

  • git status → 允许(匹配 allow 的 git *
  • git push --force main → 拒绝(匹配 deny,deny 优先级更高)
  • git push origin main → 允许(匹配 allow 的 git *,未匹配 deny)

settings.json 中的权限配置

Permission Configuration in settings.json

权限在 settings.json 的 permissions 字段中配置。

基本结构

json
{
  "permissions": {
    "allow": [
      "工具名(参数模式)",
      "工具名(参数模式)"
    ],
    "deny": [
      "工具名(参数模式)",
      "工具名(参数模式)"
    ]
  }
}

配置层级合并

权限配置遵循 settings.json 的三层层级结构,所有层级的规则会被合并:

json
// ~/.claude/settings.json (用户级)
{
  "permissions": {
    "allow": ["Read", "Glob", "Grep"],
    "deny": ["Bash(rm -rf /*)"]
  }
}

// .claude/settings.json (项目级)
{
  "permissions": {
    "allow": ["Bash(pnpm *)"],
    "deny": ["Bash(git push --force *)"]
  }
}

// .claude/settings.local.json (本地级)
{
  "permissions": {
    "allow": ["Bash(kubectl *)"]
  }
}

// 最终生效的权限:
// allow: Read, Glob, Grep, Bash(pnpm *), Bash(kubectl *)
// deny: Bash(rm -rf /*), Bash(git push --force *)

工具级权限控制

Tool-Level Permission Control

Claude Code 中每个工具都可以单独配置权限。

可用工具列表

工具名功能默认权限
Read读取文件内容ask
Write创建新文件ask
Edit编辑现有文件ask
Glob文件模式匹配搜索ask
Grep文件内容搜索ask
Bash执行 Shell 命令ask
WebFetch获取网页内容ask
WebSearch网络搜索ask
NotebookEdit编辑 Jupyter Notebookask

工具权限语法

工具名                    - 允许/拒绝该工具的所有操作
工具名(模式)              - 允许/拒绝匹配模式的操作
工具名(模式1, 模式2)      - 多个模式

Bash 工具的精细控制

Bash 工具是最需要精细控制的工具,因为它可以执行任意 Shell 命令:

json
{
  "permissions": {
    "allow": [
      "Bash(git status)",
      "Bash(git diff *)",
      "Bash(git log *)",
      "Bash(git add *)",
      "Bash(git commit *)",
      "Bash(git push origin *)",
      "Bash(npm test)",
      "Bash(npm run *)",
      "Bash(npx *)",
      "Bash(node *)",
      "Bash(ls *)",
      "Bash(cat *)",
      "Bash(find *)",
      "Bash(mkdir *)",
      "Bash(cp *)",
      "Bash(mv *)"
    ],
    "deny": [
      "Bash(rm -rf *)",
      "Bash(sudo *)",
      "Bash(chmod 777 *)",
      "Bash(curl * | bash)",
      "Bash(wget * | bash)",
      "Bash(git push --force *)",
      "Bash(git reset --hard *)",
      "Bash(> /dev/sda*)",
      "Bash(mkfs *)",
      "Bash(dd *)",
      "Bash(:(){ :|:& };:)"
    ]
  }
}

读写工具的控制

json
{
  "permissions": {
    "allow": [
      "Read",
      "Glob",
      "Grep"
    ],
    "deny": [
      "Write",
      "Edit"
    ]
  }
}

此配置下,Claude 可以读取和搜索文件,但不能创建或修改文件。适用于只需要代码审查或分析的场景。

通配符模式说明

模式匹配不匹配
Bash(git *)git status, git push origin maingitk, github-cli
Bash(npm *)npm test, npm run buildnpx, node
Bash(* --force *)git push --force origin, rm --force filegit push origin
Read读取任何文件-

--dangerously-skip-permissions 标志

The --dangerously-skip-permissions Flag

此标志跳过所有权限检查,Claude 可以执行任何操作而不需要确认。

重要警告

⚠️  此标志仅应用于以下场景:
    - CI/CD 自动化流水线
    - 受控的沙盒环境
    - Docker 容器内的自动化任务

    绝不应在交互式开发中使用!

使用场景

bash
# CI/CD 自动代码审查
claude -p "审查最近的 PR" \
  --dangerously-skip-permissions \
  --model claude-sonnet-4-20250514

# 自动化测试生成
claude -p "为 src/auth.ts 生成单元测试" \
  --dangerously-skip-permissions

# Docker 容器中的自动化
docker run --rm \
  -e ANTHROPIC_API_KEY=$API_KEY \
  -v $(pwd):/workspace \
  claude-code \
  claude -p "分析代码质量" \
    --dangerously-skip-permissions

安全防护措施

即使使用了 --dangerously-skip-permissions,以下保护机制仍然生效:

保护措施说明
网络隔离建议在隔离的网络环境中运行
文件系统限制建议挂载只读卷或限制目录
时间限制使用 CLAUDE_CODE_MAX_TURNS 限制执行轮次
日志记录所有操作都会被记录

allowedTools 和 deniedTools 配置

Configuring allowedTools and deniedTools

完整的权限配置结构

json
{
  "permissions": {
    "allow": [
      "Read",
      "Glob",
      "Grep",
      "Bash(git *)",
      "Bash(npm test)",
      "Bash(npm run lint)"
    ],
    "deny": [
      "Bash(rm -rf *)",
      "Bash(sudo *)"
    ]
  }
}

按场景配置

只读分析模式

json
{
  "permissions": {
    "allow": [
      "Read",
      "Glob",
      "Grep",
      "Bash(git log *)",
      "Bash(git diff *)",
      "Bash(git status)",
      "Bash(wc *)",
      "Bash(head *)",
      "Bash(tail *)"
    ],
    "deny": [
      "Write",
      "Edit",
      "Bash(git add *)",
      "Bash(git commit *)",
      "Bash(git push *)"
    ]
  }
}

完全自动化模式

json
{
  "permissions": {
    "allow": [
      "Read",
      "Write",
      "Edit",
      "Glob",
      "Grep",
      "Bash(git *)",
      "Bash(npm *)",
      "Bash(pnpm *)",
      "Bash(node *)",
      "Bash(npx *)"
    ],
    "deny": [
      "Bash(rm -rf /)",
      "Bash(sudo *)",
      "Bash(git push --force *)"
    ]
  }
}

前端开发模式

json
{
  "permissions": {
    "allow": [
      "Read",
      "Write",
      "Edit",
      "Glob",
      "Grep",
      "Bash(npm *)",
      "Bash(pnpm *)",
      "Bash(npx *)",
      "Bash(node *)",
      "Bash(git *)",
      "Bash(tsc *)",
      "Bash(eslint *)",
      "Bash(prettier *)"
    ],
    "deny": [
      "Bash(rm -rf *)",
      "Bash(sudo *)"
    ]
  }
}

权限提示交互

Permission Prompt Interaction

当 Claude 尝试执行未明确允许的操作时,会出现权限提示。

交互选项

bash
Claude wants to run: npm install express
Allow? (y/n/always)

  y       - 本次允许,不记住
  n       - 本次拒绝
  always  - 永久允许此类操作(写入 settings.json)

选项详解

选项行为持久化
y仅本次对话中允许此操作
n拒绝此操作,Claude 会寻找替代方案
always将此操作添加到 allow 列表是(写入用户级配置)

always 选项的工作方式

当你选择 always 时,Claude Code 会自动更新 ~/.claude/settings.json

json
// 选择 always 前
{
  "permissions": {
    "allow": ["Read", "Glob"]
  }
}

// 选择 always 后(自动添加)
{
  "permissions": {
    "allow": ["Read", "Glob", "Bash(npm install *)"]
  }
}

批量确认技巧

bash
# 如果你信任当前操作序列,可以提前配置
# 避免频繁确认中断工作流

# 在对话开始前设置
> /permissions
# 预先批量添加需要的权限

安全最佳实践

Security Best Practices

最小权限原则

只授予 Claude 完成任务所需的最小权限:

json
// 好的做法 - 精确的权限
{
  "permissions": {
    "allow": [
      "Bash(git status)",
      "Bash(git diff)",
      "Bash(npm test)"
    ]
  }
}

// 不推荐 - 过于宽泛
{
  "permissions": {
    "allow": [
      "Bash(*)"
    ]
  }
}

必须 deny 的危险命令

json
{
  "permissions": {
    "deny": [
      "Bash(rm -rf /)",
      "Bash(rm -rf /*)",
      "Bash(sudo *)",
      "Bash(chmod 777 *)",
      "Bash(git push --force *)",
      "Bash(git reset --hard *)",
      "Bash(curl * | bash)",
      "Bash(curl * | sh)",
      "Bash(wget * -O - | bash)",
      "Bash(eval *)",
      "Bash(> /dev/sd*)",
      "Bash(dd if=* of=/dev/*)",
      "Bash(mkfs.*)",
      "Bash(format *)",
      "Bash(DROP DATABASE *)",
      "Bash(DROP TABLE *)",
      "Bash(TRUNCATE *)"
    ]
  }
}

敏感文件保护

json
{
  "permissions": {
    "deny": [
      "Read(.env)",
      "Read(.env.*)",
      "Read(**/credentials*)",
      "Read(**/*secret*)",
      "Read(**/*.pem)",
      "Read(**/*.key)",
      "Write(.env)",
      "Write(.env.*)",
      "Edit(.env)",
      "Edit(.env.*)"
    ]
  }
}

安全检查清单

检查项建议
是否限制了 rm 命令deny rm -rf *
是否限制了 sudodeny sudo *
是否限制了 force pushdeny git push --force *
是否保护了敏感文件deny 对 .env 等文件的访问
是否限制了网络下载执行deny curl/wget * | bash
是否限制了破坏性 Git 操作deny git reset --hard
是否限制了数据库删除deny DROP/TRUNCATE
CI/CD 是否使用隔离环境Docker 容器或沙盒

常见权限配置示例

Common Permission Configuration Examples

示例 1:保守的个人开发配置

json
{
  "permissions": {
    "allow": [
      "Read",
      "Glob",
      "Grep",
      "Bash(git status)",
      "Bash(git diff *)",
      "Bash(git log *)"
    ],
    "deny": [
      "Bash(rm *)",
      "Bash(sudo *)",
      "Bash(git push *)"
    ]
  }
}

Claude 可以读取和搜索代码,查看 Git 状态,但不能修改文件、删除文件或推送代码。

示例 2:Node.js 全栈开发配置

json
{
  "permissions": {
    "allow": [
      "Read",
      "Write",
      "Edit",
      "Glob",
      "Grep",
      "Bash(git *)",
      "Bash(npm *)",
      "Bash(npx *)",
      "Bash(node *)",
      "Bash(pnpm *)",
      "Bash(tsc *)",
      "Bash(eslint *)",
      "Bash(prettier *)",
      "Bash(jest *)",
      "Bash(vitest *)",
      "Bash(ls *)",
      "Bash(cat *)",
      "Bash(find *)",
      "Bash(mkdir *)",
      "Bash(cp *)",
      "Bash(mv *)",
      "Bash(echo *)",
      "Bash(which *)",
      "Bash(pwd)"
    ],
    "deny": [
      "Bash(rm -rf /)",
      "Bash(rm -rf /*)",
      "Bash(sudo *)",
      "Bash(git push --force *)",
      "Bash(git reset --hard *)",
      "Bash(curl * | bash)",
      "Bash(wget * | bash)"
    ]
  }
}

示例 3:Python 数据科学配置

json
{
  "permissions": {
    "allow": [
      "Read",
      "Write",
      "Edit",
      "Glob",
      "Grep",
      "NotebookEdit",
      "Bash(python *)",
      "Bash(python3 *)",
      "Bash(pip *)",
      "Bash(pip3 *)",
      "Bash(conda *)",
      "Bash(jupyter *)",
      "Bash(pytest *)",
      "Bash(git *)",
      "Bash(ls *)",
      "Bash(cat *)",
      "Bash(head *)",
      "Bash(tail *)",
      "Bash(wc *)"
    ],
    "deny": [
      "Bash(rm -rf *)",
      "Bash(sudo *)",
      "Bash(pip install --break-system-packages *)"
    ]
  }
}

示例 4:CI/CD 代码审查配置

json
{
  "permissions": {
    "allow": [
      "Read",
      "Glob",
      "Grep",
      "Bash(git log *)",
      "Bash(git diff *)",
      "Bash(git show *)",
      "Bash(npm test)",
      "Bash(npm run lint)",
      "Bash(npm run typecheck)"
    ],
    "deny": [
      "Write",
      "Edit",
      "Bash(git add *)",
      "Bash(git commit *)",
      "Bash(git push *)",
      "Bash(git checkout *)",
      "Bash(git merge *)",
      "Bash(git rebase *)",
      "Bash(rm *)",
      "Bash(sudo *)"
    ]
  }
}

权限调试

Permission Debugging

查看当前权限

bash
# 使用 /permissions 命令
> /permissions

当前权限配置:
  Allow (15 条规则):
    - Read
    - Write
    - Edit
    - Glob
    - Grep
    - Bash(git *)
    ...

  Deny (5 条规则):
    - Bash(rm -rf *)
    - Bash(sudo *)
    ...

  来源:
    用户级: 8 条规则
    项目级: 10 条规则
    本地级: 2 条规则

测试权限规则

bash
# 让 Claude 尝试一个操作来验证权限
> 请运行 git status

# 如果权限允许,直接执行
# 如果需要确认,会出现提示
# 如果被拒绝,会显示被拒绝的原因

常见问题

Q: 为什么我选了 always 但下次还是被询问?

always 将规则写入用户级配置。如果项目级配置有对应的 deny 规则,deny 优先级更高,仍会被拒绝。

Q: 如何撤销 always 的选择?

编辑 ~/.claude/settings.json,从 permissions.allow 数组中移除对应的条目。

Q: 通配符 * 能匹配多少内容?

* 匹配零个或多个任意字符(包括空格)。Bash(git *) 匹配 git statusgit push origin main 等所有以 git 开头的命令。


小结

Summary

权限系统是 Claude Code 安全模型的核心。关键要点:

  1. deny 优先 - deny 规则永远优先于 allow,确保安全底线
  2. 最小权限 - 只授予完成任务所需的最小权限
  3. 分层配置 - 利用三层配置实现团队和个人的灵活管理
  4. 保护敏感操作 - 危险命令、敏感文件必须在 deny 列表中
  5. CI/CD 隔离 - 自动化场景使用容器或沙盒环境