Compétences claude-code-hooks
�
通过工具执行前后的钩子自定义 Claude Code 行为。自动验证命令、审核活动和执行策略。本文档技能提供完整的参考指南和适用于生产环境的安全模式。
Prend en charge: Code(CC)
1
Télécharger le ZIP du skill
2
Importer dans Claude
Allez dans Paramètres → Capacités → Skills → Importer un skill
3
Activez et commencez à utiliser
Tester
Utilisation de "claude-code-hooks". How do I block dangerous bash commands with a hook?
Résultat attendu:
- Create a PreToolUse hook that parses JSON input and checks for dangerous patterns
- Exit code 2 blocks the command, exit code 0 allows it
- Match specific tools with patterns like "Bash" or "*" for all tools
Audit de sécurité
Sûrv2 • 1/10/2026
Pure documentation skill containing only markdown reference files. No executable code, network calls, or file system access beyond documentation files. The skill documents Claude Code's built-in hook system and provides example scripts as documentation content.
4
Fichiers analysés
896
Lignes analysées
0
résultats
2
Total des audits
Aucun problème de sécurité trouvé
Audité par: claude Voir l’historique des audits →
Score de qualité
45
Architecture
100
Maintenabilité
83
Contenu
20
Communauté
100
Sécurité
78
Conformité aux spécifications
Ce que vous pouvez construire
验证并阻止危险命令
创建 PreToolUse 钩子来验证 Bash 命令,并在执行前阻止 rm -rf 或 mkfs 等破坏性操作。
审计所有工具活动
配置 PostToolUse 钩子将每个命令、文件更改和工具输出记录到审计日志中,用于合规和调试。
强制执行编码标准
使用匹配器模式在特定工具上运行 linter、格式化程序或安全扫描程序,然后才允许执行。
Essayez ces prompts
钩子基础
Claude Code 支持哪些钩子事件,它们何时触发?解释 PreToolUse 与 PostToolUse 的区别。
配置
如何在 settings.json 中配置钩子?向我展示 Bash 命令的 PreToolUse 钩子的确切 JSON 结构。
安全模式
编写钩子脚本的安全最佳实践是什么?如何防止命令注入和路径遍历?
生产使用
向我展示一个生产就绪的钩子脚本,包括输入验证、正确的错误处理、速率限制和审计日志记录。
Bonnes pratiques
- 在处理之前始终验证 JSON 输入结构,以防止错误
- 在 shell 脚本中引用所有变量以防止命令注入
- 使用 $CLAUDE_PROJECT_DIR 作为路径,确保脚本在不同环境中工作
Éviter
- 使用未加引号的变量,允许命令注入攻击
- 跳过输入验证并信任用户提供的工具输入
- 在钩子脚本中存储密钥或记录敏感数据
Foire aux questions
哪些 Claude 版本支持钩子?
Claude Code CLI 版本 0.28 及更高版本通过 settings.json 配置支持钩子系统。
钩子的最大超时时间是多少?
默认超时时间为 5000ms。您可以在钩子定义中配置更长的超时时间,实际限制范围内。
钩子可以调用其他 Claude Code 工具吗?
钩子作为外部脚本运行,无法直接调用 Claude Code 工具。它们作为独立进程执行。
钩子输出会被记录吗?
阻止时钩子的 stderr 出现在 Claude 输出中。stdout 被捕获但默认不记录。
为什么我的钩子没有触发?
检查匹配器模式是否正确,文件是否可执行,以及 settings.json 中的 JSON 是否有效,可使用 jq 进行验证。
钩子与 MCP 有什么不同?
钩子在工具调用之前或之后运行,用于验证和日志记录。MCP 提供新工具。钩子与现有工具配合使用。