openclaw - 💡(How to fix) Fix Feature: Pre/Post Tool Use Hooks (programmable safety rails) [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#60943Fetched 2026-04-08 02:45:21
View on GitHub
Comments
2
Participants
3
Timeline
5
Reactions
1
Timeline (top)
commented ×2subscribed ×2mentioned ×1

Add configurable shell-script hooks that run before and after every tool execution, allowing users to programmatically approve, deny, or augment tool calls.

Error Message

  • Any other exit code = Warn but allow (logged, execution continues)
  1. If any hook exits 2 → tool call denied, error returned to model
  2. If PostToolUse hook exits 2 → result marked as error

Root Cause

Add configurable shell-script hooks that run before and after every tool execution, allowing users to programmatically approve, deny, or augment tool calls.

Code Example

hooks:
  preToolUse:
    - ./hooks/validate-exec.sh
    - ./hooks/log-tool-use.sh
  postToolUse:
    - ./hooks/audit-external.sh

---

#!/bin/bash
# Deny rm -rf commands
if [ "$HOOK_TOOL_NAME" = "exec" ]; then
  echo "$HOOK_TOOL_INPUT" | grep -q 'rm -rf /' && {
    echo "Blocked: destructive rm command"
    exit 2
  }
fi
exit 0

---

#!/bin/bash
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) $HOOK_EVENT $HOOK_TOOL_NAME" >> ~/.openclaw/audit.log
exit 0
RAW_BUFFERClick to expand / collapse

Summary

Add configurable shell-script hooks that run before and after every tool execution, allowing users to programmatically approve, deny, or augment tool calls.

Motivation

Claude Code's architecture (and the Claw Code clean-room rewrite) implements a hook system where shell commands run at PreToolUse and PostToolUse stages. This enables:

  • Safety guardrails: A PreToolUse hook could validate exec commands against security rules before execution
  • Audit logging: A PostToolUse hook could log all external actions (sends, API calls)
  • Custom workflows: Hooks could inject additional context, transform tool inputs, or gate dangerous operations

For personal AI assistants (like those built on OpenClaw), this is especially valuable — users could define workspace-specific policies that the agent can't bypass, enforced at the tool layer rather than relying solely on prompt instructions.

Proposed Design

Configuration

hooks:
  preToolUse:
    - ./hooks/validate-exec.sh
    - ./hooks/log-tool-use.sh
  postToolUse:
    - ./hooks/audit-external.sh

Hook Protocol

  • Hooks receive tool context via environment variables: HOOK_TOOL_NAME, HOOK_TOOL_INPUT, HOOK_EVENT
  • Full JSON payload piped to stdin
  • Exit code 0 = Allow (stdout captured as optional feedback)
  • Exit code 2 = Deny (stdout returned as denial reason to model)
  • Any other exit code = Warn but allow (logged, execution continues)

Execution Flow

  1. Agent requests tool call
  2. All PreToolUse hooks run in order
  3. If any hook exits 2 → tool call denied, error returned to model
  4. Tool executes normally
  5. All PostToolUse hooks run with tool output added to context
  6. If PostToolUse hook exits 2 → result marked as error

Examples

Security validation hook

#!/bin/bash
# Deny rm -rf commands
if [ "$HOOK_TOOL_NAME" = "exec" ]; then
  echo "$HOOK_TOOL_INPUT" | grep -q 'rm -rf /' && {
    echo "Blocked: destructive rm command"
    exit 2
  }
fi
exit 0

Audit logging hook

#!/bin/bash
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) $HOOK_EVENT $HOOK_TOOL_NAME" >> ~/.openclaw/audit.log
exit 0

Prior Art

  • Claude Code: src/services/tools/toolHooks.ts
  • Claw Code (Rust): rust/crates/runtime/src/hooks.rs, rust/crates/plugins/src/hooks.rs
  • Both use the same exit-code protocol described above

extent analysis

TL;DR

To implement configurable shell-script hooks for tool execution, define the hooks in a YAML configuration file and ensure they adhere to the specified protocol, using environment variables and exit codes to control tool execution.

Guidance

  • Define the hooks configuration in YAML, specifying preToolUse and postToolUse hooks with their respective script paths.
  • Ensure each hook script uses the provided environment variables (HOOK_TOOL_NAME, HOOK_TOOL_INPUT, HOOK_EVENT) and follows the exit code protocol to control tool execution.
  • Implement hook scripts according to the proposed design, such as the security validation hook and audit logging hook examples provided.
  • Verify the hooks are working as expected by testing tool execution with different hook configurations and exit codes.

Example

#!/bin/bash
# Example hook script that denies tool execution based on input
if [ "$HOOK_TOOL_NAME" = "exec" ]; then
  echo "$HOOK_TOOL_INPUT" | grep -q 'disallowed-command' && {
    echo "Blocked: disallowed command"
    exit 2
  }
fi
exit 0

Notes

The implementation of the hook system relies on the correct usage of environment variables and exit codes. It is essential to test the hooks thoroughly to ensure they are working as expected and not causing unintended behavior.

Recommendation

Apply the proposed design and implement the hook system as described, using the provided examples and guidance to ensure correct functionality. This will allow for the creation of customizable shell-script hooks that can enhance the security and auditing capabilities of the tool execution process.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING