Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created March 22, 2026 13:34
Show Gist options
  • Select an option

  • Save tebeka/b9c197ad5393e348e938498e7984fb26 to your computer and use it in GitHub Desktop.

Select an option

Save tebeka/b9c197ad5393e348e938498e7984fb26 to your computer and use it in GitHub Desktop.
Disallow Claude from Reading Secrets

Disallowing Claude from Reading Secrets

There are files you don't want your agnet to read, especially ones with secrets in them. Here's how you can prevent it:

PreToolUse Hook

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"
          }
        ]
      }
    ],
...

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment