Skip to content

fix: Docker API hook manager crashes for all user-provided hooks (#1878)#1879

Open
hafezparast wants to merge 1 commit intounclecode:developfrom
hafezparast:fix/hook-manager-import-crash-1878
Open

fix: Docker API hook manager crashes for all user-provided hooks (#1878)#1879
hafezparast wants to merge 1 commit intounclecode:developfrom
hafezparast:fix/hook-manager-import-crash-1878

Conversation

@hafezparast
Copy link
Copy Markdown
Contributor

Summary

Changes

  • deploy/docker/hook_manager.py: Fix module injection + add exception classes to allowed builtins
  • tests/docker/test_hook_manager_unit.py: 36 unit tests covering:
    • Issue [Bug]: Docker API hook manager crashes for all kind of user-provided hooks #1878 exact repro + all 8 hook points
    • Security: import/__import__/exec/eval/open all blocked
    • Validation: empty code, sync func, missing params, syntax errors
    • Adversarial: infinite loop timeout, exception capture, 10k-line code, unicode, namespace isolation
    • Exception classes: raise/catch, isinstance checks, all 14 classes verified

Test plan

🤖 Generated with Claude Code

…lecode#1878)

PR unclecode#1712 removed `__import__` from the safe builtins for security, but
the hook compiler still used `exec("import asyncio", namespace)` which
requires `__import__`. This broke ALL user-provided hooks.

Fix: inject commonly-needed modules (asyncio, json, re, typing) directly
into the namespace instead of using exec-based imports. Also add standard
exception classes (ValueError, TypeError, KeyError, etc.) to the safe
builtins so hooks can use try/except properly.

Security: `__import__` remains blocked — users still cannot import
arbitrary modules at runtime.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ntohidi
Copy link
Copy Markdown
Collaborator

ntohidi commented Mar 30, 2026

@hafezparast , thanks for the solid fix — the approach of replacing exec("import ...") with direct module injection is clean and correctly preserves the security intent of PR #1712. Tests are thorough.

A few things to address before the merge:

Must fix (critical):

  • Remove BaseException from allowed_builtins. Since asyncio.CancelledError inherits from BaseException (Python 3.9+), a hook with except BaseException: pass can defeat the asyncio.wait_for timeout in execute_hook_safely, creating a DoS vector. Users should only be able to catch Exception and its subclasses.

Should fix:

  • Inject the typing module itself into the namespace (namespace['typing'] = typing) alongside the individual names (Dict, List, Optional). Currently, if a user needs Union, Any, Tuple, etc., they have no way to access them since import is blocked.
  • Replace asyncio.get_event_loop().run_until_complete() in tests — it's deprecated since Python 3.10 and noisy on 3.12+. Use asyncio.run() or pytest-asyncio with @pytest.mark.asyncio.

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]: Docker API hook manager crashes for all kind of user-provided hooks

2 participants