cd /news/ai-agents/como-evitar-que-claude-code-borre-tu… · home › topics › ai-agents › article
[ARTICLE · art-159845] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Cómo evitar que Claude Code borre tu proyecto: hooks de seguridad en 5 minutos

A developer published a set of Claude Code PreToolUse hooks that block destructive shell commands such as recursive rm on / or ~, git push --force, git reset --hard, and curl piped to sh by exiting with code 2, which cancels the action and returns the error message to Claude so it can revise its plan. The hooks are configured in .claude/settings.json and verified by piping sample tool_input JSON into the script to confirm exit codes 2 and 0. A companion hook blocks reads and writes to .env, *.pem and id_rsa files while allowing .env.example.

by read1 min views5 publishedOct 28, 2026

Los agentes de código son increíbles… hasta que ejecutan git reset --hard sobre una tarde de trabajo sin commitear.

Claude Code tiene una función poco conocida para evitarlo: los hooks.

Un script que Claude Code ejecuta antes o después de usar una herramienta. Si un hook PreToolUse

termina con código de salida 2, la acción se cancela y Claude recibe tu mensaje de error, así que corrige su plan solo.

.claude/hooks/bloquear.py:

#!/usr/bin/env python3
import json, re, sys
PATRONES = [
    (r"\brm\s+-[a-zA-Z]*[rR][a-zA-Z]*f?\s+(/|~|\$HOME|\*)(\s|$)", "rm recursivo sobre /, ~ o *"),
    (r"\bgit\s+push\b.*(--force\b|-f\b)(?!-with-lease)", "git push --force"),
    (r"\bgit\s+reset\s+--hard\b", "git reset --hard"),
    (r"(curl|wget)[^|]*\|\s*(sudo\s+)?(ba|z)?sh\b", "curl | sh"),
]
cmd = (json.load(sys.stdin).get("tool_input") or {}).get("command", "")
for p, motivo in PATRONES:
    if re.search(p, cmd, re.I):
        print(f"Bloqueado: {motivo}", file=sys.stderr); sys.exit(2)

.env Mismo patrón, pero mirando tool_input.file_path en las herramientas Read|Edit|Write

y bloqueando .env, *.pem, id_rsa… (y permitiendo .env.example).

.claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Bash",
        "hooks": [{ "type": "command", "command": "python3 \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/bloquear.py" }] }
    ]
  }
}

Reinicia Claude Code y escribe /hooks para comprobar que está activo.

echo '{"tool_input":{"command":"rm -rf ~"}}' | python3 .claude/hooks/bloquear.py; echo $?   # 2
echo '{"tool_input":{"command":"npm test"}}'  | python3 .claude/hooks/bloquear.py; echo $?   # 0

Los dos hooks probados y una skill de code review en español, gratis: https://github.com/carlosp5751258-lgtm/claude-code-es.

Si quieres la configuración completa (6 skills, comandos /plan y /pr, plantillas CLAUDE.md, autoformato),

está el Kit Claude Code en Español.

── more in #ai-agents 4 stories · sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/como-evitar-que-clau…] indexed:0 read:1min 2026-10-28 · —