Hey guys I’ve just started using claude code and I’ve learnt that using hooks are the safest way to run it in bypass permissions mode. I know nothing about code honestly and welcome any feedback. I’m sure this is a stupid post so your constructive criticism js welcome. Here are the hooks I’ve got in place. Are these okay? Just using claude for uni and business work
Claude Code guardrails — runs as a PreToolUse hook.
Reads tool input JSON from stdin; emits a JSON deny decision if it matches
a dangerous pattern. Silent (exit 0) otherwise.
$ErrorActionPreference = ‘Stop’ try $raw = [Console]::In.ReadToEnd() if (-not $raw) { exit 0 } $payload = $raw catch {
Never block on parse failure — let the harness proceed normally.
exit 0 } $tool = $payload.tool_name $cmd = ‘’ $path = ‘’ if ($tool -eq ‘Bash’ -or $tool -eq ‘PowerShell’) { $cmd = [string]$payload.tool_input.command } elseif ($tool -eq ‘Write’ -or $tool -eq ‘Edit’ -or $tool -eq ‘NotebookEdit’) { $path = [string]$payload.tool_input.file_path } else { exit 0 }
Derive the current user’s home dir as a regex fragment that accepts \ or /.
$userHomeRx = [regex]::Escape($env:USERPROFILE) -replace ‘\\’, ‘[\/]’ function Deny([string]$reason) $out = @{ hookSpecificOutput = @{ hookEventName = ‘PreToolUse’ permissionDecision = ‘deny’ permissionDecisionReason = “GUARDRAIL: $reason (Edit ~/.claude/hooks/guardrails.ps1 to change.)” } }
— Command-string patterns (Bash / PowerShell tools) —
$cmdRules = @( @ R = ‘rm\s+(-[a-zA-Z][rRfF][a-zA-Z]\s+)+’; Why = ‘rm -rf / -r / -f detected.’ }, @{ R = ‘(?i)Remove-Item\b(?=[\s\S]-Recurse)(?=[\s\S]-Force)’; Why = ‘Remove-Item -Recurse -Force detected.’ }, @{ R = ‘(?i)\bdel\s+/[sSqQ]’; Why = ‘del /s recursive delete detected.’ }, @{ R = ‘(?i)\brd\s+/s’; Why = ‘rd /s recursive directory remove detected.’ }, @{ R = ‘(?i)\brmdir\s+/s’; Why = ‘rmdir /s recursive directory remove detected.’ }, @{ R = 'git\s+push\b[\s\S]*?(\s-f\b, @ R = ‘git\s+reset\b[\s\S]?–hard\b’; Why = ‘git reset --hard blocked.’ }, @{ R = 'git\s+clean\b[\s\S]?-[a-zA-Z]f’; Why = ‘git clean -f blocked.’ }, @{ R = 'git\s+branch\b[\s\S]?-D\b’; Why = ‘git branch -D blocked.’ }, @{ R = ‘git\s+push\b[\s\S]*?–delete\b’; Why = ‘git remote branch delete blocked.’ }, @{ R = ‘–no-verify\b’; Why = ‘–no-verify blocked (skips hooks).’ }, @{ R = ‘–no-gpg-sign\b’; Why = ‘–no-gpg-sign blocked.’ }, @{ R = 'git\s+stash\s+(drop, @ R = ‘git\s+config\b[\s\S]*?–global\b’; Why = ‘git config --global blocked.’ }, @{ R = 'git\s+filter-(branch, @ R = ‘git\s+update-ref\s±d\b’; Why = ‘git update-ref -d blocked.’ }, @{ R = '(?i)\breg\s+(add, @ R = '(?i)Remove-Item\b[\s\S]*?HK(LM, @ R = '(?i)(Set, @ R = '(?i)New-Item\b[\s\S]*?HK(LM, @ R = ‘(?i)\bStop-Service\b’; Why = ‘Stop-Service blocked.’ }, @{ R = '(?i)\b(Set, @{ R = ‘(?i)Stop-Process\b[\s\S]*?-Force\b’; Why = ’ submitted by /u/bluemeanies1
Originally posted by u/bluemeanies1 on r/ClaudeCode
