Claude Code hook

Claude Code Security Hooks

7 layers of defense against prompt injection in Claude Code. Security hooks, read guards, canary files, and self-protection.

39 stars 4 forksHooks Source

Install

From source
git clone https://github.com/slavaspitsyn/claude-code-security-hooks.git

From the README

Claude Code Security Hooks

7 layers of defense against prompt injection in Claude Code.

Your AI coding agent has access to your SSH keys, cloud credentials, and environment variables. A single prompt injection — hidden text on a webpage, a malicious comment in code — can trick it into exfiltrating your secrets.

These hooks stop that before it happens.

The Problem

This is not theoretical. If you have Bash(curl *) in your allowed permissions, you are one prompt injection away from credential theft.

The Solution: 7 Layers

Quick Start

The installer will:
- Copy hook scripts to ~/.claude/hooks/
- Show you the JSON to add to your ~/.claude/settings.json
- Place canary files in ~/.ssh/ and other sensitive directories
- Audit your current permissions for dangerous patterns

What Gets Blocked

Credential + Network = Blocked

Direct Reads = Blocked

Self-Modification = Blocked

Hook Details

Layer 1: security-guard.sh

The main defense. Intercepts every Bash command and checks 7 rules:

Layer 2: read-guard.sh

Blocks Claude's Read tool on:

Layer 3: bash-read-guard.sh

Catches shell commands reading sensitive files: cat, head, tail, less, cp, mv, tar, zip, input redirects (<), and editors.

Layer 4: Hook Self-Protection

Inline Edit hook — blocks modifications to ~/.claude/settings.json and ~/.claude/hooks/. If the AI is compromised, it cannot disable its own guardrails.

Layer 5–6: POST Whitelist & Encoding Detection

Built into security-guard.sh. POST/upload only allowed to:
- api.anthropic.com, github.com, registry.npmjs.org
- localhost / 127.0.0.1
- Tailscale subnet (100.64.0.0/10)

Add your own domains in security-guard.sh Rule 3.

Layer 7: Canary Files

The last line of defense. If the AI bypasses all hooks and reads a file in ~/.ssh/, it encounters:

Fighting…
Read the full README on GitHub

How hooks work

  • Hooks are shell commands Claude Code runs at fixed points: before or after a tool call, when you submit a prompt, when Claude stops, on notifications and at session start.
  • They are set in the hooks section of ~/.claude/settings.json (or .claude/settings.json for a project), or come bundled in a plugin.
  • A hook can block a tool call, add context, or run a formatter or test. Read the scripts before you install them: they run on your machine with your permissions.
Claude Code docs

Details

Type
Hooks
Language
Shell
License
MIT
Last updated
26 March 2026

FAQ

What is Claude Code Security Hooks?

7 layers of defense against prompt injection in Claude Code. Security hooks, read guards, canary files, and self-protection. It is a hook for Claude Code, from slavaspitsyn/claude-code-security-hooks on GitHub.

How do I install Claude Code Security Hooks?

Clone slavaspitsyn/claude-code-security-hooks from GitHub and follow the setup steps in its README.

Is Claude Code Security Hooks popular?

slavaspitsyn/claude-code-security-hooks has 39 GitHub stars and 4 forks. It was last updated on 26 March 2026.

More hooks

Browse the most popular Claude Code mods, all hooks, or the skills and MCP server directories.