#!/bin/bash # ============================================================================= # Repository Integrity Scanner Hook # ============================================================================= # Event: PreToolUse (runs before Read on potentially malicious files) # Purpose: Scan repository files for injection vectors before processing # # This hook detects prompt injection attempts hidden in: # - README.md, SECURITY.md (hidden HTML comments) # - package.json, pyproject.toml (malicious scripts) # - .claude/, .cursor/ configs (tampered configurations) # - CONTRIBUTING.md (social engineering instructions) # # Installation: # Add to .claude/settings.json: # { # "hooks": { # "PreToolUse": [{ # "matcher": "Read", # "hooks": ["bash examples/hooks/bash/repo-integrity-scanner.sh"] # }] # } # } # # Exit codes: # 0 = allow (safe or not a target file) # 2 = block (injection detected) # # References: # - CVE-2025-54135: RCE via config file rewriting # - CVE-2025-54136: Team backdoor via post-approval config tampering # ============================================================================= set -euo pipefail # Read the hook input from stdin INPUT=$(cat) TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // empty') TOOL_INPUT=$(echo "$INPUT" | jq -r '.tool_input // empty') # Only check Read operations [[ "$TOOL_NAME" != "Read" ]] && exit 0 FILE_PATH=$(echo "$TOOL_INPUT" | jq -r '.file_path // empty') [[ -z "$FILE_PATH" ]] && exit 0 # Check if file exists [[ ! -f "$FILE_PATH" ]] && exit 0 FILENAME=$(basename "$FILE_PATH") DIRNAME=$(dirname "$FILE_PATH") # === HIGH-RISK FILES === # These files are common injection vectors HIGH_RISK_FILES=( "README.md" "readme.md" "SECURITY.md" "CONTRIBUTING.md" "CHANGELOG.md" ) # === CONFIG FILES === # Configuration files that could contain malicious settings CONFIG_FILES=( "package.json" "pyproject.toml" "setup.py" "setup.cfg" "Makefile" ".pre-commit-config.yaml" ) # === CLAUDE/CURSOR CONFIG === # IDE config files that could be tampered IDE_CONFIG_PATTERNS=( ".claude" ".cursor" ".vscode" ".idea" ) # Function to check for injection patterns check_injection_patterns() { local file="$1" local content content=$(cat "$file" 2>/dev/null || echo "") # === HIDDEN HTML COMMENTS === # Look for HTML comments with instruction-like content if echo "$content" | grep -qiE ''; then echo "BLOCKED: Hidden HTML comment with suspicious instructions in: $file" >&2 return 1 fi # === ROLE OVERRIDE PATTERNS === if echo "$content" | grep -qiE 'ignore (previous|all|your) instructions|you are now|pretend (you are|to be)|from now on|new instructions:'; then echo "BLOCKED: Prompt injection pattern detected in: $file" >&2 return 1 fi # === BASE64 IN COMMENTS === # Long base64 strings in comments could be encoded instructions if echo "$content" | grep -qE '(#|//|