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.
Python aiai-agentscrawl4ai-cloudcrawlerdata-extractionllmmarkdownmcpopen-sourceplaywrightpythonragscraperweb-crawlerweb-crawlingweb-scraping
feat(docker): opt-in deep crawling via CRAWL4AI_ALLOW_DEEP_CRAWL (#2308)
Soham Kukretia4312acdf3bf
diff --git a/crawl4ai/async_configs.py b/crawl4ai/async_configs.pyindex cc1efdb8..c131ff39 100644--- a/crawl4ai/async_configs.py+++ b/crawl4ai/async_configs.py@@ -199,6 +199,14 @@ UNTRUSTED_ALLOWED_TYPES = {"CacheMode", "MatchMode", "DisplayMode",}+# Allowed only with CRAWL4AI_ALLOW_DEEP_CRAWL=true; SEOFilter/ContentRelevanceFilter excluded (unguarded httpx fetch).+UNTRUSTED_DEEP_CRAWL_TYPES = {+ "BFSDeepCrawlStrategy", "DFSDeepCrawlStrategy", "BestFirstCrawlingStrategy",+ "FilterChain", "URLPatternFilter", "DomainFilter", "ContentTypeFilter",+ "KeywordRelevanceScorer", "CompositeScorer", "DomainAuthorityScorer",+ "FreshnessScorer", "PathDepthScorer",+}+# Fields that must NEVER come from an untrusted body. Presence => 400 (loud), so# a client smuggling these gets an explicit error rather than a silent drop.UNTRUSTED_FORBIDDEN_FIELDS = {@@ -223,6 +231,11 @@ UNTRUSTED_FORBIDDEN_FIELDS = {"PDFContentScrapingStrategy": {"image_save_dir", "save_images_locally",},+ # resume_state.pending would queue URLs that skip seed validation.+ **{+ name: {"resume_state", "logger", "on_state_change", "should_cancel"}+ for name in ("BFSDeepCrawlStrategy", "DFSDeepCrawlStrategy", "BestFirstCrawlingStrategy")+ },}# Field names that must NEVER be set from an untrusted body on ANY allowed@@ -302,6 +315,7 @@ _MAX_PDF_PAGES = 2000_MAX_LINK_PREVIEW_LINKS = 100 # the class default_MAX_LINK_PREVIEW_CONCURRENCY = 10 # the class default_MAX_LINK_PREVIEW_TIMEOUT_S = 10 # seconds here, not ms+_MAX_DEEP_CRAWL_LIST = 100 # patterns, keywords, filters, scorers per objectdef _max_timeout_ms() -> int:@@ -339,10 +353,18 @@ def _max_timeout_ms() -> int:return ceiling+def _deep_crawl_allowed() -> bool:+ """Operator opt-in for deep crawling from untrusted requests."""+ return os.getenv("CRAWL4AI_ALLOW_DEEP_CRAWL", "false").lower() == "true"++def _filter_untrusted_fields(type_name: str, params: dict) -> dict:"""Drop non-allowlisted fields and raise on forbidden (power) fields."""forbidden = UNTRUSTED_FORBIDDEN_FIELDS.get(type_name, set())allowlist = UNTRUSTED_FIELD_ALLOWLIST.get(type_name) # None => keep all non-forbidden+ if type_name == "CrawlerRunConfig" and _deep_crawl_allowed():+ forbidden = forbidden - {"deep_crawl_strategy"}+ allowlist = allowlist | {"deep_crawl_strategy"}out = {}for key, value in params.items():if key in UNTRUSTED_GLOBAL_FORBIDDEN_FIELDS or key in forbidden:@@ -352,6 +374,33 @@ def _filter_untrusted_fields(type_name: str, params: dict) -> dict:if allowlist is not None and key not in allowlist:continue # silently drop unknown/unsafe fields (forward-compatible)out[key] = value+ if type_name in UNTRUSTED_DEEP_CRAWL_TYPES and any(+ isinstance(v, list) and len(v) > _MAX_DEEP_CRAWL_LIST for v in out.values()+ ):+ raise UntrustedConfigError(+ f"{type_name} list parameters are limited to {_MAX_DEEP_CRAWL_LIST} items"+ )+ if type_name == "URLPatternFilter":+ # Regex and *.domain:// patterns compile unescaped, and globs not led by * search in+ # quadratic time; allow only *-led globs, /prefix/* and *.ext.+ patterns = out.get("patterns")+ patterns = [patterns] if isinstance(patterns, str) else patterns+ if not isinstance(patterns, list) or not all(+ isinstance(p, str)+ and not (p.startswith("^") or p.endswith("$") or "\\d" in p or "**" in p)+ and not ("://" in p and p.startswith("*."))+ and (p.startswith("*") or (p.count("*") == 1 and p.endswith("/*")))+ for p in patterns+ ):+ raise UntrustedConfigError(+ "URLPatternFilter accepts only globs that start with '*' or end with a "+ "single '/*' from an untrusted request"+ )+ if type_name == "CrawlerRunConfig" and out.get("deep_crawl_strategy") and out.get("fetch_ssl_certificate"):+ # The certificate fetch dials each discovered link directly, outside the egress proxy.+ raise UntrustedConfigError(+ "fetch_ssl_certificate cannot be combined with deep_crawl_strategy from an untrusted request"+ )return out@@ -556,7 +605,11 @@ def from_serializable_dict(data: Any, provenance: "Provenance" = None) -> Any:# Untrusted bodies may only construct the strict subset of types and# may not set forbidden power-fields.- if provenance == Provenance.UNTRUSTED and type_name not in UNTRUSTED_ALLOWED_TYPES:+ if (+ provenance == Provenance.UNTRUSTED+ and type_name not in UNTRUSTED_ALLOWED_TYPES+ and not (type_name in UNTRUSTED_DEEP_CRAWL_TYPES and _deep_crawl_allowed())+ ):raise UntrustedConfigError(f"type '{type_name}' may not be constructed from an untrusted request")diff --git a/crawl4ai/deep_crawling/filters.py b/crawl4ai/deep_crawling/filters.pyindex 2fb819ec..85352def 100644--- a/crawl4ai/deep_crawling/filters.py+++ b/crawl4ai/deep_crawling/filters.py@@ -209,7 +209,8 @@ class URLPatternFilter(URLFilter):lambda m: f'({"|".join(m.group(1).split(","))})',pattern,)- pattern = fnmatch.translate(pattern)+ # A leading * already matches any prefix; \A stops search() retrying every offset.+ pattern = ("\\A" if pattern.startswith("*") else "") + fnmatch.translate(pattern)self._path_patterns.append(pattern if isinstance(pattern, Pattern) else re.compile(pattern))diff --git a/deploy/docker/MIGRATION.md b/deploy/docker/MIGRATION.mdindex 5123c950..c389a045 100644--- a/deploy/docker/MIGRATION.md+++ b/deploy/docker/MIGRATION.md@@ -68,6 +68,10 @@ control):`base_url`, `deep_crawl_strategy`, `simulate_user`, `magic`,`process_in_browser`, and nested LLM config objects.+`deep_crawl_strategy` can be turned back on by the operator with+`CRAWL4AI_ALLOW_DEEP_CRAWL=true`, with server-side limits. See "Deep Crawling+(opt-in)" in the self-hosting guide.+Unknown fields are dropped; timeouts, viewport and scroll counts are clamped tosafe maximums.diff --git a/deploy/docker/README.md b/deploy/docker/README.mdindex 9216de6c..1b8c5775 100644--- a/deploy/docker/README.md+++ b/deploy/docker/README.md@@ -562,7 +562,62 @@ Crucially, when sending configurations directly via JSON, they **must** follow t```**LLM Extraction Strategy** *(Keep example, ensure schema uses type/value wrapper)*-*(Keep Deep Crawler Example)*+#### Deep Crawling (opt-in)++Deep crawling over the API is **disabled by default**: a request with+`deep_crawl_strategy` returns HTTP 400. To allow it, start the container with+`CRAWL4AI_ALLOW_DEEP_CRAWL=true`:++```bash+docker run -d \+ -p 11235:11235 \+ --name crawl4ai \+ --shm-size=1g \+ -e CRAWL4AI_API_TOKEN="$CRAWL4AI_API_TOKEN" \+ -e CRAWL4AI_ALLOW_DEEP_CRAWL=true \+ unclecode/crawl4ai:latest+```++With Docker Compose, put `CRAWL4AI_ALLOW_DEEP_CRAWL=true` in the `.env` file in+the project root, or export it in your shell. The compose file sets this+variable itself, so a value in `.llm.env` is ignored.++```json+{+ "urls": ["https://docs.crawl4ai.com"],+ "crawler_config": {+ "type": "CrawlerRunConfig",+ "params": {+ "deep_crawl_strategy": {+ "type": "BFSDeepCrawlStrategy",+ "params": {+ "max_depth": 2,+ "max_pages": 20,+ "filter_chain": {+ "type": "FilterChain",+ "params": {+ "filters": [+ {"type": "URLPatternFilter", "params": {"patterns": ["*/core/*"]}},+ {"type": "DomainFilter", "params": {"allowed_domains": ["docs.crawl4ai.com"]}}+ ]+ }+ }+ }+ }+ }+ }+}+```++Rules the server applies:++- **One start URL** per request, and not inside `crawler_configs`.+- `max_pages` and `max_depth` are clamped to `limits.max_pages` and `limits.max_depth` in `config.yml` (default 100 and 5). The crawl stops after `limits.wall_clock_s`, also on `/crawl/stream`.+- **Allowed types:** `BFSDeepCrawlStrategy`, `DFSDeepCrawlStrategy`, `BestFirstCrawlingStrategy`; `FilterChain` with `URLPatternFilter`, `DomainFilter`, `ContentTypeFilter`; scorers `KeywordRelevanceScorer`, `CompositeScorer`, `DomainAuthorityScorer`, `FreshnessScorer`, `PathDepthScorer`. `SEOFilter` and `ContentRelevanceFilter` are refused (they fetch pages outside the egress controls).+- `URLPatternFilter` accepts globs that start with `*` (`*/blog/*`), `/prefix/*` or `*.ext`. Regex patterns are refused.+- `resume_state` is refused, and so is `fetch_ssl_certificate` together with a deep crawl. Lists (patterns, keywords, filters, scorers) are limited to 100 items. Discovered links longer than 2048 characters are skipped.++Turn the flag on only when you accept that one request can fetch many pages.### REST API Examplesdiff --git a/deploy/docker/api.py b/deploy/docker/api.pyindex 244b5046..d4636c41 100644--- a/deploy/docker/api.py+++ b/deploy/docker/api.py@@ -641,6 +641,8 @@ async def stream_results(crawler: AsyncWebCrawler, results_gen: AsyncGenerator)yield json.dumps({"status": "completed"}).encode('utf-8')+ except asyncio.TimeoutError:+ yield json.dumps({"status": "error", "error": "Crawl exceeded the time limit"}).encode('utf-8')except asyncio.CancelledError:logger.warning("Client disconnected during streaming")finally:@@ -663,6 +665,8 @@ def _load_crawler_configs(for cc in crawler_configs]for cfg in config_list:+ if cfg.deep_crawl_strategy is not None:+ raise HTTPException(status_code=400, detail="Deep crawling supports exactly one URL per request.")for key, value in (base_config or {}).items():if hasattr(cfg, key):current_value = getattr(cfg, key)@@ -699,6 +703,20 @@ def _needs_pdf_crawler(return all(pdf)+async def _with_deadline(gen: AsyncGenerator, seconds: float) -> AsyncGenerator:+ """Stop a result stream once its wall-clock budget is spent (raises TimeoutError)."""+ loop = asyncio.get_running_loop()+ end = loop.time() + seconds+ try:+ while True:+ try:+ yield await asyncio.wait_for(gen.__anext__(), max(end - loop.time(), 0))+ except StopAsyncIteration:+ return+ finally:+ await gen.aclose()++def _normalize_and_validate_seeds(urls: List[str]) -> Tuple[List[str], List[CrawlResult]]:"""Prefix bare hosts with https:// and SSRF-validate every seed URL'sdestination. Shared by the streaming and non-streaming crawl handlers so a@@ -753,8 +771,10 @@ async def handle_crawl_request(crawler_config = CrawlerRunConfig.load(crawler_config, provenance=Provenance.UNTRUSTED)from egress_broker import enforce_egressenforce_egress(browser_config)- from governor import clamp_deep_crawl- clamp_deep_crawl(crawler_config)+ from governor import clamp_deep_crawl, deep_crawl_limits+ clamp_deep_crawl(crawler_config, **deep_crawl_limits(config))+ if crawler_config.deep_crawl_strategy is not None and len(urls) + len(refused) != 1:+ raise HTTPException(status_code=400, detail="Deep crawling supports exactly one URL per request.")dispatcher = MemoryAdaptiveDispatcher(memory_threshold_percent=config["crawler"]["memory_threshold_percent"],@@ -984,9 +1004,9 @@ async def handle_stream_crawl_request(crawler_config = CrawlerRunConfig.load(crawler_config, provenance=Provenance.UNTRUSTED)- from governor import clamp_deep_crawl+ from governor import clamp_deep_crawl, deep_crawl_limits- clamp_deep_crawl(crawler_config)+ clamp_deep_crawl(crawler_config, **deep_crawl_limits(config))crawler_config.stream = Trueconfig_list = (_load_crawler_configs(@@ -1034,6 +1054,9 @@ async def handle_stream_crawl_request(urls[0],config=crawler_config,)+ from governor import wall_clock_seconds+ if wall_clock_seconds(config) > 0:+ results_gen = _with_deadline(results_gen, wall_clock_seconds(config))else:# Default multi-URL streaming via arun_manydispatcher = MemoryAdaptiveDispatcher(diff --git a/deploy/docker/config.yml b/deploy/docker/config.ymlindex 09420989..c22bf381 100644--- a/deploy/docker/config.yml+++ b/deploy/docker/config.yml@@ -37,7 +37,7 @@ redis:# (the pre-hardening behavior), so you can relax or disable each one.limits:max_body_bytes: 10485760 # 10 MiB request body cap (413 if exceeded); 0 = unbounded- max_pages: 100 # deep-crawl page budget clamp (defense in depth)+ max_pages: 100 # deep-crawl page budget clamp (deep crawl needs CRAWL4AI_ALLOW_DEEP_CRAWL=true)max_depth: 5 # deep-crawl depth clampwall_clock_s: 300 # per-crawl deadline in seconds (504 on timeout); 0 = no deadlinequeue: # background job queue for /crawl/job and /llm/jobdiff --git a/deploy/docker/governor.py b/deploy/docker/governor.pyindex 73534e99..4cabe0ab 100644--- a/deploy/docker/governor.py+++ b/deploy/docker/governor.py@@ -1,15 +1,15 @@"""governor.py - server-enforced resource governance (R5).-The deep-crawl DoS via a client-supplied unbounded max_pages is already closed-upstream: R2 forbids `deep_crawl_strategy` on an untrusted request body, and the-`urls` list is capped at 100 by the request schema. This module adds the two-remaining verifiable chokepoints:+R2 forbids `deep_crawl_strategy` on an untrusted request body unless the+operator sets CRAWL4AI_ALLOW_DEEP_CRAWL=true, and the `urls` list is capped at+100 by the request schema. This module adds the two remaining verifiable+chokepoints:* a request body-size limit (ASGI middleware) so a giant body / inline `raw:`HTML cannot be buffered and processed in-process -> 413;- * clamp_deep_crawl(): defense in depth for any *trusted* / server-built config- that still carries a deep_crawl strategy with an unbounded page/depth count.+ * clamp_deep_crawl(): bounds the page/depth budget and URL length of any+ deep-crawl strategy, whether opted in from a request or server-built.Heavier governance (bounded work queue replacing BackgroundTasks, per-principalRedis quotas, wall-clock deadlines, stream decoupling) is left for the@@ -19,11 +19,26 @@ integration-tested pass; gunicorn --limit-request-* covers the transport layer.from __future__ import annotationsimport json-import math++from crawl4ai.deep_crawling.filters import FilterChain, URLFilterDEFAULT_MAX_BODY_BYTES = 10 * 1024 * 1024 # 10 MiBDEFAULT_MAX_PAGES = 100DEFAULT_MAX_DEPTH = 5+DEFAULT_MAX_URL_LENGTH = 2048+++class URLLengthFilter(URLFilter):+ """Reject discovered URLs too long to filter, score and cache cheaply."""++ def __init__(self, max_length: int = DEFAULT_MAX_URL_LENGTH):+ super().__init__()+ self.max_length = max_length++ def apply(self, url: str) -> bool:+ passed = len(url) <= self.max_length+ self._update_stats(passed)+ return passedclass BodySizeLimitMiddleware:@@ -65,27 +80,38 @@ class BodySizeLimitMiddleware:def clamp_deep_crawl(crawler_config, *, max_pages: int = DEFAULT_MAX_PAGES,max_depth: int = DEFAULT_MAX_DEPTH) -> None:- """Clamp an attached deep-crawl strategy's page/depth budget in place.+ """Clamp an attached deep-crawl strategy's page/depth budget in place and+ put a URL-length guard first in its filter chain.- Defense in depth: untrusted bodies cannot set deep_crawl_strategy at all- (R2), but a server/base config might, and the library default for max_pages- is infinity.+ The library default for max_pages is infinity."""dc = getattr(crawler_config, "deep_crawl_strategy", None)if dc is None:return+ # NaN fails every comparison, so test for "inside the range", not "above the cap".mp = getattr(dc, "max_pages", None)- if mp is None or (isinstance(mp, float) and math.isinf(mp)) or mp > max_pages:+ if not isinstance(mp, (int, float)) or not 0 <= mp <= max_pages:try:dc.max_pages = max_pagesexcept Exception:passmd = getattr(dc, "max_depth", None)- if md is None or md > max_depth:+ if not isinstance(md, (int, float)) or not 0 <= md <= max_depth:try:dc.max_depth = max_depthexcept Exception:pass+ chain = getattr(dc, "filter_chain", None) or FilterChain()+ dc.filter_chain = FilterChain([URLLengthFilter(), *chain.filters])+++def deep_crawl_limits(config: dict) -> dict:+ """Deep-crawl page/depth clamps from config limits."""+ limits = _limits(config)+ return {+ "max_pages": int(limits.get("max_pages", DEFAULT_MAX_PAGES)),+ "max_depth": int(limits.get("max_depth", DEFAULT_MAX_DEPTH)),+ }def max_body_bytes_from_config(config: dict) -> int:diff --git a/deploy/docker/tests/test_security_resource_caps.py b/deploy/docker/tests/test_security_resource_caps.pyindex 6cf4f038..a60a3cc2 100644--- a/deploy/docker/tests/test_security_resource_caps.py+++ b/deploy/docker/tests/test_security_resource_caps.py@@ -49,6 +49,26 @@ class TestDeepCrawlClamp:clamp_deep_crawl(cfg) # must not raiseassert cfg.deep_crawl_strategy is None+ def test_nan_limits_clamped(self):+ from governor import clamp_deep_crawl+ from crawl4ai.deep_crawling import BFSDeepCrawlStrategy+ from crawl4ai import CrawlerRunConfig+ nan = float("nan")+ cfg = CrawlerRunConfig(deep_crawl_strategy=BFSDeepCrawlStrategy(max_depth=nan, max_pages=nan))+ clamp_deep_crawl(cfg)+ assert (cfg.deep_crawl_strategy.max_pages, cfg.deep_crawl_strategy.max_depth) == (100, 5)++ def test_url_length_guard_added_even_without_filter_chain(self):+ from governor import URLLengthFilter, clamp_deep_crawl+ from crawl4ai.deep_crawling import BFSDeepCrawlStrategy+ from crawl4ai import CrawlerRunConfig+ cfg = CrawlerRunConfig(deep_crawl_strategy=BFSDeepCrawlStrategy(max_depth=1, filter_chain=None))+ clamp_deep_crawl(cfg)+ guard = cfg.deep_crawl_strategy.filter_chain.filters[0]+ assert isinstance(guard, URLLengthFilter)+ assert not guard.apply("https://a.com/" + "x" * 3000)+ assert guard.apply("https://a.com/blog/post")+class TestUntrustedDeepCrawlStillForbidden:def test_request_cannot_set_deep_crawl(self):diff --git a/docker-compose.yml b/docker-compose.ymlindex 02cbe058..a0a7aa9a 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -9,6 +9,8 @@ x-base-config: &base-configenvironment:# Auth token passthrough from the host shell (overwrites .llm.env).- CRAWL4AI_API_TOKEN=${CRAWL4AI_API_TOKEN:-}+ # Deep crawling over the API (off unless set to true in the shell or .env).+ - CRAWL4AI_ALLOW_DEEP_CRAWL=${CRAWL4AI_ALLOW_DEEP_CRAWL:-false}# Uncomment to set default environment variables (will overwrite .llm.env)# - OPENAI_API_KEY=${OPENAI_API_KEY:-}# - DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY:-}diff --git a/docs/md_v2/core/self-hosting.md b/docs/md_v2/core/self-hosting.mdindex 8d0e54f2..f5445c98 100644--- a/docs/md_v2/core/self-hosting.md+++ b/docs/md_v2/core/self-hosting.md@@ -1290,7 +1290,61 @@ Crucially, when sending configurations directly via JSON, they **must** follow t```**LLM Extraction Strategy** *(Keep example, ensure schema uses type/value wrapper)*-*(Keep Deep Crawler Example)*+#### Deep Crawling (opt-in)++Deep crawling over the API is **disabled by default**: a request with+`deep_crawl_strategy` returns HTTP 400. To allow it, start the container with+`CRAWL4AI_ALLOW_DEEP_CRAWL=true`:++```bash+docker run -d \+ -p 11235:11235 \+ --name crawl4ai \+ --shm-size=1g \+ -e CRAWL4AI_API_TOKEN="$CRAWL4AI_API_TOKEN" \+ -e CRAWL4AI_ALLOW_DEEP_CRAWL=true \+ unclecode/crawl4ai:latest+```++With Docker Compose, put `CRAWL4AI_ALLOW_DEEP_CRAWL=true` in the `.env` file in+the project root, or export it in your shell.++```json+{+ "urls": ["https://docs.crawl4ai.com"],+ "crawler_config": {+ "type": "CrawlerRunConfig",+ "params": {+ "deep_crawl_strategy": {+ "type": "BFSDeepCrawlStrategy",+ "params": {+ "max_depth": 2,+ "max_pages": 20,+ "filter_chain": {+ "type": "FilterChain",+ "params": {+ "filters": [+ {"type": "URLPatternFilter", "params": {"patterns": ["*/core/*"]}},+ {"type": "DomainFilter", "params": {"allowed_domains": ["docs.crawl4ai.com"]}}+ ]+ }+ }+ }+ }+ }+ }+}+```++Rules the server applies:++- **One start URL** per request, and not inside `crawler_configs`.+- `max_pages` and `max_depth` are clamped to `limits.max_pages` and `limits.max_depth` in `config.yml` (default 100 and 5). The crawl stops after `limits.wall_clock_s`, also on `/crawl/stream`.+- **Allowed types:** `BFSDeepCrawlStrategy`, `DFSDeepCrawlStrategy`, `BestFirstCrawlingStrategy`; `FilterChain` with `URLPatternFilter`, `DomainFilter`, `ContentTypeFilter`; scorers `KeywordRelevanceScorer`, `CompositeScorer`, `DomainAuthorityScorer`, `FreshnessScorer`, `PathDepthScorer`. `SEOFilter` and `ContentRelevanceFilter` are refused (they fetch pages outside the egress controls).+- `URLPatternFilter` accepts globs that start with `*` (`*/blog/*`), `/prefix/*` or `*.ext`. Regex patterns are refused.+- `resume_state` is refused, and so is `fetch_ssl_certificate` together with a deep crawl. Lists (patterns, keywords, filters, scorers) are limited to 100 items. Discovered links longer than 2048 characters are skipped.++Turn the flag on only when you accept that one request can fetch many pages.### LLM Configuration Examplesdiff --git a/tests/unit/test_config_provenance.py b/tests/unit/test_config_provenance.pyindex 49246a8e..883b7170 100644--- a/tests/unit/test_config_provenance.py+++ b/tests/unit/test_config_provenance.py@@ -116,3 +116,55 @@ def test_plain_business_dict_with_type_key_still_passes():provenance=UNTRUSTED,)assert cfg.extraction_strategy.schema["baseSelector"] == ".item"+++def _deep_crawl(**params):+ return {+ "type": "CrawlerRunConfig",+ "params": {"deep_crawl_strategy": {"type": "BFSDeepCrawlStrategy", "params": {"max_depth": 2, **params}}},+ }+++def test_deep_crawl_allowed_with_opt_in(monkeypatch):+ monkeypatch.setenv("CRAWL4AI_ALLOW_DEEP_CRAWL", "true")+ chain = {"type": "FilterChain", "params": {"filters": [+ {"type": "URLPatternFilter", "params": {"patterns": ["*/blog/*"]}}]}}+ cfg = CrawlerRunConfig.load(_deep_crawl(filter_chain=chain, max_pages=50), provenance=UNTRUSTED)+ assert type(cfg.deep_crawl_strategy).__name__ == "BFSDeepCrawlStrategy"+ assert cfg.deep_crawl_strategy.max_pages == 50++[email protected]("pattern", ["^(a+)+$", "tag*x"])+def test_deep_crawl_unsafe_pattern_refused(monkeypatch, pattern):+ monkeypatch.setenv("CRAWL4AI_ALLOW_DEEP_CRAWL", "true")+ chain = {"type": "FilterChain", "params": {"filters": [+ {"type": "URLPatternFilter", "params": {"patterns": [pattern]}}]}}+ with pytest.raises(UntrustedConfigError, match="URLPatternFilter"):+ CrawlerRunConfig.load(_deep_crawl(filter_chain=chain), provenance=UNTRUSTED)+++def test_deep_crawl_resume_state_refused(monkeypatch):+ monkeypatch.setenv("CRAWL4AI_ALLOW_DEEP_CRAWL", "true")+ data = _deep_crawl(resume_state={"pending": [{"url": "http://169.254.169.254/", "depth": 1}]})+ with pytest.raises(UntrustedConfigError, match="resume_state"):+ CrawlerRunConfig.load(data, provenance=UNTRUSTED)+++def test_deep_crawl_ssl_certificate_refused(monkeypatch):+ monkeypatch.setenv("CRAWL4AI_ALLOW_DEEP_CRAWL", "true")+ data = _deep_crawl()+ data["params"]["fetch_ssl_certificate"] = True+ with pytest.raises(UntrustedConfigError, match="fetch_ssl_certificate"):+ CrawlerRunConfig.load(data, provenance=UNTRUSTED)+++def test_url_pattern_star_led_glob_is_linear_and_unchanged():+ import time+ from crawl4ai.deep_crawling.filters import URLPatternFilter++ f = URLPatternFilter(["*/blog/*"])+ assert f.apply("https://x.com/blog/post")+ assert not f.apply("https://x.com/news/post")+ start = time.time()+ assert not f.apply("https://x.com/" + "a" * 50000)+ assert time.time() - start < 0.5