Skip to content

fix(hooks): fail closed on async hooks instead of letting guarded calls through - #10254

Open
YuShaw (starflowerH) wants to merge 1 commit into
crewAIInc:mainfrom
starflowerH:fix/async-hook-fail-closed-10247
Open

YuShaw (starflowerH) wants to merge 1 commit into
crewAIInc:mainfrom
starflowerH:fix/async-hook-fail-closed-10247

Conversation

@starflowerH

Copy link
Copy Markdown

Fixes #10247

Problem

Hooks are documented as synchronous, but nothing enforces it. _invoke_hook
calls result = hook(ctx) and hands the result to the reducer, so for an
async def hook the reducer receives an un-awaited coroutine:

  • before_tool_call_reducer only blocks when result is False. A coroutine
    is never False, so an async hook written to deny a call fails open:
    the tool or LLM call runs.
  • _default_reducer replaces ctx.payload with any non-None result, so an
    async payload hook stores the coroutine object as the payload.
  • Python warns coroutine '...' was never awaited, but nothing tells the user
    the hook had no effect. An async guard is an easy mistake to make (the agent
    runs async elsewhere, and the hook may call an async API), and it silently
    disables the guard.

Solution

In _invoke_hook — the one path every registration style goes through
(register_*, @on, @before_tool_call, scoped hooks) — check
inspect.isawaitable(result) before the reducer runs. If it is awaitable,
close the coroutine (so there is no "never awaited" warning) and raise
HookAborted with a clear message naming the hook. HookAborted is the one
exception _invoke_hook re-raises rather than swallowing, it propagates
through run_hooks (telemetry outcome "aborted"), and
run_before_tool_call_hooks maps it to True (blocked) — so the guarded
call is denied instead of running.

Why fail closed at run time (option A in the issue) rather than rejecting at
registration (option B): the generic except Exception in _invoke_hook
swallows errors, so a TypeError raised there would still fail open; and a
runtime awaitable check covers more than a registration-time
iscoroutinefunction check (callable objects with async __call__,
functools.partial wrapping an async fn, wrappers that hide the coroutine
nature). It also matches the repo's fail-closed tool-hook design. Happy to
rework to option B if maintainers prefer it.

Verification

  • Issue reproducer before the fix: sync deny hook blocks (blocked = True,
    no warnings); async deny hook does not block (blocked = False, warning
    coroutine 'async_deny' was never awaited). After the fix the async deny
    hook blocks (blocked = True) and no warning is emitted.
  • New regression tests: async deny hook at PRE_TOOL_CALL blocks via
    run_before_tool_call_hooks; async payload hook at PRE_MODEL_CALL raises
    HookAborted and leaves ctx.payload unchanged; no "never awaited"
    warning under warnings.simplefilter("error"). Each new test fails without
    the fix (verified via git stash of the source change only) and passes
    with it.
  • pytest lib/crewai/tests/hooks/test_dispatch.py lib/crewai/tests/hooks/test_tool_hooks.py lib/crewai/tests/hooks/test_hook_abort_propagation.py: 70 passed, 4 failed. The 4 failures are all live-OpenAI integration tests (TestNativeToolCallingHooksIntegration × 3, test_lite_agent_hooks_integration_with_real_tool × 1) that fail with "Failed to connect to OpenAI API: Connection error" in this sandbox; the same 4 fail on the unmodified baseline, so they are not regressions.
  • ruff check and ruff format --check pass on the changed source file (test files are excluded from ruff by repo config).

…ls through

Hooks are documented as synchronous, but _invoke_hook handed the
un-awaited coroutine from an async hook to the reducer, so an async
before_tool_call deny hook failed open (a coroutine is never False)
and an async payload hook stored the coroutine as the payload.
Check inspect.isawaitable(result) before the reducer runs; close the
coroutine to avoid the 'never awaited' warning and raise HookAborted
so the guarded call is denied instead of running. HookAborted is the
one exception _invoke_hook re-raises, and run_before_tool_call_hooks
maps it to blocked=True. Add DB-free regression tests covering the
async deny block, the async payload abort, the absence of the warning,
and unchanged sync behavior.
@strix-security

strix-security Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Strix Security Review

✅ No open security findings.

PR summary: Changes the hook dispatcher in `lib/crewai/src/crewai/hooks/dispatch.py` so a hook that returns an awaitable is treated as an error: the coroutine is closed and `HookAborted` is raised, which makes the guarded tool or model call fail closed instead of silently running. Adds regression tests for async deny hooks, async payload hooks and the absence of "never awaited" warnings.

Review summary

The change adds an awaitable check in _invoke_hook so a hook that returns a coroutine is closed and raises HookAborted instead of handing the un-awaited coroutine to the reducer, plus regression tests. I confirmed that the abort is not swallowed: HookAborted is re-raised by _invoke_hook and converted into a block by run_before_tool_call_hooks, so async deny and payload hooks now fail closed across the registration styles the PR claims, and the new branch itself does not offer a way back into the allow path. The remaining fail-open on ordinary hook exceptions and on exotic non-awaitable async shapes is pre-existing, documented, and only decided by the operator's own hook code, so no attacker-controlled bypass is introduced or left reachable.

Ruled out (3)
  • Does an async guard hook still fail open when it is an async generator or otherwise returns a non-awaitable object?
    The returned shape is chosen by the crew developer who writes the hook, no attacker-influenced input changes it, and those shapes failed open identically before the change, so this is a completeness gap in a hardening fix rather than an exploitable bypass.
  • Could the pre-existing `except Exception: return False` around the new branch turn a guard exception into an allow?
    That swallow is unmodified pre-existing behaviour explicitly documented as fail-open for buggy user hooks, the new branch only narrows the allow path, and reaching it requires the operator's own exotic callable (a `close()` that raises or a callable whose name lookup throws), not model input.
  • Can an approval-gate hook that calls `request_human_input()` let the tool run when stdin is closed?
    The behaviour comes from the long-standing swallow of hook exceptions and from the operator's own hook code, and the change neither edits that path nor makes its failure reachable any differently.

Review 1 on this PR · 9472d92 · requested Dec 5 08:40 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings · Try a deeper review with @strix-security /deep

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] async hook functions are never awaited, so an async before_tool_call deny hook lets the tool call run

2 participants