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:
- Deny on every way to launch it. A script can be called with
&, dot-sourcing,-File,Start-ProcessorInvoke-Expression, from PowerShell or from Bash. One pattern per shape. - Approvals are personal. My own "always allow" choices land in the local settings file, never committed, so the team baseline stays read-only.
- Settings are read from the project root. A session started inside a sub-folder does not get the root deny list. Start at the root.
Never run SQL against production from the AI
This is the strictest rule I have:
- Claude writes and reviews SQL; it does not execute DDL or DML on a production server. Production changes go through the normal change process, run by a human.
- Read-only diagnostic queries are acceptable, but I prefer that Claude hands me the query and I run it, or that it uses a read-only login.
- Validation uses mocks and tests: Pester tests that mock the data-access and destructive cmdlets and assert that they are called zero times when they must not run.
- Scripts that can harm (drop, copy, mass update) are on the deny list and have a
-WhatIfpath. - No connection string or credential in a prompt, a brief or a config file. Key Vault and managed identity.
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:
- Read freely. Searching dashboards, reading panel queries, running metric or log queries: pre-approved in
allow. - Write only with explicit approval. Updating a dashboard, creating an alert rule, a silence or an incident, changing a datasource: never in the allow list. On production, each write waits for my "yes".
- Better still, use a token with the minimum role. A viewer token makes writing impossible; an editor token is used only when I intend to write.
- Pipelines that write to a monitoring tool keep a dry-run default.
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
- Hooks and deny rules, not prose. If the cost of a mistake is high, enforce it in code.
- Fail open on tooling errors, fail closed on policy. A missing linter must not stop you; a matching secret must.
- Test your guard-rails. Ask Claude to do the forbidden thing in a throwaway folder and check that it is stopped.
- Rotate any secret that was written, even briefly. A hook warns you, but the value may already be in a log or a diff.
- Be honest about the limit. These measures reduce risk; they do not replace least-privilege accounts and review.