文章总结: 本文档介绍ClaudeCode的高级配置技巧与安全实践,重点涵盖全局CLAUDE.md的多层级安全规则、项目脚手架标准化、MCP服务器集成(如Context7)、自定义命令与子代理、Skills技能包及Hooks钩子机制。通过PreToolUse钩子强制禁止访问.env等敏感文件,结合Skills提供操作指导,实现AI辅助开发的安全可控与效率提升。 综合评分: 92 文章分类: 安全建设,安全开发,安全工具,AI安全,安全运营
Claude 高级技巧与安全配置CLAUDE.md、MCP、命令、技能与钩子 (Skills & Hooks)
原创
Ti Ti
TIPFactory情报工厂
2026年2月11日 19:33 江苏
1 安全守门员: 全局 CLAUDE.md
Claude Code 按以下顺序加载 CLAUDE.md 文件:
| 层级 | 位置 | 用途 |
| — | — | — |
| 企业级 | /etc/claude-code/CLAUDE.md | 组织范围的策略 |
| 全局 | ~/.claude/CLAUDE.md | 你对所有项目的标准 |
| 项目级 | ./CLAUDE.md | 团队共享的项目指令 |
| 项目本地 | ./CLAUDE.local.md | 个人项目的本地覆盖 |
全局文件适用于处理的每一个项目。全局文件可以配置的内容:
1.1 身份认证
示例:
## GitHub 账号
**始终**对所有项目使用 **YourUsername**:
- SSH: `[email protected]:YourUsername/<repo>.git`
## Docker Hub
已验证。用户名在 `~/.env` 中定义为 `DOCKER_HUB_USER`
## 部署
生产环境使用 Dokploy MCP。API 路径在 `~/.env` 中。
1.2 安全规则
## 绝对禁止事项
以下规则是绝对的:
### 严禁发布敏感数据
- 严禁将密码、API 密钥、Token 发布到 git/npm/docker。
- 在任何 commit 之前:验证不包含任何 Secrets。
### 严禁提交 .env 文件
- 严禁将 `.env` 提交到 git。
- 始终验证 `.env` 已列入 `.gitignore`。
### 严禁硬编码凭据
- 始终使用环境变量。
为什么这很重要:Claude 会读取你的 .env 。安全研究发现,Claude Code 会在未获明确许可的情况下自动读取 .env文件。正如 Backslash Security组织警告的内容:
“如果不加限制,Claude 可能会读取 .env、AWS 凭据或 secrets.json,并通过‘有用的建议’将其泄露。”
你的全局 CLAUDE.md 建立了一个行为守门员 —— 即使 Claude 拥有访问权限,它也不会输出这些 Secrets。
2 新项目脚手架的全局规则
这是全局 CLAUDE.md 变成“项目工厂”的地方。你创建的每个新项目都会自动继承你的标准、结构和安全要求。在全局 CLAUDE.md 中加入脚手架规则,比如:在全局文件中添加“新项目设置 (New Project Setup)”部分:
## 新项目设置
创建任何新项目时,务必执行以下操作:
### 1. 必须创建的文件
- `.env` — 环境变量(严禁提交)
- `.env.example` — 带有占位符的模板
- `.gitignore` — 必须包含:.env, .env.*, node_modules/, dist/, .claude/
- `README.md` — 项目概述(引用环境变量,不要硬编码)
### 2. 标准目录结构
project-root/
├── src/ # 源代码
├── tests/ # 测试文件
├── .claude/ # Claude 配置
│ └── commands/# 自定义斜杠命令
└── scripts/ # 构建/部署脚本
### 3. Required .gitignore Entries
# Environment
.env
.env.*
.env.local
!.env.example
# Dependencies
node_modules/
vendor/
__pycache__/
.venv/
# Build outputs
dist/
build/
.next/
*.pyc
# Claude local files
.claude/settings.local.json
CLAUDE.local.md
# IDE
.idea/
.vscode/
*.swp
# OS
.DS_Store
Thumbs.db
### 4. Required CLAUDE.md Sections
每一个项目CLAUDE.md都需要:
# Project Name
## Overview
[What this project does]
## Tech Stack
- Language: [e.g., TypeScript]
- Framework: [e.g., Next.js]
- Database: [e.g., PostgreSQL]
## Commands
- `npm run dev` — Start development server
- `npm run build` — Build for production
- `npm test` — Run tests
- `npm run lint` — Check code style
## Architecture
[High-level overview of the codebase structure]
## Environment Variables
[List required env vars WITHOUT values]
为什么要做?GitHub – madison-hutson/claude-project-scaffolding: Ready-to-use project scaffolding with quality gates, testing, and Claude Code integration. Problem This Solves: LLM-assisted development fails by silently expanding scope, degrading quality, and losing architectural intent. This scaffold exists to make those failures impossible without explicit acknowledgement.
“LLM 辅助开发失败的原因在于,它会在不知不觉中扩大范围、降低质量并失去架构意图。”
- • 每个项目都有不同的结构
- • 安全文件容易被遗忘(.gitignore、.dockerignore)
- • 错误处理不一致
- • 文档格式各不相同
- • 你浪费时间重新解释同样的要求。
GitHub – madison-hutson/claude-project-scaffolding: Ready-to-use project scaffolding with quality gates, testing, and Claude Code integration. Problem This Solves: LLM-assisted development fails by silently expanding scope, degrading quality, and losing architectural intent. This scaffold exists to make those failures impossible without explicit acknowledgement.
3 MCP服务器
MCP(模型上下文协议) 允许 Claude 与外部工具和服务进行交互。
添加MCP服务器:
# Add a server
claude mcp add <server-name> -- <command>
# List servers
claude mcp list
# Remove a server
claude mcp remove <server-name>
核心 MCP 服务器推荐
| 服务器 | 用途 | 安装命令 |
| — | — | — |
| Context7 | 实时文档访问 | claude mcp add context7 -- npx -y @anthropic-ai/context7-mcp |
| Playwright | 浏览器自动化测试 | claude mcp add playwright -- npx -y @anthropic-ai/playwright-mcp |
| GitHub | 仓库管理 | claude mcp add github -- npx -y @modelcontextprotocol/server-github |
| Filesystem | 扩展文件访问 | claude mcp add fs -- npx -y @anthropic-ai/filesystem-mcp |
具体到某一个MCP服务器,可以在Claude.md如下配置:
## Required MCP Servers
These MCP servers must be installed for full functionality:
### context7
Live documentation access for all libraries.
Install: `claude mcp add context7 -- npx -y @anthropic-ai/context7-mcp`
### playwright
Browser automation for testing.
Install: `claude mcp add playwright -- npx -y @anthropic-ai/playwright-mcp`
4 Context7 实时文档
Context7 是改变游戏规则的工具。它让 Claude 能够访问任何库的最新文档,解决了模型训练数据截止日期 (Knowledge Cutoff) 的问题。示例:
You: "Using context7, show me how to use the new Next.js 15 cache API"
Claude: *fetches current Next.js docs*
*provides accurate, up-to-date code*
| Pattern | Example | | — | — | | 明确的 | “使用 context7,查找 Prisma 的 createMany 方法” | | 研究 | “请查看 context7 以了解 React 服务器组件模式” | | Debugging 调试 | “使用 context7 查找正确的 Tailwind v4 语法” | | | |
4.1.1 添加到全局 CLAUDE.md
## Documentation Lookup
When unsure about library APIs or recent changes:
1. Use Context7 MCP to fetch current documentation
2. Prefer official docs over training knowledge
3. Always verify version compatibility
5 自定义命令和子代理
命令以 Markdown 文件的形式存储在 .claude/commands/ 中,就是我们输入到/斜杠触发的这个。
示例:
---
description: Fix TypeScript errors
---
Run `npx tsc --noEmit` and fix any type errors.
For each error:
1. Identify the root cause
2. Fix with minimal changes
3. Verify the fix compiles
After fixing all errors, run the check again to confirm.
**使用方法:/fix-types
5.1 子 agent
子代理在隔离的上下文窗口中运行——它们不会干扰您的主要对话。
每个子代理都在其独立的上下文窗口中运行。这意味着它可以专注于特定任务,而不会受到主要对话的“干扰”。
一些全局Commands可以用来触发子代理干活:
## Global Commands
Store these in ~/.claude/commands/ for use in ALL projects:
### /new-project
Creates new project with all scaffolding rules applied.
### /security-check
Scans for secrets, validates .gitignore, checks .env handling.
### /pre-commit
Runs all quality gates before committing.
### /docs-lookup
Spawns sub-agent with Context7 to research documentation.
6 单一用途的对话聊天很重要
这或许是最重要的部分。 研究始终表明,混杂不同主题会严重降低准确性参考:arxiv.org
随着上下文窗口中标记数量的增加,模型准确回忆信息的能力会下降。
- • 中间迷失问题:LLMs对上下文开头和结尾的信息记忆效果最好,中间内容容易被遗忘。
- • 情境漂移:当你转换话题时,先前的上下文就会变成干扰后续推理的噪音 。
- • 注意力预算:“Transformer 模型需要 n² 个词元之间的成对关系。随着上下文的扩展,模型的‘注意力预算’就会变得捉襟见肘。”
可以在执行任务之间经常使用 /clear 来重置上下文窗口,尤其是在长时间会话期间,无关的对话会不断累积。
也可以使用子Agent隔离执行:
Spawn a sub-agent to research React Server Components.
Return only a summary of key patterns.
7 Skills & Hooks — 强制执行
为什么 CLAUDE.md 规则会失效?
- • 上下文窗口压力 :长时间的对话可能会使规则脱离主动关注范围。
- • 指令冲突 :其他上下文可能会凌驾于您的规则之上。
- • 复制粘贴传播 :即使 Claude 不编辑
.env,它也可能将密钥复制到另一个文件。
PreToolUse hook blocking .env edits:
→ Always runs
→ Returns exit code 2
→ Operation blocked. Period.
CLAUDE.md saying "don't edit .env":
→ Parsed by LLM
→ Weighed against other context
→ Maybe followed
7.1 Hooks:确定性控制
钩子是 shell 命令,会在特定的生命周期节点执行。它们不是建议,而是每次都会运行的代码。
| 事件 (Event) | 触发时机 (When It Fires) | 使用场景 (Use Case) |
| — | — | — |
| PreToolUse | 任何工具执行之前 | 拦截/阻止危险操作 |
| PostToolUse | 工具执行完成之后 | 运行 Linter、格式化工具或测试 |
| Stop | Claude 完成响应时 | 回合结束时的质量把控 (Quality Gates) |
| UserPromptSubmit | 用户提交提示词 (Prompt) 时 | 验证或增强提示词内容 |
| SessionStart | 新会话开始时 | 加载上下文,进行初始化 |
| Notification | Claude 发送警报时 | 桌面通知 |
示例:禁止密码获取
在~/.claude/settings.json中添加:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Read|Edit|Write",
"hooks": [
{
"type": "command",
"command": "python3 ~/.claude/hooks/block-secrets.py"
}
]
}
]
}
}
hook脚本:~/.claude/hooks/block-secrets.py
#!/usr/bin/env python3
"""
PreToolUse hook to block access to sensitive files.
Exit code 2 = block operation and feed stderr to Claude.
"""
import json
import sys
from pathlib import Path
SENSITIVE_PATTERNS = {
'.env', '.env.local', '.env.production',
'secrets.json', 'secrets.yaml',
'id_rsa', 'id_ed25519', '.npmrc', '.pypirc'
}
def main():
try:
data = json.load(sys.stdin)
tool_input = data.get('tool_input', {})
file_path = tool_input.get('file_path') or tool_input.get('path') or ''
if not file_path:
sys.exit(0)
path = Path(file_path)
if path.name in SENSITIVE_PATTERNS or '.env' in str(path):
print(f"BLOCKED: Access to '{path.name}' denied.", file=sys.stderr)
print("Use environment variables instead.", file=sys.stderr)
sys.exit(2) # Exit 2 = block and feed stderr to Claude
sys.exit(0)
except Exception:
sys.exit(0) # Fail open
if __name__ == '__main__':
main()
示例:在回复停止时候验证质量
当 Claude 完成每一回合后,运行代码检查和测试:
{
"hooks": {
"Stop": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/end-of-turn.sh"
}
]
}
]
}
}
8 Skills
技能是 Markdown 文件,它教 Claude 如何做某事——就像一份培训手册,Claude 可以随时查阅。
摘自 Anthropic 的工程博客 :
“培养一名经纪人的技能,就像为一名新员工编写入职指南一样。”
8.1 Skills 如何工作
- • 启动 :Claude 仅将技能_名称和描述_加载到上下文中
- • 触发条件 :在相关情况下,Claude 会读取完整的
SKILL.md文件。 - • 按需加载 :仅在引用时才加载其他资源。
这意味着您可以以最小的上下文成本安装数十种技能。
8.2 Skill Structure
.claude/skills/
└── commit-messages/
├── SKILL.md ← Required: instructions + frontmatter
├── templates.md ← Optional: reference material
└── validate.py ← Optional: executable scripts
8.2.1 Skill.md
---
name: commit-messages
description: Generate clear commit messages from git diffs. Use when writing commit messages or reviewing staged changes.
---
# Commit Message Skill
When generating commit messages:
1. Run `git diff --staged` to see changes
2. Use conventional commit format: `type(scope): description`
3. Keep subject line under 72 characters
## Types
- feat: New feature
- fix: Bug fix
- docs: Documentation
- refactor: Code restructuring
8.3 何时使用技能,何时使用其他选项
| 需求 (Need) | 解决方案 (Solution) |
| — | — |
| 项目特定的指令 | 项目级的 CLAUDE.md |
| 跨项目的可重用工作流 | 技能 (Skill) |
| 外部工具集成 | MCP 服务器 (MCP Server) |
| 确定性的强制执行 | 钩子 (Hook) |
| 一次性自动化任务 | 斜杠命令 (Slash Command) |
9 结合Hooks和Skills
- • 一项
secrets-handling技能教会了克劳德_如何_妥善处理秘密。 - •
PreToolUse钩子强制 Claude 永远无法实际读取.env文件。
| 层级 (Layer) | 机制 (Mechanism) | 类型 (Type) | | — | — | — | | 1 | CLAUDE.md 行为规则 | 建议 (Suggestion) | | 2 | PreToolUse 钩子 (Hooks) | 强制执行 (Enforcement) | | 3 | settings.json 拒绝列表 (Deny List) | 强制执行 (Enforcement) | | 4 | .gitignore | 预防 (Prevention) | | 5 | 带有安全检查列表的技能 (Skills) | 指导 (Guidance) |
一个完整的CLAUDE.md示例:
# Global CLAUDE.md
## Identity & Accounts
- GitHub: YourUsername (SSH key: ~/.ssh/id_ed25519)
- Docker Hub: authenticated via ~/.docker/config.json
- Deployment: Dokploy (API URL in ~/.env)
## NEVER EVER DO (Security Gatekeeper)
- NEVER commit .env files
- NEVER hardcode credentials
- NEVER publish secrets to git/npm/docker
- NEVER skip .gitignore verification
## New Project Setup (Scaffolding Rules)
### Required Files
- .env (NEVER commit)
- .env.example (with placeholders)
- .gitignore (with all required entries)
- .dockerignore
- README.md
- CLAUDE.md
### Required Structure
project/
├── src/
├── tests/
├── docs/
├── .claude/commands/
└── scripts/
### Required .gitignore
.env
.env.*
node_modules/
dist/
.claude/settings.local.json
CLAUDE.local.md
### Node.js Requirements
- Error handlers in entry point
- TypeScript strict mode
- ESLint + Prettier configured
### Quality Gates
- No file > 300 lines
- All tests must pass
- No linter warnings
- CI/CD workflow required
## Framework-Specific Rules
[Your framework patterns here]
## Required MCP Servers
- context7 (live documentation)
- playwright (browser testing)
## Global Commands
- /new-project — Apply scaffolding rules
- /security-check — Verify no secrets exposed
- /pre-commit — Run all quality gates
10 快捷参考
| Tool | Purpose | Location | | — | — | — | | Global CLAUDE.md | Security + Scaffolding | ~/.claude/CLAUDE.md | | Project CLAUDE.md | Architecture + Commands | ./CLAUDE.md | | MCP Servers | External integrations | claude mcp add | | Context7 | Live documentation | claude mcp add context7 | | Slash Commands | Workflow automation | .claude/commands/*.md | | Skills | Packaged expertise | .claude/skills/*/SKILL.md | | Hooks | Deterministic enforcement | ~/.claude/settings.json | | Sub-Agents | Isolated context | Spawn via commands | | /clear | Reset context | Type in chat | | /init | Generate project CLAUDE.md | Type in chat |
免责声明:
本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。
任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。
本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我。
本文转载自:TIPFactory情报工厂 Ti Ti《Claude 高级技巧与安全配置CLAUDE.md、MCP、命令、技能与钩子 (Skills & Hooks)》
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。








评论