LLI Wiki — My Digital Knowledge Base

Guard-rails for Claude Code: hooks, permissions and read-only access to production

Allow/ask/deny permissions, hooks that warn about secrets and forbidden patterns, free reads and approved writes for MCP tools, never running SQL against production from the AI.

Giving an AI assistant access to a DBA repository is only reasonable if the dangerous things are made hard, not just discouraged. Instructions in CLAUDE.md are advice; a model can misread or forget them. What I rely on for the things that matter is a layered set of guard-rails: permissions, hooks, tool scoping and a few absolute rules.

The principle: advice, warning, block

Level Mechanism Use for
Advice a sentence in CLAUDE.md or a rule conventions, style
Warning a hook that reports and lets Claude fix secrets in a file, forbidden patterns
Approval an ask permission force push, hard reset, anything reversible with effort
Block a deny permission or a blocking hook scripts that change production, destructive commands

The more irreversible the action, the further down the table it belongs.

Permissions: allow, ask, deny

The shared settings.json has three lists. I keep the committed one conservative.

{
  "permissions": {
    "allow": [
      "Bash(git status *)",
      "Bash(git diff *)",
      "Bash(git log *)",
      "PowerShell(Invoke-ScriptAnalyzer *)",
      "PowerShell(Invoke-Pester *)"
    ],
    "ask": [
      "Bash(git push *--force*)",
      "Bash(git reset --hard*)"
    ],
    "deny": [
      "Bash(*deploy-to-prod.ps1*)",
      "PowerShell(*-File *deploy-to-prod.ps1*)"
    ]
  }
}

Points I learned the hard way:

Never run SQL against production from the AI

This is the strictest rule I have:

Read-only versus write tools through MCP

The same split applies to MCP servers. I connect Grafana through MCP to both a dev and a prod instance:

Hooks: warn about secrets and forbidden patterns

A hook is a command run on an event. A PostToolUse hook that exits with code 2 sends its error output back to Claude as feedback, so the model corrects its own edit. A PreToolUse hook that exits 2 blocks the call.

Here is a generic pre-write check for secrets in configuration files (my own sketch):

# pre-write-secret-check.ps1 : block obvious secrets written into config files
$event = [Console]::In.ReadToEnd() | ConvertFrom-Json
$path  = $event.tool_input.file_path
$text  = "$($event.tool_input.content)$($event.tool_input.new_string)"

if ($path -notmatch '\.(json|ya?ml|config|xml|ini|env|psd1)$') { exit 0 }

$patterns = @(
    '(?i)(password|pwd|secret|api_?key)\s*[:=]\s*["'']?[^\s"'',;<{$]{8,}',
    '(?i)accountkey=[A-Za-z0-9+/=]{20,}',
    '(?i)(server|data source)=[^;]+;.*password='
)
foreach ($p in $patterns) {
    if ($text -match $p) {
        [Console]::Error.WriteLine('Possible secret in a config file. Use a Key Vault reference instead.')
        exit 2
    }
}
exit 0

I register it under PreToolUse with a matcher on the edit and write tools. A softer version of the same idea, a warning rather than a block, can be written as a plugin rule (a Markdown file with a regex on the file path and on the content). I use warnings for style patterns such as a forbidden logging cmdlet, and blocks for secrets.

My other hook is a PostToolUse linter that runs the PowerShell analyser on each edited script and returns findings with exit code 2, so Claude fixes lint errors without me asking. Two safety details: it exits 0 if the analyser is missing, and it exits 0 on a parse error, so a broken hook never blocks work.

Lessons learned

Mastodon LLIMastodon SocialFosstodon