surshar / claude-code
Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.
Built by Anthropic · github.com/anthropics
| 1 | #!/bin/bash |
| 2 | # Example PreToolUse hook for validating Bash commands |
| 3 | # This script demonstrates bash command validation patterns |
| 4 | |
| 5 | set -euo pipefail |
| 6 | |
| 7 | # Read input from stdin |
| 8 | input=$(cat) |
| 9 | |
| 10 | # Extract command |
| 11 | command=$(echo "$input" | jq -r '.tool_input.command // empty') |
| 12 | |
| 13 | # Validate command exists |
| 14 | if [ -z "$command" ]; then |
| 15 | echo '{"continue": true}' # No command to validate |
| 16 | exit 0 |
| 17 | fi |
| 18 | |
| 19 | # Check for obviously safe commands (quick approval) |
| 20 | if [[ "$command" =~ ^(ls|pwd|echo|date|whoami)(\s|$) ]]; then |
| 21 | exit 0 |
| 22 | fi |
| 23 | |
| 24 | # Check for destructive operations |
| 25 | if [[ "$command" == *"rm -rf"* ]] || [[ "$command" == *"rm -fr"* ]]; then |
| 26 | echo '{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "Dangerous command detected: rm -rf"}' >&2 |
| 27 | exit 2 |
| 28 | fi |
| 29 | |
| 30 | # Check for other dangerous commands |
| 31 | if [[ "$command" == *"dd if="* ]] || [[ "$command" == *"mkfs"* ]] || [[ "$command" == *"> /dev/"* ]]; then |
| 32 | echo '{"hookSpecificOutput": {"permissionDecision": "deny"}, "systemMessage": "Dangerous system operation detected"}' >&2 |
| 33 | exit 2 |
| 34 | fi |
| 35 | |
| 36 | # Check for privilege escalation |
| 37 | if [[ "$command" == sudo* ]] || [[ "$command" == su* ]]; then |
| 38 | echo '{"hookSpecificOutput": {"permissionDecision": "ask"}, "systemMessage": "Command requires elevated privileges"}' >&2 |
| 39 | exit 2 |
| 40 | fi |
| 41 | |
| 42 | # Approve the operation |
| 43 | exit 0 |