-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotect-files.sh
More file actions
executable file
·51 lines (40 loc) · 1.56 KB
/
Copy pathprotect-files.sh
File metadata and controls
executable file
·51 lines (40 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# protect-files.sh
# PreToolUse hook for Edit|Write tools.
# Blocks edits to sensitive files: .env, lock files, .git/, and paths
# containing "secret" or "credential".
# Exit codes: 0 = allow, 2 = block
set -euo pipefail
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Normalize to lowercase for case-insensitive pattern checks.
FILE_LOWER=$(echo "$FILE_PATH" | tr '[:upper:]' '[:lower:]')
BASENAME=$(basename "$FILE_PATH")
BASENAME_LOWER=$(echo "$BASENAME" | tr '[:upper:]' '[:lower:]')
block() {
local reason="$1"
echo "Blocked: $reason" >&2
exit 2
}
# Block .env files (exact name or .env.* variants).
if [[ "$BASENAME_LOWER" == ".env" ]] || [[ "$BASENAME_LOWER" == .env.* ]]; then
block "editing '$FILE_PATH' is not allowed (.env files may contain secrets)"
fi
# Block package lock files.
case "$BASENAME_LOWER" in
package-lock.json|yarn.lock|pnpm-lock.yaml|composer.lock|gemfile.lock|poetry.lock)
block "editing '$FILE_PATH' is not allowed (lock files should only be updated by the package manager)"
;;
esac
# Block anything inside a .git directory.
if [[ "$FILE_PATH" == *"/.git/"* ]] || [[ "$FILE_PATH" == .git/* ]]; then
block "editing '$FILE_PATH' is not allowed (files inside .git/ must not be modified directly)"
fi
# Block paths containing "secret" or "credential" (case-insensitive).
if [[ "$FILE_LOWER" == *secret* ]] || [[ "$FILE_LOWER" == *credential* ]]; then
block "editing '$FILE_PATH' is not allowed (path contains 'secret' or 'credential')"
fi
exit 0