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.

Merge security fixes for 0.9.5

ntohidi2921d4bcfe17
diff --cc crawl4ai/async_configs.py
index d499f6e9,9ea6cf1f..15789be1
--- a/crawl4ai/async_configs.py
+++ b/crawl4ai/async_configs.py
@@@ -231,13 -222,32 +231,37 @@@ 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")
+ },
+ # Same reason, no scalar allowlist. model_name is a HuggingFace repo id
+ # handed to from_pretrained: the caller would pick the weights the server
+ # downloads and unpickles.
+ "CosineStrategy": {"model_name"},
+ # Caller-supplied regexes. Python's re holds the GIL while it matches, so
+ # one catastrophic-backtracking pattern freezes the whole server, and the
+ # crawl deadline cannot interrupt it. The built-in patterns stay available.
+ "RegexExtractionStrategy": {"custom"},
+ "RegexChunking": {"patterns"},
}
+ # Extraction schemas are free-form dicts, so their "regex" field type (a
+ # caller-supplied re.search pattern, same ReDoS as above) is checked by walking
+ # the schema rather than by field name.
+ _SCHEMA_STRATEGIES = {
+ "JsonCssExtractionStrategy", "JsonXPathExtractionStrategy", "JsonLxmlExtractionStrategy",
+ }
+
+
+ def _has_regex_field(node) -> bool:
+ if isinstance(node, dict):
+ return node.get("type") == "regex" or any(_has_regex_field(v) for v in node.values())
+ if isinstance(node, list):
+ return any(_has_regex_field(v) for v in node)
+ return False
+
# Field names that must NEVER be set from an untrusted body on ANY allowed
# type, checked regardless of per-type allowlist. This is the fail-closed
# backstop: the per-type maps above only cover BrowserConfig/CrawlerRunConfig,
diff --cc deploy/docker/governor.py
index 0c1f8d6a,372a3c37..d9e469dc
--- a/deploy/docker/governor.py
+++ b/deploy/docker/governor.py
@@@ -19,9 -19,11 +19,12 @@@ integration-tested pass; gunicorn --lim
from __future__ import annotations
import json
+ import logging
-import math
+
+from crawl4ai.deep_crawling.filters import FilterChain, URLFilter
+ logger = logging.getLogger("crawl4ai.security")
+
DEFAULT_MAX_BODY_BYTES = 10 * 1024 * 1024 # 10 MiB
DEFAULT_MAX_PAGES = 100
DEFAULT_MAX_DEPTH = 5
@@@ -95,24 -82,19 +98,30 @@@ def clamp_deep_crawl(crawler_config, *
try:
dc.max_pages = max_pages
except Exception:
- pass
+ logger.warning(
+ "clamp_deep_crawl: could not clamp max_pages on %r; "
+ "deep crawl may run with an unbounded page budget", dc, exc_info=True
+ )
md = 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_depth
except Exception:
- pass
+ logger.warning(
+ "clamp_deep_crawl: could not clamp max_depth on %r; "
+ "deep crawl may run with an unbounded depth budget", dc, exc_info=True
+ )
+ 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: