Skip to content

Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.

fix(docker): distinguish removed hooks.code in the disabled-hooks 403

Soham Kukreticd78a4406fd6
diff --git a/deploy/docker/server.py b/deploy/docker/server.py
index 86919ef0..e6f63db2 100644
--- a/deploy/docker/server.py
+++ b/deploy/docker/server.py
@@ -672,6 +672,21 @@ _HOOKS_CODE_WARNING = (
)
+def _reject_disabled_hooks(hooks) -> None:
+ """403 for any hooks payload while hooks are disabled. Legacy inline code
+ gets its own detail: enabling CRAWL4AI_HOOKS_ENABLED would not run it (the
+ feature was removed in 0.9.0), so the generic remedy would mislead."""
+ if hooks.code:
+ raise HTTPException(
+ 403,
+ "Inline hook code (hooks.code) was removed in 0.9.0 and cannot be "
+ "enabled; it was not executed. Use declarative hook actions instead "
+ "(GET /hooks/info), which are additionally disabled on this server "
+ "(CRAWL4AI_HOOKS_ENABLED).",
+ )
+ raise HTTPException(403, "Hooks are disabled. Set CRAWL4AI_HOOKS_ENABLED=true to enable.")
+
+
@app.post("/screenshot")
@limiter.limit(config["rate_limiting"]["default_limit"])
@mcp_tool("screenshot")
@@ -900,7 +915,7 @@ async def crawl(
if not crawl_request.urls:
raise HTTPException(400, "At least one URL required")
if crawl_request.hooks and not HOOKS_ENABLED:
- raise HTTPException(403, "Hooks are disabled. Set CRAWL4AI_HOOKS_ENABLED=true to enable.")
+ _reject_disabled_hooks(crawl_request.hooks)
# Check whether it is a redirection for a streaming request
try:
crawler_config = CrawlerRunConfig.load(
@@ -945,7 +960,7 @@ async def crawl_stream(
if not crawl_request.urls:
raise HTTPException(400, "At least one URL required")
if crawl_request.hooks and not HOOKS_ENABLED:
- raise HTTPException(403, "Hooks are disabled. Set CRAWL4AI_HOOKS_ENABLED=true to enable.")
+ _reject_disabled_hooks(crawl_request.hooks)
return await stream_process(crawl_request=crawl_request)
diff --git a/deploy/docker/tests/test_legacy_compat.py b/deploy/docker/tests/test_legacy_compat.py
index b8b8402c..63a4e22f 100644
--- a/deploy/docker/tests/test_legacy_compat.py
+++ b/deploy/docker/tests/test_legacy_compat.py
@@ -116,8 +116,18 @@ class TestLegacyHookCode:
assert req.hooks.code == LEGACY_HOOKS["code"]
assert req.hooks.hooks == []
- @pytest.mark.parametrize("hooks_payload", [LEGACY_HOOKS, DECLARATIVE_HOOKS])
- def test_any_hooks_payload_403_when_disabled(self, server_module, monkeypatch, stock_client, hooks_payload):
+ # Any hooks payload is refused while hooks are disabled, but the detail
+ # must not mislead legacy callers: enabling the flag would not run
+ # hooks.code (removed in 0.9.0), so payloads carrying it get a removal
+ # message instead of the generic 'set CRAWL4AI_HOOKS_ENABLED' hint.
+ @pytest.mark.parametrize("hooks_payload,expected_detail", [
+ (LEGACY_HOOKS, "removed in 0.9.0"), # code-only
+ ({**DECLARATIVE_HOOKS, **LEGACY_HOOKS}, "removed in 0.9.0"), # mixed
+ (DECLARATIVE_HOOKS, "Set CRAWL4AI_HOOKS_ENABLED=true"), # declarative-only
+ ])
+ def test_any_hooks_payload_403_when_disabled(
+ self, server_module, monkeypatch, stock_client, hooks_payload, expected_detail
+ ):
monkeypatch.setattr(server_module, "HOOKS_ENABLED", False)
r = stock_client.post(
"/crawl",
@@ -125,6 +135,7 @@ class TestLegacyHookCode:
headers=_bearer(),
)
assert r.status_code == 403
+ assert expected_detail in r.json()["detail"]
def _stub_crawl(self, server_module, monkeypatch, results):
async def fake_handle_crawl_request(**kwargs):