mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 12:55:51 +00:00
* docs: 修复文档巡检发现的 4 处错误 - daemon.md: 反映实际实现状态(supervisor/worker 已实现而非 stub) - bridge-mode.md: API 操作数量从 7 修正为 9 - web-search-tool.md: 文件路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - remote-control-self-hosting.md: 补充缺失的 RCS_WS_IDLE_TIMEOUT 和 RCS_WS_KEEPALIVE_INTERVAL 配置项 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety 和 Context 文档中的代码引用和类型错误 - permission-model: 修正规则来源从"五层"到八层,优先级顺序对齐代码 - permission-model: PermissionUpdate 类型改为实际的 addRules/replaceRules 等 - permission-model: 补充 acceptEdits 和 dontAsk 两种权限模式 - permission-model: DENIAL_LIMITS 字段名对齐实际代码 - plan-mode: 工具路径从 src/tools/ 改为 packages/builtin-tools/src/tools/ - compaction: 修正 COMPACTABLE_TOOLS 和 POST_COMPACT_* 的行号 - project-memory: 修正 ENTRYPOINT_NAME 常量的行号 - system-prompt: 修正 SystemPrompt 类型定义文件路径和多个行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 introduction 文档中的错误路径和行号引用 - why-this-whitepaper.mdx: BashTool 路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - what-is-claude-code.mdx: 移除不存在的 Azure provider,改为实际的 7 种 provider - architecture-overview.mdx: State 类型行号从 204 修正为 207 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 conversation/features 文档中的错误 - streaming.mdx: queryStreamRaw → queryModelWithStreaming 函数名修正 - streaming.mdx: Azure 提供商不存在,替换为实际 7 个提供商 - debug-mode.mdx: --inspect-wait 描述错误,实际使用 BUN_INSPECT 环境变量 - buddy.mdx: 补充缺失的 companionReact.ts、CompanionCard.tsx、index.ts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复文档巡检中的源码引用错误 - feature-flags.mdx: 修正 feature() 兜底描述,实际从 bun:bundle 导入而非 cli.tsx:3 内联 - feature-flags.mdx: 修正工具 require 路径为 @claude-code-best/builtin-tools 包路径 - ant-only-world.mdx: 修正 tools.ts 中 require 路径为包路径 - ant-only-world.mdx: 修正 INTERNAL_ONLY_COMMANDS 行号 (267-295) 和数量 (24+) - skills.mdx: 修正 COMMANDS memoize 行号 258 → 299 - mcp-protocol.mdx: 修正 fetchToolsForClient LRU 缓存上限 20 → 100 - streaming.mdx: 修正流式事件引用 - file-operations.mdx: 修正工具路径引用 - search-and-navigation.mdx: 修正搜索工具引用 - shell-execution.mdx: 修正 shell 工具引用 - buddy.mdx: 补充缺失的 frontmatter 字段 - debug-mode.mdx: 修正调试模式描述 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 tools/agent 文档中的文件路径和行号引用 - 修正 TodoWriteTool、AgentTool、ToolSearchTool 等工具路径 src/tools/ → packages/builtin-tools/src/tools/ - 更新 Tool.ts、tools.ts、BashTool.tsx 中过时的行号引用 - 修正 WebSearchTool/WebFetchTool/EnterWorktreeTool/ExitWorktreeTool 路径 - 修正 AgentTool.tsx 中多行行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的文件路径和行号引用 - ultraplan.md: 更新文件行数(525/349/127) - fork-subagent.md: 路径迁移 src/tools/ → packages/builtin-tools/ - mcp-skills.md: 修正 getMcpSkillCommands 行号 547→604,client.ts 行号 117→129 - kairos.md: 修正 getBriefSection/getProactiveSection 行号 - proactive.md: 修正 getProactiveSection 行号 860→864 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正顶层文档中的路径迁移和行号引用 - auto-updater.md: config.ts 行号 1735→1737,标注未接入启动流程的函数 - external-dependencies.md: WebSearchTool/WebFetchTool 路径迁移到 builtin-tools 包,Vertex 行号修正 - lsp-integration.md: LSPTool 路径从 src/tools/ 迁移到 packages/builtin-tools/ - stub-recovery-design-1-4.md: 修正 Windows 绝对路径链接为标准代码引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 task 文档中的文件扩展名和路径引用 - task-004: AssistantSessionChooser.ts → .tsx, assistant.ts → .tsx - task-003: cli.tsx 行号 249→272, markdownConfigLoader.ts 行号 29→35 - lan-pipes: SendMessageTool 路径迁移到 packages/builtin-tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 computer-use-tools-reference 缺失的 Windows 工具 添加遗漏的 open_terminal 和 activate_window 两个 Windows 专属工具, 修正工具总数 37→39,Windows 工具数 10→12。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 audit/bash-classifier/token-budget/tree-sitter 文档 - feature-flags-audit: ScheduleCronTool 路径迁移、DAEMON 状态更新为 COMPLETE、assistant 文件标记已补全、UDS 标记已实现 - bash-classifier: BashPermissionRequest 文件路径修正、withRetry 行号移除 - token-budget: attachments.ts 行号范围修正 - tree-sitter-bash: bashPermissions.ts 路径迁移到 packages/builtin-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 langfuse-monitoring AgentTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 bridgeApi 行号和 Tool.ts 行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety/Extensibility 文档中的工具路径迁移和行号引用 - sandbox.mdx: shouldUseSandbox.ts 和 bashPermissions.ts 路径迁移至 packages/builtin-tools - why-safety-matters.mdx: bashPermissions.ts 路径迁移(3 处) - plan-mode.mdx: EnterPlanModeTool/prompt.ts 路径迁移 - auto-mode.mdx: Auto mode 指令行号 3464→3481 - hooks.mdx: AgentTool/runAgent.ts 路径迁移 - skills.mdx: SkillTool.ts 路径迁移 - custom-agents.mdx: Agent built-in 目录和 exploreAgent.ts 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 internals 文档引用计数和路径 - ant-only-world: USER_TYPE 引用计数 465→410+,工具路径迁移到 builtin-tools - growthbook-ab-testing: growthbook.ts 行数 1156→1258 - hidden-features: 语音模式状态更新(audio-napi 已恢复) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正工具文档中的行号引用 - sub-agents: AgentTool.call 入口行号 340→387 - shell-execution: ShellCommand onTimeout 行号 129→144 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的状态、路径和计数 - all-features-guide: 修正 feature flag 启用范围(dev only vs dev+build) - tier3-stubs: 大量状态修正(stub→已实现),缩减过时条目 - workflow-scripts: 路径迁移到 builtin-tools,状态更新 - web-browser-tool: 工具状态缺失→已实现,路径迁移 - context-collapse: CtxInspectTool 状态缺失→已实现 - computer-use: 行号引用更新,平台分发描述修正 - computer-use-tools-reference: 工具数 39→38 - voice-mode: voiceModeEnabled 行数 55→54 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 the-loop 查询循环行号引用 query.ts 代码变更后终止原因行号整体偏移约 40 行 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 feature-flags-audit 完整 build 默认 feature 列表 添加 ULTRATHINK/LODESTONE/ACP/DAEMON 等 19 个缺失的 build 默认 feature, 修正 dev-only 特征标注(UDS_INBOX/LAN_PIPES/BG_SESSIONS/TEMPLATES) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit ConfigTool 路径迁移 ConfigTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit BashTool 路径迁移 BashTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit SkillTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 feature-flags-audit WorkflowTool 状态为已实现 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
178 lines
6.9 KiB
Plaintext
178 lines
6.9 KiB
Plaintext
---
|
||
title: "权限模型 - Allow/Ask/Deny 三级权限体系"
|
||
description: "详解 Claude Code 的三级权限模型实现:基于 src/utils/permissions/permissions.ts 的规则匹配引擎、五层规则来源优先级、工具名/命令/路径三维度匹配、Denial Tracking 死循环防护、权限模式切换机制。"
|
||
keywords: ["权限模型", "Allow Ask Deny", "PermissionRule", "checkPermissions", "Denial Tracking", "权限规则"]
|
||
---
|
||
|
||
{/* 本章目标:基于源码揭示权限系统的完整实现 */}
|
||
|
||
## 三种权限行为
|
||
|
||
每一次工具调用,系统都会做出三种裁决之一:
|
||
|
||
| 行为 | 含义 | 返回类型 | 典型场景 |
|
||
|------|------|---------|---------|
|
||
| **Allow** | 自动放行,用户无感知 | `{ behavior: 'allow', updatedInput, decisionReason }` | Read 读取项目内文件 |
|
||
| **Ask** | 弹出确认对话框 | `{ behavior: 'ask', message, suggestions, metadata }` | Bash 执行未知命令 |
|
||
| **Deny** | 直接拒绝 | `{ behavior: 'deny', message, decisionReason }` | 尝试执行被禁止的命令 |
|
||
|
||
这些行为由 `PermissionResult` 类型定义(`src/utils/permissions/PermissionResult.ts`)。
|
||
|
||
## 权限规则的来源
|
||
|
||
规则从 8 个来源汇聚(`PERMISSION_RULE_SOURCES`,`permissions.ts:109`),优先级从低到高(后者覆盖前者):
|
||
|
||
```
|
||
1. userSettings — ~/.claude/settings.json(跨项目)
|
||
2. projectSettings — .claude/settings.json(团队共享)
|
||
3. localSettings — .claude/settings.local.json(gitignored,个人覆盖)
|
||
4. flagSettings — --settings 命令行参数
|
||
5. policySettings — 企业管理员下发的策略(用户不可覆盖)
|
||
6. cliArg — 命令行 --allow/--deny 参数
|
||
7. command — Skill 工具的 allowedTools 白名单
|
||
8. session — 用户在当前对话中手动授权("Always allow")
|
||
```
|
||
|
||
每个来源维护三个数组:`alwaysAllowRules[source]`、`alwaysAskRules[source]`、`alwaysDenyRules[source]`。
|
||
|
||
规则数据结构为 `PermissionRule`:
|
||
```typescript
|
||
{
|
||
source: PermissionRuleSource // 来自哪个层级
|
||
ruleBehavior: 'allow' | 'ask' | 'deny'
|
||
ruleValue: {
|
||
toolName: string // 如 "Bash"、"mcp__server1"
|
||
ruleContent?: string // 如 "git *"、"src/**"
|
||
}
|
||
}
|
||
```
|
||
|
||
## 规则匹配引擎
|
||
|
||
### 三维度匹配
|
||
|
||
`permissions.ts` 实现了三种匹配维度:
|
||
|
||
**1. 工具名匹配**(`toolMatchesRule()`,第 238 行)
|
||
|
||
匹配整个工具,仅当规则没有 `ruleContent`:
|
||
```typescript
|
||
// 精确匹配
|
||
rule "Bash" → 匹配 BashTool
|
||
rule "mcp__server1" → 匹配该 MCP Server 的所有工具(server 级别)
|
||
rule "mcp__server1__*" → 通配符匹配(同上)
|
||
```
|
||
|
||
MCP 工具使用 `getToolNameForPermissionCheck()` 获取匹配名称,支持有前缀(`mcp__server__tool`)和无前缀模式。
|
||
|
||
**2. 命令模式匹配**(BashTool 的 `checkPermissions()`)
|
||
|
||
BashTool 通过 `preparePermissionMatcher()`(`Tool.ts:520`)解析命令模式:
|
||
```json
|
||
{"tool": "Bash", "ruleContent": "git *"} → 匹配 "git commit -m 'fix'"
|
||
```
|
||
|
||
命令通过 AST 解析(`readOnlyValidation.ts` 使用 tree-sitter bash),提取第一个子命令进行匹配。
|
||
|
||
**3. 路径匹配**(文件工具的 `checkPermissions()`)
|
||
|
||
Read/Edit/Write 工具通过 `getPath()` 提取文件路径,与 `ruleContent` 中的 glob 模式匹配:
|
||
```json
|
||
{"tool": "Edit", "ruleContent": "src/**"} → 匹配 "src/utils/foo.ts"
|
||
```
|
||
|
||
### 权限检查的完整流程
|
||
|
||
每次工具调用的权限检查(`canUseTool()` → `checkPermissions()`)经过以下步骤:
|
||
|
||
```
|
||
1a. Blanket deny 检查
|
||
getDenyRuleForTool() → 工具名完全匹配 deny 规则?
|
||
↓ 命中 → deny(工具在 getTools() 阶段就被过滤掉)
|
||
|
||
1b. Blanket allow 检查
|
||
toolAlwaysAllowedRule() → 工具名完全匹配 allow 规则?
|
||
↓ 命中 → allow
|
||
|
||
2. 工具自身 checkPermissions()
|
||
各工具有自定义逻辑:
|
||
- BashTool: readOnlyValidation → sandbox 判定 → AST 解析 → 模式匹配
|
||
- FileEditTool: 路径白名单检查
|
||
- SkillTool: safe properties 白名单 + 精确/前缀匹配
|
||
↓ 返回 PermissionResult
|
||
|
||
3. Hook 系统
|
||
executePermissionRequestHooks() → PreToolUse hook 可以 override
|
||
↓ hook 返回 deny → deny
|
||
↓ hook 返回 ask → 升级为 ask
|
||
|
||
4. Ask 规则检查
|
||
getAskRules() → 命中 → ask
|
||
|
||
5. 默认行为
|
||
根据当前 permissionMode 决定默认行为
|
||
- 'default': 大部分工具 ask
|
||
- 'plan': 写操作 deny,读操作 allow
|
||
- 'bypass': 全部 allow
|
||
```
|
||
|
||
## 权限模式
|
||
|
||
| 模式 | `PermissionMode` 值 | 适用场景 | 行为 |
|
||
|------|---------------------|---------|------|
|
||
| **Default** | `'default'` | 日常使用 | 敏感操作逐一确认 |
|
||
| **Plan Mode** | `'plan'` | 探索阶段 | 只能读不能写(`isReadOnly()` 检查) |
|
||
| **Accept Edits** | `'acceptEdits'` | 快速迭代 | 工作区内文件编辑自动放行,其他操作仍需确认 |
|
||
| **Don't Ask** | `'dontAsk'` | 减少打断 | 尽量自动决策,减少确认弹窗 |
|
||
| **Auto** | `'auto'` | 信任 AI | 通过 transcript classifier 自动决策(需 `TRANSCRIPT_CLASSIFIER` feature flag) |
|
||
| **Bypass** | `'bypassPermissions'` | 完全信任 | 所有操作自动放行(需显式 `--dangerously-skip-permissions`) |
|
||
|
||
Plan Mode 切换由 `EnterPlanModeTool.call()` 触发:
|
||
```typescript
|
||
// EnterPlanModeTool.ts:88
|
||
context.setAppState(prev => ({
|
||
...prev,
|
||
toolPermissionContext: applyPermissionUpdate(
|
||
prepareContextForPlanMode(prev.toolPermissionContext),
|
||
{ type: 'setMode', mode: 'plan', destination: 'session' },
|
||
),
|
||
}))
|
||
```
|
||
|
||
退出时由 `ExitPlanModeV2Tool` 恢复为之前的模式。
|
||
|
||
## Denial Tracking:死循环防护
|
||
|
||
`src/utils/permissions/denialTracking.ts` 实现了拒绝追踪机制:
|
||
|
||
```typescript
|
||
const DENIAL_LIMITS = {
|
||
maxConsecutive: 3, // 同一工具连续拒绝上限
|
||
maxTotal: 20, // 总拒绝上限
|
||
}
|
||
```
|
||
|
||
当 AI 被连续拒绝同一类操作达到上限时:
|
||
1. `recordDenial()` 记录拒绝,增加计数
|
||
2. `shouldFallbackToPrompting()` 检测到连续拒绝,返回 true
|
||
3. 系统向 AI 注入消息:"Your previous tool call was rejected..."
|
||
4. AI 被迫改变策略,避免"反复请求同一个被拒操作"的死循环
|
||
|
||
操作成功时调用 `recordSuccess()` 重置计数。
|
||
|
||
## 规则的运行时更新
|
||
|
||
权限规则可以在运行时动态更新(`applyPermissionUpdate()`,`PermissionUpdate.ts`):
|
||
|
||
```typescript
|
||
type PermissionUpdate =
|
||
| { type: 'addRules', destination, rules, behavior }
|
||
| { type: 'replaceRules', destination, rules, behavior }
|
||
| { type: 'removeRules', destination, rules, behavior }
|
||
| { type: 'setMode', destination, mode }
|
||
| { type: 'addDirectories', destination, directories }
|
||
| { type: 'removeDirectories', destination, directories }
|
||
```
|
||
|
||
当用户在 Ask 对话框中选择 "Always allow",系统调用 `persistPermissionUpdates()` 将规则写入对应层级的 settings 文件(project/user/managed),同时更新内存中的 `toolPermissionContext`。
|