Skip to main content
hook

file-protection — Claude Code hook: file protection

This hook blocks Edit, MultiEdit, and Write when a file path matches one of its patterns. Use it to guard system files and production configuration from accidental edits.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook security/file-protection
Open on GitHub

What file-protection does

Before Edit, MultiEdit, or Write runs, the hook checks the path in CLAUDE_TOOL_FILE_PATH. A match makes the command exit with an error, so the requested file change is blocked.

Its patterns cover paths such as */etc/*, */usr/bin/*, *.production.*, *prod*config*, */node_modules/*, and */vendor/*. Protection is limited to these patterns; paths that do not match them are not blocked.

Install it with npx claude-code-templates@latest --hook security/file-protection. The hook configuration is in .claude/settings.json. Review the patterns if the files you need to protect use other paths.

What it helps with

  • block edits under */etc/*
  • prevent writes to files in */node_modules/*
  • guard files matching *.production.*
  • stop changes to paths matching *prod*config*

Example prompts for the agent

  • Edit the service configuration at /etc/service.conf.
  • Write the update to node_modules/package.json.
  • Change prod-config.json.

How to install file-protection

1claude-code-templates

npx claude-code-templates@latest --hook security/file-protection

2Manually: put the files in place

.claude/settings.json

The hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json

Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command

Questions about file-protection

How do I install file-protection in Claude Code?

Run npx claude-code-templates@latest --hook security/file-protection. Its configuration is in .claude/settings.json.

What does file-protection do?

It checks the target path before Edit, MultiEdit, or Write. If the path matches a protected pattern, the operation exits with an error.

Which paths does file-protection block?

Patterns include */etc/*, */usr/bin/*, *.production.*, *prod*config*, */node_modules/*, and */vendor/*. Paths outside those patterns are not blocked.

Source text

file-protection.json · MIT
{
  "description": "Protect critical files from accidental modification. Prevents editing of important system files, configuration files, and production code.",
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|MultiEdit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "PROTECTED_PATTERNS=('*/etc/*' '*/usr/bin/*' '*/usr/sbin/*' '*.production.*' '*prod*config*' '*/node_modules/*' '*/vendor/*'); for pattern in \"${PROTECTED_PATTERNS[@]}\"; do if [[ \"$CLAUDE_TOOL_FILE_PATH\" == $pattern ]]; then echo \"Error: File $CLAUDE_TOOL_FILE_PATH is protected from modification\" >&2; exit 1; fi; done"
          }
        ]
      }
    ]
  }
}

© davila7 · MIT · davila7/claude-code-templates

Similar hooks

All hooks for Security →

From the same set740

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor