Enhanced File Removal Prevention in pre_tool_use Hook#4
Open
qdhenry wants to merge 2 commits intodisler:mainfrom
Open
Enhanced File Removal Prevention in pre_tool_use Hook#4qdhenry wants to merge 2 commits intodisler:mainfrom
qdhenry wants to merge 2 commits intodisler:mainfrom
Conversation
- Expand command detection to catch alternative removal methods - Add protected path system with configurable paths via env var - Improve user feedback with context-specific error messages - Block workarounds and alternative deletion methods
- Add logic to check if paths are within CWD - Allow deletion of any files/folders within CWD (unless protected) - Block deletions outside CWD or in protected system paths - Improve error messages to distinguish between different block reasons
Author
|
@disler , For context, I wanted to borrow this hook you published previously for a project I am working on. However, when I ran some tests, I realized Claude had snuck around the hook and moved the directory on my home path straight to the trash can using the mv command, so seeing that I wanted to add some additional guards to prevent this. I'm super grateful for your YouTube channel and the projects you share, brother! Keep up the fantastic work! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR enhances the pre_tool_use.py hook to provide intelligent file removal prevention that allows safe operations within the current working directory while protecting system files.
Key Changes
1. Smart Deletion Logic
2. New Helper Functions
is_within_cwd(path)- Checks if a path is within the current working directoryextract_target_paths(command)- Extracts all target paths from removal commands3. Improved Pattern Detection
The hook detects various removal methods:
4. Better Error Messages
Configuration
Users can customize protected paths via the
CLAUDE_PROTECTED_PATHSenvironment variable with colon-separated paths.Testing
All scenarios tested successfully:
Impact
This change provides a better balance between safety and usability: