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
fix(docker): fall through junk proxy env values, refuse non-http proxy schemes
Soham Kukreti01e6e96453ef
diff --git a/deploy/docker/egress_proxy.py b/deploy/docker/egress_proxy.pyindex 1aa1bcc2..807bb80a 100644--- a/deploy/docker/egress_proxy.py+++ b/deploy/docker/egress_proxy.py@@ -39,35 +39,39 @@ _BAD = b"HTTP/1.1 400 Bad Request\r\nContent-Length: 11\r\n\r\nBad Request"_MAX_HEADER_BYTES = 64 * 1024-def _env(*names: str) -> str:- return next((os.environ[n] for n in names if os.environ.get(n)), "")--def upstream_proxy(scheme: str = "https"):"""(host, port, auth_header_bytes|None) of the upstream proxy, or None.Read per-call (not at import) so operators and tests see env changes.- The target scheme picks HTTP(S)_PROXY per convention, falling back to- the other pair when only one is set.+ The target scheme picks HTTP(S)_PROXY per convention; the first+ candidate that parses wins, so junk values fall through to a fallback."""order = ("HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy") if scheme == "http" \else ("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy")- raw = _env("CRAWL4AI_UPSTREAM_PROXY", *order).strip()- if not raw:- return None- sp = urlsplit(raw if "://" in raw else "http://" + raw)- if not sp.hostname:- return None- auth = None- if sp.username:- cred = f"{unquote(sp.username)}:{unquote(sp.password or '')}".encode("utf-8")- auth = b"Proxy-Authorization: Basic " + base64.b64encode(cred) + b"\r\n"- return sp.hostname, sp.port or 80, auth+ for name in ("CRAWL4AI_UPSTREAM_PROXY", *order):+ raw = (os.environ.get(name) or "").strip()+ if not raw:+ continue+ sp = urlsplit(raw if "://" in raw else "http://" + raw)+ if not sp.hostname:+ logger.warning("ignoring %s: unparseable proxy URL", name)+ continue+ if sp.scheme != "http":+ # We only speak plaintext HTTP to the upstream (no TLS/socks dial).+ logger.warning("ignoring %s: unsupported proxy scheme %r", name, sp.scheme)+ continue+ auth = None+ if sp.username:+ cred = f"{unquote(sp.username)}:{unquote(sp.password or '')}".encode("utf-8")+ auth = b"Proxy-Authorization: Basic " + base64.b64encode(cred) + b"\r\n"+ return sp.hostname, sp.port or 80, auth+ return Nonedef _no_proxy_match(host: str, ip: str) -> bool:"""True if NO_PROXY says this target must bypass the upstream proxy."""- entries = [e.strip() for e in _env("NO_PROXY", "no_proxy").split(",") if e.strip()]+ raw = os.environ.get("NO_PROXY") or os.environ.get("no_proxy") or ""+ entries = [e.strip() for e in raw.split(",") if e.strip()]for entry in entries:if entry == "*":return True@@ -78,6 +82,9 @@ def _no_proxy_match(host: str, ip: str) -> bool:except ValueError:passsuffix = entry.lower().lstrip(".")+ head, sep, port_part = suffix.rpartition(":")+ if sep and port_part.isdigit():+ suffix = headlow = host.lower()if low == suffix or low.endswith("." + suffix):return Truediff --git a/deploy/docker/tests/test_security_egress_proxy.py b/deploy/docker/tests/test_security_egress_proxy.pyindex f53767f3..9dc377e2 100644--- a/deploy/docker/tests/test_security_egress_proxy.py+++ b/deploy/docker/tests/test_security_egress_proxy.py@@ -264,8 +264,16 @@ def test_upstream_proxy_env_parsing(monkeypatch):monkeypatch.delenv("HTTP_PROXY")monkeypatch.delenv("HTTPS_PROXY")assert egress_proxy.upstream_proxy() is None- # non-latin-1 credentials must not raise (encoded as UTF-8)+ # a junk/unsupported candidate falls through to a valid fallback+ monkeypatch.setenv("HTTP_PROXY", "http://good:3128")+ monkeypatch.setenv("HTTPS_PROXY", "http://")+ assert egress_proxy.upstream_proxy() == ("good", 3128, None)+ monkeypatch.setenv("HTTPS_PROXY", "https://tls-proxy.corp") # unsupported scheme+ assert egress_proxy.upstream_proxy() == ("good", 3128, None)monkeypatch.delenv("CRAWL4AI_UPSTREAM_PROXY")+ monkeypatch.delenv("HTTP_PROXY")+ assert egress_proxy.upstream_proxy() is None # https:// alone -> refused, not mis-dialed+ # non-latin-1 credentials must not raise (encoded as UTF-8)monkeypatch.setenv("HTTPS_PROXY", "http://u:%E5%AF%86%E7%A0%[email protected]:8080")assert egress_proxy.upstream_proxy()[2] is not None# NO_PROXY routing: suffix and CIDR entries force a direct dial@@ -275,6 +283,8 @@ def test_upstream_proxy_env_parsing(monkeypatch):assert egress_proxy._use_upstream(pin) is Nonemonkeypatch.setenv("NO_PROXY", "203.0.113.0/24")assert egress_proxy._use_upstream(pin) is None+ monkeypatch.setenv("NO_PROXY", "site.corp.example:443") # host:port form+ assert egress_proxy._use_upstream(pin) is Noneclass TestEnforceEgressWiring: