Research indicates that secrets are leaked in 3.2% of public commits made via Claude Code, compared to the 1.5% average reported by GitGuardian. Relying solely on instructions within CLAUDE.md is insufficient, as agents may lose track of these rules during long sessions or fail to prioritize them within the context window.

The PreToolUse hook in Claude Code allows for intercepting tool calls. It receives a JSON object containing the tool name and input. A return code of 2 blocks the execution, while the agent receives the error reason via stderr. Developers can also return a JSON response with permissionDecision: deny to prevent unauthorized actions.

Security measures must account for how agents interact with Git. Simple file scanning fails if a key is added to the index and then deleted from the disk. Effective protection requires checking the index, differences against HEAD, and new files before a commit, as well as scanning all commits not yet pushed to the remote repository.

Best practices for securing AI agents include using dedicated tools like gitleaks or trufflehog for pattern matching, splitting complex commands into individual parts for inspection, and ensuring that any failure or timeout results in a blocking code 2. Additionally, native Git hooks are recommended as a secondary layer of defense.