fix: Docker API hook manager crashes for all user-provided hooks (#1878)#1879
fix: Docker API hook manager crashes for all user-provided hooks (#1878)#1879hafezparast wants to merge 2 commits intounclecode:developfrom
Conversation
…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>
|
@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):
Should fix:
|
Docker verification ✅Tested against a locally built image from this branch with
Docker logs show |
|
@hafezparast Thanks for the Docker verification — good to know it works end-to-end. However, the review items I listed still need to be addressed before we can merge:
Could you push those changes? Thanks! |
- Remove BaseException from allowed_builtins: catching it defeats asyncio.wait_for timeout (asyncio.CancelledError inherits from BaseException in Python 3.9+), creating a DoS vector - Inject typing module + Union/Any/Tuple/Set into hook namespace so users can access all common type hints without __import__ - Replace deprecated asyncio.get_event_loop().time() with asyncio.get_running_loop().time() (3x in execute_hook_safely) - Replace deprecated asyncio.get_event_loop().run_until_complete() with asyncio.run() in test_hook_manager_unit.py (20 calls) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
All three review items addressed in the latest commit:
|
|
@hafezparast Thanks for the work on this! The core bug has already been fixed on Since the fix is already in and this PR has merge conflicts with the current |
Summary
__import__from safe builtins for security, but the hook compiler still usedexec("import asyncio", namespace)which requires__import__— breaking ALL user-provided hooksexec-based imports with direct module injection into the namespace (namespace['asyncio'] = asyncio, etc.)ValueError,TypeError,KeyError, etc.) to safe builtins so hooks can usetry/exceptproperly__import__remains blocked — users still cannot import arbitrary modules at runtimeChanges
deploy/docker/hook_manager.py: Fix module injection + add exception classes to allowed builtinstests/docker/test_hook_manager_unit.py: 36 unit tests covering:__import__/exec/eval/open all blockedTest plan
pytest tests/docker/test_hook_manager_unit.py -v)__import__still blocked at runtime (security preserved)🤖 Generated with Claude Code