settings.json 中的权限声明
High Contrast
Dark Mode
Light Mode
Sepia
Forest
2 min read486 words

settings.json 中的权限声明

Skills 文件控制 Claude 应该做什么,而 settings.json 控制 Claude 被允许做什么。这两层配置共同构成了 Skills 的安全边界。


settings.json 的位置和作用

Claude Code 在以下位置查找配置文件(优先级从高到低):

项目级:.claude/settings.json    (仅对当前项目生效)
用户级:~/.claude/settings.json  (对当前用户所有项目生效)

settings.json 的核心功能: - 声明哪些工具操作默认允许(无需用户逐次确认) - 声明哪些操作明确禁止 - 配置 Skills 相关的全局行为


权限声明的基本结构

{
"permissions": {
"allow": [
"Bash(git log:*)",
"Bash(npm test:*)",
"Bash(npm run lint:*)",
"Read(*)",
"Glob(*)"
],
"deny": [
"Bash(git push:*)",
"Bash(rm -rf:*)",
"Write(/etc/*)"
]
}
}

allow 和 deny 的规则语法

工具级别的声明

"allow": [
"Read(*)",        // 允许读取任何文件
"Glob(*)",        // 允许所有 Glob 匹配
"Bash(*)",        // 允许所有 Bash 命令(谨慎使用)
"WebFetch(*)"     // 允许所有 WebFetch 请求
]

命令级别的声明(推荐)

"allow": [
"Bash(git *:*)",          // 允许所有 git 子命令
"Bash(npm test:*)",       // 只允许 npm test
"Bash(npm run *:*)",      // 允许所有 npm run 脚本
"Bash(docker ps:*)",      // 允许 docker ps(查看容器)
"Read(src/**)",           // 允许读取 src 目录下的所有文件
"Write(.claude/reports/*)" // 只允许写入报告目录
]

deny 覆盖 allow

{
"permissions": {
"allow": [
"Bash(git *:*)"    // 允许所有 git 命令
],
"deny": [
"Bash(git push:*)",      // 但禁止 git push
"Bash(git force-push:*)" // 和 git force push
]
}
}

规则deny 的优先级高于 allow。如果一个操作同时被 allow 和 deny 匹配,则拒绝。


为不同类型的 Skills 配置权限

只读分析类 Skills(最安全)

适合代码审查、日志分析、依赖检查等不需要修改任何文件的 Skill:

{
"permissions": {
"allow": [
"Read(*)",
"Glob(*)",
"Grep(*)",
"Bash(git log:*)",
"Bash(git diff:*)",
"Bash(git status:*)",
"Bash(npm audit:*)",
"Bash(npm outdated:*)"
],
"deny": [
"Write(*)",
"Edit(*)",
"Bash(git commit:*)",
"Bash(git push:*)",
"Bash(git checkout:*)",
"Bash(rm:*)"
]
}
}

文档生成类 Skills

允许写入特定目录,但限制范围:

{
"permissions": {
"allow": [
"Read(*)",
"Glob(*)",
"Grep(*)",
"Bash(git log:*)",
"Bash(git diff:*)",
"Write(docs/**)",
"Write(CHANGELOG.md)",
"Write(.claude/reports/**)"
],
"deny": [
"Bash(git commit:*)",
"Bash(git push:*)",
"Write(src/**)"
]
}
}

运维操作类 Skills(最需要谨慎)

{
"permissions": {
"allow": [
"Read(*)",
"Glob(*)",
"Bash(git log:*)",
"Bash(git status:*)",
"Bash(git diff:*)",
"Bash(npm test:*)",
"Bash(npm run lint:*)",
"Bash(docker ps:*)",
"Bash(docker logs:*)"
],
"deny": [
"Bash(git push:*)",
"Bash(docker rm:*)",
"Bash(docker stop:*)",
"Bash(rm -rf:*)",
"Write(/.env*)"
]
}
}

Skill 级别的工具限制

除了全局 settings.json,每个 Skill 文件也可以在 frontmatter 中声明它所需的工具(allowed-tools)。两层控制叠加生效:

settings.json (全局允许/拒绝)
+
Skill frontmatter (Skill 声明它需要的工具)
=
最终允许的操作

示例

settings.json 配置:allow Bash(git *:*), deny Bash(git push:*)
Skill A(代码审查)frontmatter:
allowed-tools: Bash, Read, Grep
→ 可以用 Bash,但仍受 settings.json 的 deny 限制
→ git push 仍然被拒绝
Skill B(文档生成)frontmatter:
allowed-tools: Read, Write
→ 不声明 Bash,Claude 不会调用 Bash 工具
→ 即使 settings.json 允许了 Bash,Skill B 也不会用

权限配置的最佳实践

最小权限原则

// ❌ 过于宽松
{
"permissions": {
"allow": ["Bash(*)", "Write(*)", "Read(*)"]
}
}
// ✅ 最小权限:只允许 Skills 实际需要的操作
{
"permissions": {
"allow": [
"Read(*)",
"Glob(*)",
"Grep(*)",
"Bash(git log:*)",
"Bash(git diff:*)",
"Bash(git status:*)",
"Bash(npm test:*)",
"Bash(npm run lint:*)"
],
"deny": [
"Bash(git commit:*)",
"Bash(git push:*)",
"Bash(rm:*)",
"Write(*)"
]
}
}

分项目配置

不同项目应该有不同的权限配置:

# API 后端项目(允许数据库操作相关命令)
.claude/settings.json
→ allow: Bash(psql:*), Bash(redis-cli info:*)
→ deny: Bash(psql -c "DROP:*")
# 前端项目(只需要 npm 和 git 命令)
.claude/settings.json
→ allow: Bash(npm *:*), Bash(git *:*)
→ deny: Bash(git push:*)

本节记录清单


下一节CLAUDE.md 对 Skill 行为的全局约束——settings.json 控制工具权限,而 CLAUDE.md 控制 Claude 的行为准则。如何用 CLAUDE.md 为所有 Skills 设置统一的工作规范?