There are files you don't want your agnet to read, especially ones with secrets in them. Here's how you can prevent it:
In ~/.claude/settings.json (or in the project directory .claude/settings.json)
{
"hooks": {
"PreToolUse": [
{
"matcher": "Read|Write|Edit",
"hooks": [
{
"type": "command",
"command": "python ~/.claude/hooks/block_env_files.py"
}
]
}
],
...You can probably do it with shell and jq, but I find this nicer. Claude passes the data to the hook in JSON over stdin.
#!/usr/bin/env python
import json
import sys
from pathlib import Path
data = json.load(sys.stdin)
file_path = data.get('tool_input', {}).get('file_path', '')
if Path(file_path).name in {'.env', '.envrc'}:
msg = {
'continue': False,
'stopReason': 'Blocked: reading .env/.envrc files is not allowed',
}
print(json.dumps(msg))