block-dangerous-git.sh 507 B

12345678910111213141516171819202122232425
  1. #!/bin/bash
  2. INPUT=$(cat)
  3. COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')
  4. DANGEROUS_PATTERNS=(
  5. "git push"
  6. "git reset --hard"
  7. "git clean -fd"
  8. "git clean -f"
  9. "git branch -D"
  10. "git checkout \."
  11. "git restore \."
  12. "push --force"
  13. "reset --hard"
  14. )
  15. for pattern in "${DANGEROUS_PATTERNS[@]}"; do
  16. if echo "$COMMAND" | grep -qE "$pattern"; then
  17. echo "BLOCKED: '$COMMAND' matches dangerous pattern '$pattern'. The user has prevented you from doing this." >&2
  18. exit 2
  19. fi
  20. done
  21. exit 0