Claude Code Society, home

Learn, lesson 6 of 6

Guardrails: permissions and hooks

You decide how much Claude Code does without asking. A permission mode sets the overall level, rules allow or block particular actions, and a hook is a small script of yours that runs before an action and can stop it cold.

What you're doing

You'll learn the permission modes and pick one, add a rule that keeps Claude away from a secrets file, and write a hook that refuses git push --force, a command that can overwrite the history on GitHub. Then you'll watch the hook fire.

The modes

The bar at the bottom of Claude Code shows the mode you're in. Press Shift + Tab to move between them.

Manual
Claude asks before most edits, commands and network requests. Only reading runs without asking. Its setting name is default.
Accept edits
File edits and common file commands like mkdir and mv run without asking. Other commands still ask.
Plan
Claude reads and proposes a plan but doesn't edit your files until you approve it (lesson 2).
Auto
No routine questions. A separate model checks each action before it runs and blocks anything that goes beyond what you asked. Recent versions start here.
Bypass permissions
Skips the questions altogether. Only for throwaway containers or virtual machines, never your own laptop.

Auto mode cuts down on questions, but the official docs say plainly that it doesn't guarantee safety. Rules and hooks are how you make some things never happen, whatever mode you're in.

Steps

  1. Start in Manual mode for this lesson

    Terminal
    cd study-timer
    claude --permission-mode default

    Ask for any small change and notice that Claude now stops and asks first. Press Shift + Tab a few times and watch the bar change. To make Manual your default on every project, put "permissions": { "defaultMode": "default" } in ~/.claude/settings.json.

  2. Add a deny rule

    Project settings live in .claude/settings.json. Create it with this rule, which stops Claude reading a .env file in your project (where secrets often live):

    .claude/settings.json
    {
      "permissions": {
        "deny": [
          "Read(./.env)"
        ]
      }
    }

    Rules come in three kinds: allow, ask and deny. Deny is checked first and wins over everything else. Type /permissions to see every rule and which file it came from.

  3. Learn what a hook gets

    A PreToolUse hook runs just before Claude uses a tool. Claude Code hands your script a short piece of JSON describing the action. For a terminal command, the command itself is in tool_input.command. If your script exits with code 2, the action is blocked, and whatever you printed to the error stream goes back to Claude as the reason.

  4. Write the hook script

    Make a .claude/hooks folder and save the script for your system in it.

    macOS or Linux: .claude/hooks/block-force-push.sh
    #!/bin/bash
    # Stops git push --force before Claude Code runs it.
    COMMAND=$(jq -r '.tool_input.command // empty')
    
    if echo "$COMMAND" | grep -q "push --force"; then
      echo "Blocked: force-pushing isn't allowed in this project" >&2
      exit 2
    fi
    
    exit 0
    Windows: .claude/hooks/block-force-push.ps1
    $callInput = [Console]::In.ReadToEnd() | ConvertFrom-Json
    $command = $callInput.tool_input.command
    
    if ($command -match 'push --force') {
      @{
        hookSpecificOutput = @{
          hookEventName = "PreToolUse"
          permissionDecision = "deny"
          permissionDecisionReason = "Blocked: force-pushing isn't allowed in this project"
        }
      } | ConvertTo-Json
    } else {
      exit 0
    }

    The Windows version blocks by answering with a "deny" decision instead of exit code 2. Both are documented ways to block. Each one only catches that exact spelling, which is fine for practice; for a hard rule on a real project, add a deny rule as well.

  5. Get it ready to run

    On macOS and Linux the script needs jq and permission to run:

    macOS (with Homebrew)
    brew install jq
    chmod +x .claude/hooks/block-force-push.sh
    Ubuntu or Debian
    sudo apt install jq
    chmod +x .claude/hooks/block-force-push.sh

    Windows needs nothing extra; the settings below run the script through PowerShell.

  6. Register the hook

    Add a hooks section next to permissions in .claude/settings.json. Use the version for your system.

    macOS or Linux: .claude/settings.json
    {
      "permissions": {
        "deny": [
          "Read(./.env)"
        ]
      },
      "hooks": {
        "PreToolUse": [
          {
            "matcher": "Bash",
            "hooks": [
              {
                "type": "command",
                "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-force-push.sh"
              }
            ]
          }
        ]
      }
    }
    Windows: .claude/settings.json
    {
      "permissions": {
        "deny": [
          "Read(./.env)"
        ]
      },
      "hooks": {
        "PreToolUse": [
          {
            "matcher": "Bash|PowerShell",
            "hooks": [
              {
                "type": "command",
                "command": "powershell.exe",
                "args": [
                  "-NoProfile",
                  "-ExecutionPolicy",
                  "Bypass",
                  "-File",
                  "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-force-push.ps1"
                ]
              }
            ]
          }
        ]
      }
    }

    JSON is strict: no trailing commas and no comments. Inside Claude Code, type /hooks; your hook should be listed under PreToolUse. If it isn't there after a few seconds, restart the session.

  7. Test the script by hand (macOS and Linux)

    Feed it the same kind of JSON Claude Code would, and check the exit code:

    Terminal
    echo '{"tool_name":"Bash","tool_input":{"command":"git push --force"}}' | ./.claude/hooks/block-force-push.sh
    echo $?

    You should see your Blocked: message and then 2. Try it again with git status as the command: no message, and 0.

  8. Watch it fire

    Type into Claude Code
    I'm testing a safety hook. Run exactly this command: git push --force

    Claude Code stops the command before it runs and shows your reason. The push never happens. Hooks run before the permission mode is even considered, so this holds in every mode.

  9. Commit your guardrails

    Commit .claude/settings.json and the .claude/hooks folder so they travel with the project. Settings just for you go in .claude/settings.local.json, which Git ignores once Claude Code has saved a setting to it.

Exercise

Proof of done

Set your permission mode, add one hook that stops a command you never want run, and prove it fires once.

Want a different command? Ask Claude Code to change the script, then run the hand test and the live test again.

You're done when

  • You can name the mode you work in and switch modes with Shift + Tab.
  • /permissions shows your deny rule, and /hooks lists your hook under PreToolUse.
  • Claude Code tried to force-push and your hook blocked it with your message.
  • The settings file and the hook are committed.

Sources

Read on October 4, 2026.

  1. Choose a permission mode: the modes and what runs in each, Shift + Tab, --permission-mode, defaultMode, auto mode as the starting mode on recent versions and its safety warning, bypass mode for isolated machines only.
  2. Configure permissions: allow, ask and deny rules, deny checked first, Read(./.env), /permissions, .claude/settings.local.json.
  3. Automate actions with hooks: hook input, exit code 2 and the error stream, the jq requirement, /hooks, hook locations, hooks running before the permission mode, testing a script by piping JSON into it.
  4. Hooks reference: the Windows PowerShell version with a "deny" decision and the Bash|PowerShell matcher.