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(api): refuse an invalid cache_mode string with 400, not 500

ntohidiae4a9b60a5c5
diff --git a/crawl4ai/async_configs.py b/crawl4ai/async_configs.py
index fbea1e3f..d499f6e9 100644
--- a/crawl4ai/async_configs.py
+++ b/crawl4ai/async_configs.py
@@ -427,6 +427,10 @@ def _filter_untrusted_fields(type_name: str, params: dict) -> dict:
raise UntrustedConfigError(
"fetch_ssl_certificate cannot be combined with deep_crawl_strategy from an untrusted request"
)
+ cache_mode = out.get("cache_mode") if type_name == "CrawlerRunConfig" else None
+ if isinstance(cache_mode, str) and cache_mode.lower() not in {m.value for m in CacheMode}:
+ # CrawlerRunConfig raises ValueError on it, which would surface as a 500.
+ raise UntrustedConfigError(f"invalid cache_mode: {cache_mode!r}")
return out
diff --git a/deploy/docker/tests/test_playground_config_protocol.py b/deploy/docker/tests/test_playground_config_protocol.py
index 926b113d..d71529de 100644
--- a/deploy/docker/tests/test_playground_config_protocol.py
+++ b/deploy/docker/tests/test_playground_config_protocol.py
@@ -90,7 +90,11 @@ class TestTemplatesSurviveTheUntrustedGate(unittest.TestCase):
obj = CONFIG_CLASSES[name].load(
{"type": name, "params": params}, provenance=Provenance.UNTRUSTED
)
- applied = {key: getattr(obj, key) for key in params}
+ # Enum fields (cache_mode) come back as the enum; compare by value.
+ applied = {
+ key: getattr(getattr(obj, key), "value", getattr(obj, key))
+ for key in params
+ }
self.assertEqual(applied, params)
def test_stream_reaches_the_shape_should_use_stream_reads(self):
diff --git a/tests/unit/test_config_provenance.py b/tests/unit/test_config_provenance.py
index 62f167a6..201629c5 100644
--- a/tests/unit/test_config_provenance.py
+++ b/tests/unit/test_config_provenance.py
@@ -195,3 +195,17 @@ def test_url_pattern_star_led_glob_is_linear_and_unchanged():
start = time.time()
assert not f.apply("https://x.com/" + "a" * 50000)
assert time.time() - start < 0.5
+
+
+def test_cache_mode_string_is_converted_and_invalid_one_refused():
+ from crawl4ai import CacheMode
+
+ cfg = CrawlerRunConfig.load(
+ {"type": "CrawlerRunConfig", "params": {"cache_mode": "Enabled"}}, provenance=UNTRUSTED
+ )
+ assert cfg.cache_mode == CacheMode.ENABLED
+ # A bad value must be a 400 (UntrustedConfigError), not a ValueError (500).
+ with pytest.raises(UntrustedConfigError, match="cache_mode"):
+ CrawlerRunConfig.load(
+ {"type": "CrawlerRunConfig", "params": {"cache_mode": "nope"}}, provenance=UNTRUSTED
+ )