Confirm production kubectl/helm/IAM mutations before they run (also inside aws-vault exec), require an explicit kube context, and hand interactive aws-vault…

Six Claude Code mods that guard risky actions and cut down on babysitting: confirm production changes, keep secrets out of the model's context, start branches from the right base, hold Claude to "just look" when you ask for it, tell you when CI finishes, and let you approve a command auto mode blocked with one button.
They started as one person's setup and stay opinionated, but every personal value is a plugin option with a sensible default.
[!WARNING] Mods run inside Claude Code with your permissions and are not sandboxed. These guards parse shell commands heuristically: they catch the common forms Claude writes, not every way a command can be spelled. Treat them as a safety net for mistakes, not a security boundary. Read the source before installing.
| Mod | What it does |
|---|---|
prod-guard | Holds mutating kubectl/helm commands against production contexts and aws iam writes until you confirm the exact command; a confirmed call skips the auto-mode classifier, and one the classifier blocks anyway goes into your prompt box as ! …. Refuses mutating kubectl without an explicit --context. Judges the command inside aws-vault exec <profile> -- … by the same rules, and puts aws-vault login or a bare aws-vault exec <profile> (an interactive shell) in your prompt box as ! … instead of running it (left out when the box holds a draft, so the draft is kept). |
secret-guard | Blocks Bash and Read access to .env files (not .env.example) and to ~/.claude.json. Masks JWTs, AWS/GitHub/Slack/Anthropic/OpenAI/Google keys and private key blocks in Bash and Read output before Claude sees them. |
branch-base | Rewrites git checkout -b, git switch -c and git worktree add with no start point to start from origin/<base> after a fetch, and adds --base <base> to gh pr create. When a repo has no such branch, asks you which base to use (remembered per repo until reload). |
read-only-lock | After a look-only prompt ("just investigate", "plan first, don't change anything", or Korean equivalents such as "확인만 해"), blocks file edits and mutating commands and shows a one-line band with an Unlock button. "go ahead", "apply it" or the button unlocks. |
deploy-watch | After git push or gh pr create, polls GitHub Actions for that commit every 30 s; after gh pr merge, for the merge commit GitHub made on the base branch, where deploy workflows run (an auto-merge is followed until it lands); after gh workflow run, for the dispatched workflow. Raises a toast and a notification when the runs finish and sends the result to Claude as a message that starts a turn (a push that started no runs is only noted, without starting one), so Claude reports it and picks up work that was waiting on CI without watching CI itself. /ci opens a pane with the runs. |
approve-blocked | When the auto-mode classifier blocks a Bash command, asks you Run or Don't run in a dialog instead of leaving Claude to tell you to run it with !. Run runs the call again past the classifier (a permissions.deny rule still stands); if the classifier blocks it again, the command goes into your prompt box as ! … so Enter runs it. Don't run tells Claude not to hand the command to you. A call prod-guard already had you confirm is not asked about twice. |
Requires Claude Code v2.1.287 or later (mods are on by default). At the prompt of a terminal session:
/plugin install prod-guard --marketplace blackironj/claude-mods
Answer y to add the marketplace, then pick a scope (user scope loads it in every session). Repeat for each mod you want: secret-guard, branch-base, read-only-lock, deploy-watch, approve-blocked.
From a shell:
claude plugin install prod-guard --marketplace blackironj/claude-mods
Disable or uninstall any mod from the Installed tab in /plugin.
Set them on the install screen, or change them later in /config.
| Mod | Option | Default | Meaning |
|---|---|---|---|
prod-guard | prodContexts | prod | Comma-separated kube contexts whose mutations need confirmation (also matched against helm --kube-context). |
prod-guard | requireContext | true | Refuse mutating kubectl commands without --context. |
branch-base | baseBranch | dev | Remote branch new branches start from and PRs target. |
deploy-watch needs the GitHub CLI signed in; branch-base needs git.claude -p and the VS Code chat panel the guards still run but nothing is drawn.read-only-lock decides from keywords, so it can lock or stay unlocked when you didn't mean it to; the band shows when it is on.secret-guard also blocks commands that create a .env (for example cp .env.example .env); run those yourself. Masking covers the token shapes listed above, not arbitrary passwords.branch-base does not affect worktrees Claude Code creates on its own (EnterWorktree).approve-blocked turns every auto-mode block of a Bash command into a question for you, so pressing Run overrides the classifier for that one call: read the command first. Other tools the classifier blocks are left as they are.prod-guard lets a call you confirmed run even where a permission rule or the auto-mode classifier would stop it, and its aws-vault handoff puts a command Claude wrote into your prompt box: a ! command runs in your shell with no permission check, so read it before pressing Enter.prod-guard lets aws-vault exec <profile> -- … run in Claude's shell, which has no terminal for prompts. When the session needs an MFA code or a key-store passphrase, the command fails; run one aws-vault command yourself with ! to refresh the session.Each mod is a plugin folder (.claude-plugin/plugin.json, hooks/hooks.json, a TypeScript hooks module, tests). There is no build step: Claude Code loads the .ts/.tsx sources directly. See CLAUDE.md for the layout, checks and conventions.
claude plugin validate prod-guard # what the module hooks and calls, and anything the engine would refuse
claude plugin test prod-guard # runs prod-guard/tests/*.test.ts against the engine
claude --plugin-dir ./prod-guard # a session with your working copy loaded and hot-reloaded
hooks/register.ts 80 lines1import type { EngineInterface, Register } from 'claude-code'
2
3import { classifierBlock } from './blocked'
4import { classify, type Policy } from './classify'
5
6const MAX_SHOWN = 600
7
8function policyOf(options: Readonly<Record<string, unknown>>): Policy {
9 const contexts = String(options.prodContexts ?? 'prod')
10 return {
11 prodContexts: contexts.split(',').map(c => c.trim()).filter(c => c !== ''),
12 requireContext: options.requireContext !== false,
13 }
14}
15
16/** Puts `! <command>` in an empty prompt box and tells the model to hand the command to the user. */
17async function handOff($: EngineInterface, command: string, why: string) {
18 // Fill only an empty prompt box: replacing would throw away what the user was typing.
19 const isFilled = (await $.prompt.read()).text.trim() === '' && (await $.prompt.fill({ text: `! ${command}`, mode: 'replace' })).isFilled
20 $.ui.toast(isFilled ? 'prod-guard: review the command in your prompt box, then press Enter to run it' : 'prod-guard: run the command yourself with !')
21 return {
22 deny:
23 `prod-guard: ${why}, so it was handed to the user` +
24 (isFilled ? ' (placed in their prompt box as a `!` command)' : '') +
25 '. Do not retry or work around it. End your turn now with no further tool calls, and show the user ' +
26 'this exact line to run, in a code block, `!` included:\n\n' +
27 `! ${command}\n\n` +
28 'Its output lands in this conversation when they run it; a `!` command typed while you are still working does not run.',
29 }
30}
31
32export const register: Register = (on, options) => {
33 const policy = policyOf(options)
34 // Commands the user confirmed, while they run, so tool.check can let them past the
35 // auto-mode classifier (keyed by command so approve-blocked's tool.check query sees them).
36 const confirmed = new Set<string>()
37
38 on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
39 const verdict = classify(e.command, policy)
40
41 switch (verdict.kind) {
42 case 'pass':
43 return next(e)
44 case 'deny':
45 return { deny: `prod-guard: ${verdict.why}.` }
46 case 'handoff':
47 return handOff($, e.command, verdict.why)
48 case 'confirm': {
49 const shown = e.command.length > MAX_SHOWN ? `${e.command.slice(0, MAX_SHOWN)}…` : e.command
50 const answer = await $.ui.ask(`${verdict.why}. Run this command?\n\n${shown}`, {
51 header: 'prod-guard',
52 options: ['Run', 'Cancel'],
53 })
54 if (answer !== 'Run') {
55 return { deny: `prod-guard: the user declined (${verdict.why})${answer === 'Cancel' ? '' : `: ${answer}`}.` }
56 }
57 confirmed.add(e.command)
58 try {
59 const result = await next(e)
60 const reason = classifierBlock(result)
61 return reason === undefined ? result : await handOff($, e.command, `auto mode still blocked it after you confirmed (${reason})`)
62 } finally {
63 confirmed.delete(e.command)
64 }
65 }
66 }
67 }).catch(($, e, next) =>
68 next.called ? next(e) : { deny: 'prod-guard: the guard failed or the question was dismissed; the command did not run.' },
69 )
70
71 on('tool.check', { tool: 'Bash' }, async ($, e, next) => {
72 const verdict = await next(e)
73 const command = (e.input as { command?: string }).command ?? ''
74 // Only the classifier's question is answered for the user: a settings deny rule still stands.
75 return confirmed.has(command) && verdict.decision === 'ask'
76 ? { decision: 'allow', reason: 'prod-guard: confirmed by the user' }
77 : verdict
78 })
79}
80hooks/blocked.ts 11 lines1import type { ToolCallResult } from 'claude-code'
2
3const CLASSIFIER_BLOCK = /denied by the Claude Code auto mode classifier/
4
5/** The classifier's reason when a call's result is an auto-mode block, else undefined. */
6export function classifierBlock(result: ToolCallResult): string | undefined {
7 const text = result.deny ?? (result.isError === true ? result.text : undefined)
8 if (text === undefined || !CLASSIFIER_BLOCK.test(text)) return undefined
9 return /Reason: (.+?)\. If you have/s.exec(text)?.[1] ?? 'no reason given'
10}
11hooks/classify.ts 102 lines1import { option, positionals, program, segments } from './shell'
2
3export type Policy = {
4 /** Kube contexts whose mutations need confirmation (also helm --kube-context). */
5 prodContexts: readonly string[]
6 /** Refuse kubectl mutations that name no --context. */
7 requireContext: boolean
8}
9
10export const DEFAULT_POLICY: Policy = { prodContexts: ['prod'], requireContext: true }
11
12export type Verdict =
13 | { kind: 'pass' }
14 | { kind: 'confirm'; why: string }
15 | { kind: 'handoff'; why: string }
16 | { kind: 'deny'; why: string }
17
18const KUBECTL_VALUED = [
19 '--context', '-n', '--namespace', '--kubeconfig', '--cluster', '--user', '-s', '--server',
20 '--token', '--as', '-c', '--container', '-o', '--output', '-f', '--filename', '-l', '--selector',
21]
22const KUBECTL_MUTATING = new Set([
23 'apply', 'create', 'delete', 'edit', 'patch', 'replace', 'scale', 'autoscale', 'drain', 'cordon',
24 'uncordon', 'taint', 'label', 'annotate', 'set', 'expose', 'run', 'exec', 'cp', 'debug',
25])
26const ROLLOUT_READS = new Set(['status', 'history'])
27const HELM_MUTATING = new Set(['install', 'upgrade', 'uninstall', 'delete', 'rollback'])
28const HELM_VALUED = ['--kube-context', '-n', '--namespace', '--kubeconfig', '--kube-apiserver', '--kube-as-user', '--kube-token', '--registry-config', '--repository-cache', '--repository-config', '--burst-limit', '--qps']
29const IAM_WRITE = /^(create|put|attach|detach|delete|update|add|remove|tag|untag|set|upload|change|reset|enable|deactivate)-/
30
31function kubectl(ws: string[], policy: Policy): Verdict {
32 const [verb = '', sub = ''] = positionals(ws, 1, KUBECTL_VALUED)
33 const isMutating = KUBECTL_MUTATING.has(verb) || (verb === 'rollout' && !ROLLOUT_READS.has(sub))
34 if (!isMutating) return { kind: 'pass' }
35 const context = option(ws, '--context')
36 if (context === undefined) {
37 return policy.requireContext
38 ? { kind: 'deny', why: `kubectl ${verb} has no explicit --context; add --context=<context> as the first argument` }
39 : { kind: 'pass' }
40 }
41 return policy.prodContexts.includes(context)
42 ? { kind: 'confirm', why: `kubectl ${verb}${verb === 'rollout' ? ` ${sub}` : ''} on ${context}` }
43 : { kind: 'pass' }
44}
45
46function helm(ws: string[], policy: Policy): Verdict {
47 // Global flags may come before the subcommand: `helm --kube-context prod -n ns uninstall x`.
48 const [sub = ''] = positionals(ws, 1, HELM_VALUED)
49 if (!HELM_MUTATING.has(sub)) return { kind: 'pass' }
50 const context = option(ws, '--kube-context')
51 if (context !== undefined && policy.prodContexts.includes(context)) return { kind: 'confirm', why: `helm ${sub} on ${context}` }
52 if (context === undefined) return { kind: 'confirm', why: `helm ${sub} without --kube-context (target cluster unknown)` }
53 return { kind: 'pass' }
54}
55
56function aws(ws: string[]): Verdict {
57 const [service, op] = positionals(ws, 1, ['--profile', '--region', '--output', '--query', '--endpoint-url'])
58 return service === 'iam' && op !== undefined && IAM_WRITE.test(op) ? { kind: 'confirm', why: `aws iam ${op}` } : { kind: 'pass' }
59}
60
61const VAULT_VALUED = new Set([
62 '--backend', '--prompt', '--keychain', '--secret-service-collection', '--pass-dir', '--pass-cmd',
63 '--pass-prefix', '--file-dir', '-d', '--duration', '--region', '-t', '--mfa-token',
64])
65
66// `aws-vault exec <profile> -- <cmd>` runs non-interactively on a cached session, so the
67// wrapped command is judged like any other; only an interactive shell or login is handed off.
68function awsVault(ws: string[], policy: Policy): Verdict {
69 if (ws[1] === 'login') return { kind: 'handoff', why: 'aws-vault login opens the AWS console in your browser' }
70 if (ws[1] !== 'exec') return { kind: 'pass' }
71 let i = 2
72 for (let w = ws[i]; w !== undefined && w.startsWith('-') && w !== '--'; w = ws[i]) {
73 if (w === '--help') return { kind: 'pass' }
74 i += !w.includes('=') && VAULT_VALUED.has(w) ? 2 : 1
75 }
76 const rest = ws.slice(i + 1)
77 const inner = rest[0] === '--' ? rest.slice(1) : rest
78 return inner.length > 0
79 ? judge(program(inner), policy)
80 : { kind: 'handoff', why: 'aws-vault exec without a command opens an interactive shell' }
81}
82
83function judge(ws: string[], policy: Policy): Verdict {
84 if (ws[0] === 'kubectl') return kubectl(ws, policy)
85 if (ws[0] === 'helm') return helm(ws, policy)
86 if (ws[0] === 'aws') return aws(ws)
87 if (ws[0] === 'aws-vault') return awsVault(ws, policy)
88 return { kind: 'pass' }
89}
90
91const RANK = { pass: 0, confirm: 1, handoff: 2, deny: 3 } as const
92
93/** The strictest verdict over every simple command in a Bash command line. */
94export function classify(command: string, policy: Policy = DEFAULT_POLICY): Verdict {
95 let worst: Verdict = { kind: 'pass' }
96 for (const seg of segments(command)) {
97 const v = judge(program(seg.words), policy)
98 if (RANK[v.kind] > RANK[worst.kind]) worst = v
99 }
100 return worst
101}
102hooks/shell.ts 144 lines1// Minimal shell splitting for guards: good enough to find simple commands
2// in a Bash tool call, not a full shell parser. Quotes are respected;
3// substitutions ($(...), backticks) are not expanded.
4
5export type Segment = {
6 /** The segment's source text, trimmed. */
7 text: string
8 /** Offset of `text` in the full command. */
9 start: number
10 /** Words with quotes removed. */
11 words: string[]
12}
13
14/** Splits a command line at unquoted `&&`, `||`, `;`, `|`, `&` and newlines. */
15export function segments(command: string): Segment[] {
16 const out: Segment[] = []
17 let segStart = 0
18 let quote: '"' | "'" | null = null
19
20 const push = (end: number) => {
21 const raw = command.slice(segStart, end)
22 const lead = raw.length - raw.trimStart().length
23 const text = raw.trim()
24 if (text !== '') out.push({ text, start: segStart + lead, words: words(text) })
25 }
26
27 for (let i = 0; i < command.length; i++) {
28 const c = command.charAt(i)
29 if (quote !== null) {
30 if (c === '\\' && quote === '"') i++
31 else if (c === quote) quote = null
32 continue
33 }
34 if (c === '\\') {
35 i++
36 continue
37 }
38 if (c === '"' || c === "'") {
39 quote = c
40 continue
41 }
42 const two = command.slice(i, i + 2)
43 if (two === '&&' || two === '||') {
44 push(i)
45 segStart = i + 2
46 i++
47 } else if (c === '&' && /[<>]/.test(command.charAt(i - 1) + command.charAt(i + 1))) {
48 // fd duplication or `&>`, not a separator
49 continue
50 } else if (c === ';' || c === '|' || c === '&' || c === '\n') {
51 push(i)
52 segStart = i + 1
53 }
54 }
55 push(command.length)
56 return out
57}
58
59/** Splits one simple command into words, removing quotes. */
60export function words(text: string): string[] {
61 const out: string[] = []
62 let cur = ''
63 let has = false
64 let quote: '"' | "'" | null = null
65
66 for (let i = 0; i < text.length; i++) {
67 const c = text.charAt(i)
68 if (quote !== null) {
69 if (c === quote) quote = null
70 else if (c === '\\' && quote === '"' && i + 1 < text.length) cur += text.charAt(++i)
71 else cur += c
72 continue
73 }
74 if (c === '"' || c === "'") {
75 quote = c
76 has = true
77 } else if (c === '\\' && i + 1 < text.length) {
78 cur += text.charAt(++i)
79 has = true
80 } else if (/\s/.test(c)) {
81 if (has) out.push(cur)
82 cur = ''
83 has = false
84 } else {
85 cur += c
86 has = true
87 }
88 }
89 if (has) out.push(cur)
90 return out
91}
92
93const WRAPPERS = new Set(['sudo', 'env', 'time', 'command', 'exec'])
94
95/** Drops leading `VAR=value` assignments and `sudo`/`env`/`time` wrappers. */
96export function program(ws: string[]): string[] {
97 const i = ws.findIndex(w => !/^[A-Za-z_][A-Za-z0-9_]*=/.test(w) && !WRAPPERS.has(w))
98 return i === -1 ? [] : ws.slice(i)
99}
100
101/**
102 * The value of a long or short option in `ws`, written `--name=value` or
103 * `--name value`; undefined when absent.
104 */
105export function option(ws: string[], ...names: string[]): string | undefined {
106 for (const [i, w] of ws.entries()) {
107 for (const name of names) {
108 if (w === name) return ws[i + 1]
109 if (name.startsWith('--') && w.startsWith(name + '=')) return w.slice(name.length + 1)
110 }
111 }
112 return undefined
113}
114
115/**
116 * Positional words after `from`, skipping flags and redirections; flags in
117 * `valued` consume the next word unless written `--flag=value`.
118 */
119export function positionals(ws: string[], from: number, valued: readonly string[]): string[] {
120 const out: string[] = []
121 let skip = false
122 for (const [i, w] of ws.entries()) {
123 if (i < from) continue
124 if (skip) {
125 skip = false
126 continue
127 }
128 if (w === '--') {
129 out.push(...ws.slice(i + 1))
130 break
131 }
132 if (/^\d*(>>?|<|&>)/.test(w)) {
133 skip = /^\d*(>>?|<|&>)$/.test(w)
134 continue
135 }
136 if (w.startsWith('-') && w !== '-') {
137 skip = !w.includes('=') && valued.includes(w)
138 continue
139 }
140 out.push(w)
141 }
142 return out
143}
144