~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
HTML Downloader with JavaScript rendering support.
Uses Crawl4AI (default) or plain Playwright for JS-rendered pages.
Crawl4AI adds: robots.txt checking, shadow DOM flattening, iframe
inlining, smart scrolling for lazy-loaded content, and caching.
Falls back to plain Playwright if Crawl4AI is not installed.
No stealth/anti-detection features are used — the browser identifies
honestly via BROWSER_USER_AGENT and respects robots.txt.
"""
import asyncio
import functools
from datetime import datetime, timezone
from email.utils import parsedate_to_datetime
from typing import Optional
from urllib.parse import urljoin, urlparse
from loguru import logger
from .base import rate_limit_authority
from .html import HTMLDownloader
from ...constants import BROWSER_USER_AGENT
from ...security import ssrf_validator
from ...security.egress.policy import Decision, EgressScope, PolicyDeniedError
from ...security.ssrf_validator import redact_url_for_log
from ...security.safe_requests import (
_MAX_REDIRECTS,
_REDIRECT_STATUS_CODES,
_resolve_redirect_method,
)
# Heavy subresource types dropped when ``block_resources`` is set, mirroring
# the previous extension-glob resource route but keyed on Playwright's
# resource_type (robust to query strings / extensionless URLs).
_BLOCKED_RESOURCE_TYPES = frozenset({"image", "media", "font", "stylesheet"})
# Non-http(s) URL schemes the browser may safely handle itself (they never
# touch the network, so they are not SSRF / local-resource vectors): the route
# guard lets ONLY these fall through via ``route.fallback()``. EVERY other
# non-http(s) scheme — ``file:`` (local file read), ``chrome:``/``chrome-
# extension:``, ``gopher:``, ``ftp:``, ``ws(s):`` handled elsewhere, etc. — is
# aborted fail-closed rather than blanket-allowed, so a redirect (or a page
# resource) cannot pivot the browser to a local file or an internal handler.
_FALLTHROUGH_NON_HTTP_SCHEMES = frozenset({"data", "blob", "about"})
# Neutralise Service Workers on browser paths that cannot set
# service_workers="block" at context creation (Crawl4AI's BrowserConfig
# exposes no such option). Requests made BY a Service Worker are not
# intercepted by page/context route handlers, so a SW could otherwise
# fetch an internal/metadata host unvalidated. Preventing registration
# forces all page traffic back through the guarded normal network path.
# Injected as an init script so it runs before any page script.
_DISABLE_SERVICE_WORKERS_JS = """
try {
if (window.navigator && window.navigator.serviceWorker) {
const reject = () =>
Promise.reject(new Error('service workers are disabled'));
try { window.navigator.serviceWorker.register = reject; } catch (e) {}
try {
Object.defineProperty(window.navigator, 'serviceWorker', {
configurable: true,
get() { return undefined; },
});
} catch (e) {}
}
} catch (e) {}
"""
# SSRF hardening: WebRTC (RTCPeerConnection ICE / STUN / TURN) opens raw
# UDP/TCP sockets to an arbitrary host:port through Chromium's WebRTC stack —
# a data path that neither page.route() (Fetch) nor route_web_socket()
# instruments, so it bypasses ssrf_validator entirely. Text extraction never
# needs WebRTC, so neutralize the constructors before any page script runs
# (same fail-closed reasoning as the service-worker block).
_DISABLE_WEBRTC_JS = """
(() => {
const deny = function () {
throw new DOMException('WebRTC is disabled', 'NotSupportedError');
};
for (const name of ['RTCPeerConnection', 'webkitRTCPeerConnection',
'mozRTCPeerConnection', 'RTCDataChannel']) {
try {
Object.defineProperty(window, name, {
configurable: false, enumerable: false, get() { return deny; },
});
} catch (e) {}
}
})();
"""
# Signals that a page is a JS-rendered SPA and needs browser rendering
SPA_SIGNALS = [
'id="root"',
'id="app"',
'id="__next"',
"__NEXT_DATA__",
"data-reactroot",
'ng-version="',
"<noscript>You need to enable JavaScript",
"<noscript>Please enable JavaScript",
"window.__INITIAL_STATE__",
]
def _browser_transport_allowed(session) -> bool:
"""Native browser DNS cannot enforce a restricted run's pinned scope."""
context = getattr(session, "egress_context", None)
return context is None or getattr(context, "scope", None) in (
EgressScope.BOTH,
EgressScope.UNPROTECTED,
)
def _cookie_domain_matches(host: str, cookie_domain: str) -> bool:
"""Whether a ``Set-Cookie`` ``Domain=`` attribute may be honored for a
response served by ``host``.
Mirrors RFC 6265 §5.1.3 domain-matching (and real-browser behavior): the
host must equal the cookie domain or be a subdomain of it. A leading dot
on the attribute is ignored. Returns False for a Domain that points at a
DIFFERENT or unrelated registrable domain — a cookie a browser drops —
so the caller can fall back to host-scoping instead of letting a redirect
hop plant a cookie on someone else's domain (session fixation).
Note: this does NOT consult the Public Suffix List, so a suffix like
``Domain=co.uk`` set by ``a.co.uk`` is honored here and could be re-sent
to a sibling host later in the SAME redirect chain. The residual is very
narrow — the browser context is ephemeral (one fetch) and Chromium
enforces the PSL on its own jar — and it is strictly better than the
pre-fix behavior, which forwarded all cookies cross-domain. Wiring in a
PSL check would tighten it further (optional follow-up).
"""
if not host or not cookie_domain:
return False
host = host.lower().rstrip(".")
cookie_domain = cookie_domain.lower().lstrip(".").rstrip(".")
if not cookie_domain:
return False
return host == cookie_domain or host.endswith("." + cookie_domain)
def _same_registrable_site(host_a: str, host_b: str) -> bool:
"""Whether two request hosts belong to the same domain family for the
purpose of forwarding credentialed request headers across a redirect hop.
Mirrors the host/subdomain relation ``_cookie_domain_matches`` already
uses (RFC 6265 §5.1.3 style): equal hosts, or one a subdomain of the
other, count as the SAME site; anything else is cross-site. Like that
helper it does NOT consult the Public Suffix List, so it is a deliberately
conservative approximation of "registrable domain" — consistent with the
rest of this guard, while Chromium still enforces its own PSL on the
browser jar. Used to decide when to drop ``Authorization`` /
``Proxy-Authorization`` on a cross-site hop, the twin of the per-hop
``Cookie`` re-scoping (a browser strips credentials cross-origin too).
Fail-CLOSED: a missing/empty host on either side returns False (treated as
cross-site), so credentialed headers are dropped rather than forwarded on
any ambiguity.
"""
if not host_a or not host_b:
return False
a = host_a.lower().rstrip(".")
b = host_b.lower().rstrip(".")
if not a or not b:
return False
return a == b or a.endswith("." + b) or b.endswith("." + a)
def _parse_one_set_cookie(raw: str):
"""Parse a single ``Set-Cookie`` header value into ``(name, value, attrs)``.
A manual RFC 6265 §5.2 split rather than ``http.cookies.SimpleCookie``:
SimpleCookie silently DROPS cookies whose name is not a strict RFC token
(e.g. ``data[uid]``, which real browsers accept), so those cookies would
vanish from the re-applied jar. This split keeps them.
Returns ``None`` when the header has no ``name=value`` pair. ``attrs``
keys are lowercased; value-less flag attributes (``Secure``, ``HttpOnly``)
map to ``True``. Each ``Set-Cookie`` header carries exactly one cookie
(RFC 6265 §3), and the only comma that can appear inside a value belongs
to an ``Expires`` HTTP-date, which never contains a semicolon — so a
plain ``;`` split is safe.
"""
if not raw:
return None
parts = raw.split(";")
name, sep, value = parts[0].partition("=")
name = name.strip()
if not sep or not name:
return None
value = value.strip()
# Strip one layer of surrounding DQUOTEs (mirrors SimpleCookie).
if len(value) >= 2 and value[0] == '"' and value[-1] == '"':
value = value[1:-1]
attrs = {}
for segment in parts[1:]:
akey, asep, aval = segment.partition("=")
akey = akey.strip().lower()
if not akey:
continue
attrs[akey] = aval.strip() if asep else True
return name, value, attrs
def _set_cookie_is_expired(attrs: dict) -> bool:
"""Whether a parsed ``Set-Cookie`` is a deletion/expired directive that
must NOT be re-applied to the jar.
A ``Max-Age=0`` (or negative) or a past ``Expires`` is how a server
DELETES a cookie; re-adding it as a live cookie would resurrect a value
the server just cleared. ``Max-Age`` takes precedence over ``Expires``
(RFC 6265 §5.3). Fail-OPEN: an unparseable attribute is treated as NOT
expired, so a live cookie is never dropped by a date-parsing quirk.
"""
max_age = attrs.get("max-age")
if isinstance(max_age, str) and max_age.strip():
try:
return int(max_age) <= 0
except ValueError:
pass # Unparseable Max-Age -> fall through to Expires.
expires = attrs.get("expires")
if isinstance(expires, str) and expires.strip():
try:
dt = parsedate_to_datetime(expires)
if dt is None:
return False
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt <= datetime.now(timezone.utc)
except Exception:
return False
return False
def _run_async(coro, timeout: float = None):
"""Run an async coroutine from synchronous code.
Handles the case where an event loop is already running
(e.g. inside Jupyter or an async framework) by creating
a new thread with its own loop.
Args:
coro: The coroutine to run.
timeout: Max seconds to wait for the result. Prevents
indefinite hangs if the coroutine's internal timeout fails.
"""
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if loop is None:
return asyncio.run(coro)
# Already inside an event loop — run in a new thread
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
future = pool.submit(asyncio.run, coro)
return future.result(timeout=timeout)
class PlaywrightHTMLDownloader(HTMLDownloader):
"""HTML downloader with JS rendering via Crawl4AI or Playwright.
Default: Crawl4AI (robots.txt, shadow DOM, iframes, caching).
Fallback: plain Playwright if Crawl4AI is not installed.
No stealth or anti-detection features are used.
"""
def __init__(
self,
timeout: int = 30,
language: str = "English",
wait_until: str = "networkidle",
block_resources: bool = True,
allow_private_ips: bool = False,
*,
block_link_local: bool = False,
**kwargs,
):
super().__init__(timeout=timeout, language=language, **kwargs)
self.wait_until = wait_until
self.block_resources = block_resources
# SSRF policy for the browser route guards (initial navigation +
# every redirect hop + subresources). Defaults strict; relaxed to
# True only under the PRIVATE_ONLY egress scope so local lab hosts
# remain reachable. Cloud-metadata IPs stay blocked regardless
# (``ssrf_validator`` always rejects them). Mirrors the static
# SafeSession path's ``allow_private_ips`` handling.
self.allow_private_ips = allow_private_ips
# Link-local carve-out inside the private-IP grant (see
# AutoHTMLDownloader): both route guards validate every hop with it,
# so a redirect into the link-local range is refused on the JS route
# exactly as the static route refuses it. No effect without
# ``allow_private_ips``.
self.block_link_local = block_link_local
# The guarded robots.txt check (below) reuses this SafeSession, whose
# send() SSRF-validates every hop; keep its policy in lockstep with
# the browser guard so both honour the same egress scope.
if hasattr(self.session, "allow_private_ips"):
self.session.allow_private_ips = allow_private_ips
if hasattr(self.session, "block_link_local"):
self.session.block_link_local = block_link_local
# Plain Playwright fallback state
self._playwright = None
self._browser = None
# ------------------------------------------------------------------
# Per-request egress guard for the JS-render browser paths.
#
# WHY a manual redirect loop instead of "validate the request URL and
# continue": Playwright does NOT re-fire ``page.route`` handlers for the
# hops of a server-side redirect chain — the browser follows 3xx
# responses internally, below the interception layer. So a
# ``route.continue_()`` / ``route.fallback()`` on a public URL that
# 302-redirects to a cloud-metadata endpoint (link-local) or an RFC1918
# host would let the browser reach the internal target unchecked —
# exactly the gap the static ``SafeSession`` path already closes by
# validating every hop. To reach parity we follow the redirect chain
# ourselves with ``route.fetch(max_redirects=0)``, validate EACH hop's
# target before fetching it, and hand the browser only the final, safe
# response via ``route.fulfill``. The browser therefore never issues its
# own redirect-following request to an unvalidated destination.
# ------------------------------------------------------------------
def _playwright_route_guard(self, route) -> None:
"""Egress guard for the plain-Playwright (sync) browser path.
Validates the initial navigation and every redirect hop against
``ssrf_validator.validate_url`` before the browser connects, and
drops heavy subresources when ``block_resources`` is set. Also
re-scopes cookies per redirect hop (via ``_reapply_hop_cookies_sync``)
so a cookie set on one hop is never forwarded to a different
registrable domain a later hop redirects to — the same protection the
crawl4ai path applies via ``_reapply_hop_cookies``.
"""
if not _browser_transport_allowed(self.session):
route.abort("blockedbyclient")
return
request = route.request
first_url = request.url
scheme = urlparse(first_url).scheme.lower()
if scheme not in ("http", "https"):
# ALLOWLIST, fail-closed: only data:/blob:/about: (never touch the
# network) may fall through to the browser. Every other non-http(s)
# scheme — file: (local file read), chrome:, gopher:, ftp: … — is
# aborted so a redirect or page resource cannot pivot the browser
# to a local file or an internal handler.
if scheme in _FALLTHROUGH_NON_HTTP_SCHEMES:
route.fallback()
else:
route.abort()
return
if (
self.block_resources
and request.resource_type in _BLOCKED_RESOURCE_TYPES
):
route.abort()
return
current = first_url
method = request.method
# None => reuse the intercepted request's body; "" => drop the body
# after a POST->GET redirect downgrade (parity with safe_post).
body_override = None
# Per-hop "Cookie" header, re-derived from the browser context's
# cookie jar (scoped to the NEXT hop's URL) on EVERY hop by
# _reapply_hop_cookies_sync. It stays None only for the very first
# fetch, which legitimately uses the originally intercepted request's
# own Cookie header; from the first redirect onward it is always set
# (possibly to an explicit EMPTY header). This is required because
# route.fetch() otherwise keeps resending the ORIGINAL intercepted
# request's Cookie header for the rest of this handler invocation —
# so without a per-hop override a cookie scoped to hop N's domain
# would ride along to a cross-host hop N+1 (a cross-origin cookie
# leak). Mirrors the crawl4ai path's _reapply_hop_cookies.
cookie_header_override = None
for _ in range(_MAX_REDIRECTS + 1):
if not ssrf_validator.validate_url(
current,
allow_private_ips=self.allow_private_ips,
block_link_local=self.block_link_local,
):
# Log scheme://host:port only — a request URL may carry
# credentials in userinfo / query params (RFC 3986 §3.2.1).
logger.warning(
"Playwright: blocked SSRF-unsafe browser request to {}",
ssrf_validator.redact_url_for_log(current),
)
route.abort("blockedbyclient")
return
fetch_kwargs = {
"url": current,
"method": method,
"max_redirects": 0,
}
if body_override is not None:
fetch_kwargs["post_data"] = body_override
try:
self._apply_hop_cookie_header(
request, fetch_kwargs, cookie_header_override
)
except Exception:
# Fail CLOSED: building the hop headers reads
# request.headers and must never let an exception escape
# this route handler and fall through to an unguarded
# fetch — abort the hop exactly like a failed fetch below.
logger.opt(exception=False).debug(
"Playwright: failed to build guarded hop headers"
)
route.abort("failed")
return
try:
response = route.fetch(**fetch_kwargs)
except Exception:
logger.opt(exception=False).debug(
"Playwright: guarded fetch failed"
)
route.abort("failed")
return
next_url = self._redirect_target(current, response)
# Re-apply this hop's Set-Cookie header(s) to the context AND
# re-derive, from the context jar scoped to the NEXT hop, the
# explicit Cookie header for that hop's route.fetch() — BEFORE
# following it. next_url is None on the terminal hop (no next
# request needs a header). See _reapply_hop_cookies for why the
# per-hop re-derivation is required.
cookie_header_override = self._reapply_hop_cookies_sync(
route,
current,
response,
next_url,
)
if next_url is None:
route.fulfill(response=response)
return
new_method = _resolve_redirect_method(method, response.status)
if new_method == "GET" and method != "GET":
body_override = "" # POST->GET downgrade must not leak body
method = new_method
current = next_url
logger.warning(
"Playwright: too many redirects — blocking browser request"
)
route.abort("blockedbyclient")
def _playwright_ws_guard(self, ws_route) -> None:
"""Block every WebSocket on the plain-Playwright path.
``route("**/*")`` does not match WS upgrades, so page JS could open
``ws(s)://<internal>`` unvalidated. WebSockets are not needed for
HTML text extraction, so reject all of them: simply returning
WITHOUT calling ``connect_to_server`` means the connection to the
real server is never established (verified: the target sees zero
connections). We deliberately do NOT call ``ws_route.close()`` here —
in Playwright's SYNC API a blocking route-handler call re-enters the
dispatcher thread that ``page.goto`` is parked on and DEADLOCKS; the
no-op form blocks the server just as effectively without that risk.
"""
try:
logger.debug(
"Playwright: blocking WebSocket to {}",
ssrf_validator.redact_url_for_log(ws_route.url),
)
except Exception:
logger.debug("Playwright: blocking WebSocket")
async def _crawl4ai_route_guard(self, route) -> None:
"""Egress guard for the Crawl4AI (async Playwright) browser path.
Same policy as ``_playwright_route_guard`` but async: the blocking
DNS resolution inside ``validate_url`` is offloaded to a thread so it
does not stall Crawl4AI's event loop. Installed on each page context
via the ``on_page_context_created`` hook.
"""
if not _browser_transport_allowed(self.session):
await route.abort("blockedbyclient")
return
request = route.request
first_url = request.url
scheme = urlparse(first_url).scheme.lower()
if scheme not in ("http", "https"):
# ALLOWLIST, fail-closed (see _playwright_route_guard): only
# data:/blob:/about: fall through; every other non-http(s) scheme
# (file:/chrome:/gopher: …) is aborted so it cannot pivot the
# browser to a local file or an internal handler.
if scheme in _FALLTHROUGH_NON_HTTP_SCHEMES:
await route.fallback()
else:
await route.abort()
return
if (
self.block_resources
and request.resource_type in _BLOCKED_RESOURCE_TYPES
):
await route.abort()
return
loop = asyncio.get_running_loop()
current = first_url
method = request.method
body_override = None
# Carries the "Cookie" header for the NEXT hop, re-derived from the
# context's cookie jar (scoped to that hop's URL) on EVERY hop by
# _reapply_hop_cookies. It stays None only for the very first fetch,
# which legitimately uses the originally intercepted request's own
# Cookie header; from the first redirect onward it is always set
# (possibly to an explicit EMPTY header). This is required because
# route.fetch() otherwise keeps resending the ORIGINAL intercepted
# request's Cookie header for the rest of this handler invocation —
# so without a per-hop override a cookie scoped to hop N's domain
# would ride along to hop N+1 even across a DIFFERENT registrable
# domain (a cross-origin cookie leak).
cookie_header_override = None
for _ in range(_MAX_REDIRECTS + 1):
ok = await loop.run_in_executor(
None,
functools.partial(
ssrf_validator.validate_url,
current,
allow_private_ips=self.allow_private_ips,
block_link_local=self.block_link_local,
),
)
if not ok:
logger.warning(
"Crawl4AI: blocked SSRF-unsafe browser request to {}",
ssrf_validator.redact_url_for_log(current),
)
await route.abort("blockedbyclient")
return
fetch_kwargs = {
"url": current,
"method": method,
"max_redirects": 0,
}
if body_override is not None:
fetch_kwargs["post_data"] = body_override
try:
self._apply_hop_cookie_header(
request, fetch_kwargs, cookie_header_override
)
except Exception:
# Fail CLOSED: building the hop headers reads
# request.headers and must never let an exception escape
# this route handler and fall through to an unguarded
# fetch — abort the hop exactly like a failed fetch below.
logger.opt(exception=False).debug(
"Crawl4AI: failed to build guarded hop headers"
)
await route.abort("failed")
return
try:
response = await route.fetch(**fetch_kwargs)
except Exception:
logger.opt(exception=False).debug(
"Crawl4AI: guarded fetch failed"
)
await route.abort("failed")
return
next_url = self._redirect_target(current, response)
# Re-apply this hop's Set-Cookie header(s) to the context AND
# re-derive, from the context jar scoped to the NEXT hop, the
# explicit Cookie header for that hop's route.fetch() — BEFORE
# following it. Passing next_url (which is None on the terminal
# hop) lets _reapply_hop_cookies skip deriving a header when
# there is no next request. See _reapply_hop_cookies for why the
# per-hop re-derivation is required on the crawl4ai path.
cookie_header_override = await self._reapply_hop_cookies(
route,
current,
response,
next_url,
)
if next_url is None:
await route.fulfill(response=response)
return
new_method = _resolve_redirect_method(method, response.status)
if new_method == "GET" and method != "GET":
body_override = "" # POST->GET downgrade must not leak body
method = new_method
current = next_url
logger.warning(
"Crawl4AI: too many redirects — blocking browser request"
)
await route.abort("blockedbyclient")
async def _crawl4ai_ws_guard(self, ws_route) -> None:
"""Block every WebSocket on the Crawl4AI path (async twin of
``_playwright_ws_guard``).
Same no-op-reject strategy: not calling ``connect_to_server`` leaves
the real server unreached. Kept no-op (no ``close()``) for symmetry
with the sync guard and to avoid any handler-reentrancy surprises.
"""
try:
logger.debug(
"Crawl4AI: blocking WebSocket to {}",
ssrf_validator.redact_url_for_log(ws_route.url),
)
except Exception:
logger.debug("Crawl4AI: blocking WebSocket")
@staticmethod
def _cookies_from_response(response, response_url: str) -> list:
"""Parse ``Set-Cookie`` response header(s) into Playwright cookie
dicts, ready for ``BrowserContext.add_cookies()``.
WHY THIS EXISTS: both browser guards re-derive the ``Cookie`` request
header per redirect hop from the context's own jar, so this parses a
hop's ``Set-Cookie`` header(s) and re-applies them to the jar before
the next hop is followed. On the crawl4ai path it is also load-bearing
for content fidelity: crawl4ai pre-seeds the page context with a
marker cookie via ``context.add_cookies()`` before navigation
(``BrowserManager.setup_context``), and once a context's jar has been
touched that way, Playwright's ``route.fetch()`` on a subsequent
redirect hop does NOT merge that hop's ``Set-Cookie`` into the jar for
us — so a session/auth cookie set on an intermediate hop would
silently be lost, corrupting the final page (e.g. a login- or
region-gated redirect).
A ``Set-Cookie`` that is really a DELETION directive (``Max-Age=0`` or
a past ``Expires``) is honored and NOT re-added, so a cookie the
server just cleared is never resurrected onto the jar.
Uses ``headers_array`` (a list of ``{name, value}`` pairs) rather
than the ``headers`` dict, because a response can carry MULTIPLE
``Set-Cookie`` headers — a plain dict would silently collapse them
to one.
"""
host = urlparse(response_url).hostname
if not host:
return []
try:
headers = response.headers_array
except Exception:
return []
cookies = []
for header in headers:
if str(header.get("name", "")).lower() != "set-cookie":
continue
raw = header.get("value", "")
try:
parsed = _parse_one_set_cookie(raw)
except Exception:
logger.opt(exception=False).debug(
"Browser guard: failed to parse a Set-Cookie header"
)
continue
if parsed is None:
continue
name, value, attrs = parsed
if _set_cookie_is_expired(attrs):
# A Max-Age=0 / past-Expires Set-Cookie is a DELETION
# directive: re-adding it would resurrect a cookie the
# server just cleared. Honor the expiry and skip it.
logger.debug(
"Browser guard: skipping expired/deletion Set-Cookie"
)
continue
raw_domain = attrs.get("domain")
if (
isinstance(raw_domain, str)
and raw_domain
and _cookie_domain_matches(host, raw_domain)
):
cookie_domain = raw_domain
else:
# No Domain attribute, or a Domain that does NOT
# domain-match the response host (a cross-domain or
# unrelated Domain a browser rejects). Scope the cookie
# to the response host — mirroring browser behavior and
# preventing a hop from planting a cookie on another
# registrable domain (session fixation).
if isinstance(raw_domain, str) and raw_domain:
logger.debug(
"Browser guard: rejecting a Set-Cookie Domain that "
"does not match the response host; scoping the "
"cookie to the host instead"
)
cookie_domain = host
path = attrs.get("path")
cookie = {
"name": name,
"value": value,
"domain": cookie_domain,
"path": path if isinstance(path, str) and path else "/",
}
if attrs.get("secure"):
cookie["secure"] = True
if attrs.get("httponly"):
cookie["httpOnly"] = True
cookies.append(cookie)
return cookies
async def _reapply_hop_cookies(
self,
route,
current_url: str,
response,
next_hop_url: Optional[str],
) -> Optional[str]:
"""Apply ``response``'s ``Set-Cookie`` header(s) to the browser
context and return the ``Cookie`` header the caller must pass to the
NEXT hop's ``route.fetch(headers=...)`` — re-derived, on EVERY hop,
from the context jar scoped to ``next_hop_url``.
Why re-derive on every hop instead of carrying the previous hop's
header forward: ``route.fetch()`` keeps resending the ORIGINAL
intercepted request's Cookie header for the rest of this guard
invocation unless we override it. If we ever carried a previous
hop's Cookie header (or let the original one ride along) across a
redirect to a DIFFERENT registrable domain, that domain would
receive cookies scoped to the previous one — a cross-origin cookie
leak a real browser never performs. So we ALWAYS read the jar back
through ``context.cookies(next_hop_url)`` (Playwright scopes it by
domain/path) and return exactly those cookies:
* same-domain next hop -> the jar returns that domain's cookies
(including any just set on this hop, applied below via
``add_cookies``), so same-domain persistence keeps working;
* cross-domain next hop -> the jar returns nothing for that domain,
so we return an EMPTY header and the caller sends NO Cookie to it.
``context.add_cookies()`` still runs for its own sake: it keeps the
jar correct for anything reading it independently of this walk
(subresources of the eventually-fulfilled page, ``document.cookie``,
later navigations in the same context).
Fail-SAFE: on any parse/apply/read error we return an EMPTY header
rather than a previous hop's, so an error can only DROP a cookie,
never leak one cross-domain. This never affects the SSRF
validate/abort decisions around it. Returns ``None`` only for the
terminal hop (``next_hop_url is None`` — no next request needs a
header); that value is unused by the caller.
"""
try:
context = route.request.frame.page.context
cookies = self._cookies_from_response(response, current_url)
if cookies:
await context.add_cookies(cookies)
if next_hop_url is None:
# Terminal hop: no next request, so no Cookie header to
# derive. The jar was still updated above.
return None
live_cookies = await context.cookies(next_hop_url)
return "; ".join(f"{c['name']}={c['value']}" for c in live_cookies)
except Exception:
logger.opt(exception=False).debug(
"Crawl4AI: failed to re-apply redirect-hop cookies"
)
# Prefer sending NO cookie to the next hop over resending a
# previous hop's (possibly cross-domain) one: "" clears the
# Cookie header. None is used only when there is no next hop.
return "" if next_hop_url is not None else None
def _reapply_hop_cookies_sync(
self,
route,
current_url: str,
response,
next_hop_url: Optional[str],
) -> Optional[str]:
"""Synchronous twin of ``_reapply_hop_cookies`` for the plain
(sync-API) Playwright path.
Same contract and fail-SAFE behavior — see ``_reapply_hop_cookies``
for the full rationale. Applies this hop's ``Set-Cookie`` header(s)
to the browser context and returns the ``Cookie`` header for the NEXT
hop, re-derived from the context jar scoped to ``next_hop_url`` so a
cookie belonging to one domain is never forwarded to a different
registrable domain a later redirect lands on. Returns ``None`` only
for the terminal hop; on any error returns an EMPTY header (drop a
cookie, never leak one cross-domain).
"""
try:
context = route.request.frame.page.context
cookies = self._cookies_from_response(response, current_url)
if cookies:
context.add_cookies(cookies)
if next_hop_url is None:
# Terminal hop: no next request, so no Cookie header to
# derive. The jar was still updated above.
return None
live_cookies = context.cookies(next_hop_url)
return "; ".join(f"{c['name']}={c['value']}" for c in live_cookies)
except Exception:
logger.opt(exception=False).debug(
"Playwright: failed to re-apply redirect-hop cookies"
)
return "" if next_hop_url is not None else None
@staticmethod
def _apply_hop_cookie_header(
request, fetch_kwargs: dict, cookie_header_override: Optional[str]
) -> None:
"""Override ONLY the ``Cookie`` header on ``fetch_kwargs`` with the
per-hop re-derived ``cookie_header_override``.
Shared by both browser guards (sync + crawl4ai). Rebuilds the base
headers ``route.fetch()`` would otherwise default to (the original
intercepted request's) MINUS its ``Cookie`` header, then sets the
freshly re-derived Cookie header. When that value is EMPTY, the Cookie
header is dropped entirely rather than sent empty: the next hop's
domain has no cookies in the jar, so sending nothing is correct — and,
crucially, this stops ``route.fetch()`` from resending the ORIGINAL
intercepted request's Cookie header to what may be a DIFFERENT
registrable domain (the cross-origin leak this guards against).
The original request's ``Host`` and ``Content-Length`` are ALSO dropped
from the rebuilt headers on every hop: a redirect hop's ``Host`` must be
derived from the NEW target (a forwarded stale ``Host`` is wrong on a
cross-host hop), and after a POST->GET downgrade (body dropped to "")
the original ``Content-Length`` no longer matches the body.
``route.fetch()`` recomputes both from the hop URL and the actual body
once these headers are absent.
On a CROSS-SITE hop the originally intercepted request's
``Authorization`` / ``Proxy-Authorization`` headers are ALSO dropped
(in addition to ``Cookie``): those credentials are scoped to the
original origin, so — exactly as a real browser strips Authorization
on a cross-origin redirect — they must never ride along to a different
registrable domain a later hop lands on. Same-site hops keep them so
legitimate within-site auth redirects (e.g. api.example.com ->
example.com) still work. Cross-site is decided by
``_same_registrable_site`` — the same host/subdomain notion the cookie
re-scoping relies on.
A ``None`` override (the very first hop only) leaves ``fetch_kwargs``
untouched so the initial request keeps its own Cookie header. The very
first hop is by construction same-origin (its target IS the original
request URL), so no credential is ever leaked by that early return.
"""
if cookie_header_override is None:
return
# Dropped on EVERY (post-first) hop, before rebuilding the headers:
# * cookie — always re-derived per hop below.
# * host — a redirect hop's Host must come from the NEW target, not a
# stale forwarded value; a cross-host hop that kept the previous
# hop's Host would send the wrong Host. ``route.fetch()`` derives the
# correct Host from the hop URL once this header is absent.
# * content-length — after a POST->GET redirect downgrade the body is
# dropped to "" (see the guard loop's ``body_override``), so a
# forwarded original Content-Length would be incorrect;
# ``route.fetch()`` recomputes it from the actual (post_data) body.
drop = {"cookie", "host", "content-length"}
# On a cross-site hop also strip the credential headers the original
# request carried (a browser strips Authorization cross-origin too).
origin_host = urlparse(request.url).hostname
target_host = urlparse(fetch_kwargs.get("url", "")).hostname
if not _same_registrable_site(origin_host, target_host):
drop |= {"authorization", "proxy-authorization"}
base_headers = {
k: v for k, v in request.headers.items() if k.lower() not in drop
}
if cookie_header_override:
base_headers["cookie"] = cookie_header_override
fetch_kwargs["headers"] = base_headers
@staticmethod
def _redirect_target(current_url: str, response) -> Optional[str]:
"""Return the absolute redirect target for ``response``, or None.
None means the response is terminal (not a followed 3xx, or a 3xx
with no ``Location``) and should be served to the browser as-is.
Mirrors the static path's redirect handling so both validate the
same hops.
"""
if response.status not in _REDIRECT_STATUS_CODES:
return None
location = response.headers.get("location")
if not location:
return None
return urljoin(current_url, location.strip())
def _robots_allows(self, url: str) -> bool:
"""Guarded robots.txt politeness check.
REPLACES Crawl4AI's built-in ``check_robots_txt`` fetch, which used a
raw ``aiohttp`` client (redirect-following, no SSRF check) — a
``/robots.txt`` that 302-redirected to an internal/metadata host was
fetched unguarded. Here the fetch goes through the downloader's
``SafeSession``, whose ``send()`` SSRF-validates every hop (initial +
redirects) under the same egress scope as the browser guard.
Fails OPEN (returns True) on any non-200, network error, SSRF block,
or parse failure — matching the "allow on robots uncertainty"
convention. It only ever DISALLOWS on an explicit robots rule, so a
blocked (SSRF-unsafe) robots fetch never grants extra access.
"""
from urllib.robotparser import RobotFileParser
try:
# A backslash, whitespace or control character makes urlparse
# and browsers disagree about the host (``http://a\\@b/`` is
# host ``a`` to a browser, ``b`` to urlparse). Such a URL is
# refused by the SSRF validator anyway; do not derive a robots
# request for another host from it.
if ssrf_validator.RFC_FORBIDDEN_URL_CHARS_RE.search(url):
return True
parsed = urlparse(url)
# Keep the authority's bracket and port syntax while omitting
# any userinfo from this derived request.
authority = parsed.netloc.rpartition("@")[2]
if not authority:
return True
scheme = parsed.scheme or "http"
robots_url = f"{scheme}://{authority}/robots.txt"
response = self.session.get(
robots_url,
timeout=min(self.timeout, 10),
allow_redirects=True,
)
if response.status_code != 200 or not response.text:
return True
parser = RobotFileParser()
parser.parse(response.text.splitlines())
return parser.can_fetch(BROWSER_USER_AGENT, url)
except PolicyDeniedError:
raise
except Exception:
# SafeSession raises ValueError on an SSRF-unsafe hop; network /
# parse errors land here too. Fail open for politeness — the
# actual navigation is still validated by the browser guard.
logger.opt(exception=False).debug(
"Guarded robots.txt check failed open"
)
return True
def _fetch_html(self, url: str) -> Optional[str]:
"""Fetch HTML with JS rendering.
Tries Crawl4AI first (with robots.txt, shadow DOM, iframes),
falls back to plain Playwright.
"""
if not _browser_transport_allowed(self.session):
raise PolicyDeniedError(
Decision(False, "browser_transport_cannot_enforce_scope"), url
)
# Entry-URL scheme guard (fail-closed). The browser downloader is
# reachable via download()/download_with_result(), which do NOT
# constrain the scheme themselves — only can_handle() checks it, and
# not every caller consults can_handle() (e.g.
# extraction/pipeline.py calls download() directly). The per-request
# route guard validates redirect hops and subresources, but a
# top-level page.goto()/crawler.arun() on a non-http(s) entry URL
# (e.g. file:///etc/passwd) could read a LOCAL FILE and return it as
# page content before any hop is walked. Refuse non-http(s) entry URLs
# here so such a URL is never navigated.
if urlparse(url).scheme.lower() not in ("http", "https"):
logger.warning(
"Browser downloader: refusing non-http(s) entry URL {}",
ssrf_validator.redact_url_for_log(url),
)
return None
# Try Crawl4AI first (richer features, robots.txt)
html = self._fetch_with_crawl4ai(url)
if html is not None:
# Crawl4AI succeeded (non-empty) or intentionally blocked
# by robots.txt (empty string). Either way, don't fall
# through to Playwright.
return html or None
# Crawl4AI not installed or failed — fall back to Playwright
return self._fetch_with_playwright(url)
def _fetch_with_crawl4ai(self, url: str) -> Optional[str]:
"""Fetch HTML using Crawl4AI with ethical defaults."""
engine_type = f"crawl4ai_download_{rate_limit_authority(url)}"
try:
from crawl4ai import (
AsyncWebCrawler,
BrowserConfig,
CrawlerRunConfig,
)
except ImportError:
logger.debug(
"crawl4ai.unavailable url={url}",
url=redact_url_for_log(url),
)
return None
# Ethical robots.txt check — done HERE through the guarded
# SafeSession, NOT via crawl4ai's check_robots_txt (disabled below):
# crawl4ai fetches robots.txt with a raw aiohttp client that follows
# redirects with no SSRF validation, so a redirecting /robots.txt is
# an SSRF vector that bypasses the browser route guard entirely.
if not self._robots_allows(url):
logger.info(
"Crawl4AI: blocked by robots.txt for {}",
ssrf_validator.redact_url_for_log(url),
)
return "" # Empty string signals intentional skip
logger.debug(
"crawl4ai.fetch_started url={url}",
url=redact_url_for_log(url),
)
wait_time = self.rate_tracker.apply_rate_limit(engine_type)
browser_cfg = BrowserConfig(
headless=True,
verbose=False,
user_agent=BROWSER_USER_AGENT,
)
run_cfg = CrawlerRunConfig(
# robots.txt handled by the guarded _robots_allows() above;
# crawl4ai's own fetch is unguarded (SSRF), so keep it OFF.
check_robots_txt=False,
# Better extraction: flatten modern web features
flatten_shadow_dom=True,
process_iframes=True,
# Trigger lazy-loaded content
scan_full_page=True,
# Performance
wait_until=self.wait_until,
page_timeout=self.timeout * 1000,
exclude_all_images=self.block_resources,
# No stealth
override_navigator=False,
magic=False,
simulate_user=False,
verbose=False,
)
try:
async def _crawl():
async with AsyncWebCrawler(config=browser_cfg) as crawler:
# Install the SSRF route guard on every page context
# BEFORE any navigation. FAIL-SAFE: if the hook API is
# unavailable (crawl4ai API drift), do NOT run crawl4ai
# unguarded — return None so _fetch_html falls through
# to the guarded plain-Playwright path.
async def _on_page_context_created(
page, context=None, config=None, **kwargs
):
# Install ALL three guards fail-CLOSED: if any raises
# (crawl4ai/Playwright API drift, or a context with
# incomplete API parity), let it propagate so the
# crawl is abandoned — crawl4ai's execute_hook does not
# swallow it, so it reaches the outer ``except`` below
# and _fetch_with_crawl4ai returns None, falling back
# to the guarded plain-Playwright path. Never navigate
# with a missing HTTP, WebSocket, or SW guard.
target = context if context is not None else page
# HTTP(S) requests: SSRF-validate every hop.
await target.route("**/*", self._crawl4ai_route_guard)
# WebSockets: route("**/*") does not match WS upgrades
# — block them explicitly.
await target.route_web_socket(
"**/*", self._crawl4ai_ws_guard
)
# Service Workers: crawl4ai's BrowserConfig cannot set
# service_workers="block", and SW-made requests bypass
# route handlers — prevent SW registration via an init
# script so all traffic stays on the guarded path.
await target.add_init_script(
_DISABLE_SERVICE_WORKERS_JS
)
# WebRTC: RTCPeerConnection ICE (STUN/TURN) opens raw
# UDP/TCP sockets via Chromium's WebRTC stack, which
# neither route() nor route_web_socket() intercept —
# neutralize it before any page script runs.
await target.add_init_script(_DISABLE_WEBRTC_JS)
return page
try:
crawler.crawler_strategy.set_hook(
"on_page_context_created",
_on_page_context_created,
)
except Exception:
logger.warning(
"Crawl4AI: could not install SSRF route guard — "
"refusing to crawl unguarded, falling back to "
"the guarded Playwright path"
)
return None
return await crawler.arun(url=url, config=run_cfg)
# A guard-install failure raised inside the hook propagates out of
# crawl4ai's execute_hook and arun, landing in the outer ``except``
# below (return None -> guarded plain-Playwright fallback). This is
# the fail-CLOSED path: a crawl NEVER proceeds with a missing HTTP,
# WebSocket, or Service-Worker guard.
result = _run_async(_crawl(), timeout=self.timeout + 30)
if result is None:
# Guard could not be installed (fail-safe) — defer to the
# guarded plain-Playwright path rather than crawl unguarded.
return None
if result.success and result.html:
html = result.html
logger.debug(
"crawl4ai.fetch_succeeded url={url} content_length={content_length}",
url=redact_url_for_log(url),
content_length=len(html),
)
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=wait_time,
success=True,
retry_count=1,
search_result_count=1,
)
return html
# Check if blocked by robots.txt
error_msg = getattr(result, "error_message", "") or ""
if "robots.txt" in error_msg.lower():
logger.info(
"crawl4ai.robots_denied url={url}",
url=redact_url_for_log(url),
)
# Don't fall back to Playwright — respect the block
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=wait_time,
success=False,
retry_count=1,
error_type="robots_txt_blocked",
)
return "" # Empty string signals intentional skip
status = getattr(result, "status_code", "unknown")
logger.debug(
"crawl4ai.fetch_failed url={url}",
url=redact_url_for_log(url),
)
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=wait_time,
success=False,
retry_count=1,
error_type=f"crawl4ai_status_{status}",
)
return None
except PolicyDeniedError:
raise
except Exception as e:
# DEBUG, not ERROR: this except is normal control flow — it
# returns None, so _fetch_html falls back to
# _fetch_with_playwright and the fetch usually then succeeds.
# Logging this at ERROR would write a false-alarm entry into the
# encrypted DB sink and the frontend progress stream for every
# crawl4ai hiccup, even when the overall fetch didn't fail.
logger.opt(exception=False).debug(
"crawl4ai.fetch_error url={url} error_type={error_type}",
url=redact_url_for_log(url),
error_type=type(e).__name__,
)
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=wait_time,
success=False,
retry_count=1,
error_type=type(e).__name__,
)
return None
def _fetch_with_playwright(self, url: str) -> Optional[str]:
"""Fetch HTML using plain Playwright (fallback)."""
if not self._robots_allows(url):
logger.info(
"Playwright: blocked by robots.txt for {}",
ssrf_validator.redact_url_for_log(url),
)
return None
logger.debug(
"playwright.fetch_started url={url}",
url=redact_url_for_log(url),
)
engine_type = f"playwright_download_{rate_limit_authority(url)}"
wait_time = self.rate_tracker.apply_rate_limit(engine_type)
try:
from playwright.sync_api import sync_playwright
# Lazy-init browser (reuse across multiple fetches).
# --no-sandbox: Chromium needs SYS_ADMIN to set up its user-namespace
# sandbox; the production container drops that cap. Without this
# flag, launch() crashes inside Docker. Crawl4AI's own arg list
# already includes it; this fallback path was missing it.
# --disable-dev-shm-usage: Docker's default /dev/shm is 64 MB,
# which Chromium can blow through and OOM. Use /tmp instead.
if self._browser is None:
logger.debug(
"playwright.launch_started url={url}",
url=redact_url_for_log(url),
)
pw = sync_playwright().start()
try:
self._browser = pw.chromium.launch(
headless=True,
args=["--no-sandbox", "--disable-dev-shm-usage"],
)
except Exception:
pw.stop()
raise
self._playwright = pw
# ``browser.new_page()`` creates a FRESH BrowserContext for this
# fetch (Playwright makes the page own that context) — only
# ``self._browser`` is cached across fetches, NOT the context. The
# route/WS handlers and init scripts below are therefore registered
# on a per-fetch context that ``page.close()`` (in the finally)
# tears down together with them, so they do NOT accumulate across
# fetches; each fetch gets exactly one of each guard.
page = self._browser.new_page(
user_agent=BROWSER_USER_AGENT,
# Block Service Workers: their requests are not intercepted by
# route handlers, so a SW could fetch an internal/metadata
# host unvalidated. Blocking forces all traffic through the
# guarded normal path (SWs aren't needed for text extraction).
service_workers="block",
)
try:
# Egress guard for EVERY request: validates the initial
# navigation and each redirect hop before the browser
# connects (see _playwright_route_guard), and also drops
# heavy subresources when block_resources is set — so it
# subsumes the old extension-glob resource route. Registered
# at the CONTEXT level so it also covers requests a blocked
# SW would otherwise have made.
page.context.route("**/*", self._playwright_route_guard)
# WebSockets bypass route("**/*") — block them explicitly.
page.context.route_web_socket("**/*", self._playwright_ws_guard)
# WebRTC (RTCPeerConnection ICE/STUN/TURN) opens raw sockets via
# Chromium's WebRTC stack, which no route handler intercepts —
# neutralize it before any page script runs. Install at the
# CONTEXT level (like the route guards above) so it also covers
# popups (window.open) — a page-level init script does not
# propagate to new pages, leaving a popup with live WebRTC.
page.context.add_init_script(_DISABLE_WEBRTC_JS)
page.goto(
url,
wait_until=self.wait_until,
timeout=self.timeout * 1000,
)
html = page.content()
finally:
try:
page.close()
except Exception:
logger.debug(
"playwright.page_close_failed url={url}",
url=redact_url_for_log(url),
)
if html:
logger.debug(
"playwright.fetch_succeeded url={url} content_length={content_length}",
url=redact_url_for_log(url),
content_length=len(html),
)
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=wait_time,
success=True,
retry_count=1,
search_result_count=1,
)
return html
logger.debug(
"playwright.fetch_empty url={url}",
url=redact_url_for_log(url),
)
return None
except ImportError:
logger.warning(
"playwright.unavailable url={url}",
url=redact_url_for_log(url),
)
return None
except PolicyDeniedError:
raise
except Exception as e:
# Type name only: Playwright error texts embed the navigated
# URL, and loguru renders ``ExcType: str(e)`` with a traceback
# even when diagnose=False.
logger.opt(exception=False).error(
"playwright.fetch_error url={url} error_type={error_type}",
url=redact_url_for_log(url),
error_type=type(e).__name__,
)
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=wait_time,
success=False,
retry_count=1,
error_type=type(e).__name__,
)
return None
def close(self):
"""Clean up Playwright browser and resources."""
if self._browser:
try:
self._browser.close()
except Exception:
logger.debug("playwright.browser_close_failed")
self._browser = None
if self._playwright:
try:
self._playwright.stop()
except Exception:
logger.debug("playwright.stop_failed")
self._playwright = None
super().close()
class AutoHTMLDownloader(HTMLDownloader):
"""HTML downloader that tries static fetch first, falls back to
Crawl4AI/Playwright when the page needs JavaScript rendering.
Detection heuristics:
- Extracted content is too short (<200 chars)
- Raw HTML contains SPA framework signals (React, Vue, Angular, Next.js)
"""
def __init__(
self,
timeout: int = 30,
language: str = "English",
min_content_length: int = 200,
# Disabled by default to match the production Docker image, which
# ships without Chromium — every JS-rendering fallback attempt
# would otherwise fail loudly (see issue #3826). Callers running
# outside Docker with Chromium installed opt in via the
# ``web.enable_javascript_rendering`` setting, or pass ``True``
# explicitly when constructing the downloader.
enable_js_rendering: bool = False,
allow_private_ips: bool = False,
block_link_local: bool = False,
**kwargs,
):
super().__init__(timeout=timeout, language=language, **kwargs)
self.min_content_length = min_content_length
self.enable_js_rendering = enable_js_rendering
# SSRF policy threaded to the lazily-built Playwright child so the
# JS-render browser path honours the active egress scope. Default
# strict; ContentFetcher relaxes to True under PRIVATE_ONLY.
self.allow_private_ips = allow_private_ips
# Keep the static-fetch SafeSession in lockstep with the browser
# path so both routes honour the same private-egress gate (mirrors
# PlaywrightHTMLDownloader.__init__).
if hasattr(self.session, "allow_private_ips"):
self.session.allow_private_ips = allow_private_ips
# Link-local carve-out inside the private-IP grant: cloud metadata
# lives in the link-local range beyond the always-blocked literals
# and no legitimate page does. Off by default; full_search sets it
# alongside allow_private_ips. The SafeSession forwards it to every
# hop of the static fetch and the Playwright child to both of its
# route guards.
self.block_link_local = block_link_local
if hasattr(self.session, "block_link_local"):
self.session.block_link_local = block_link_local
self._playwright_downloader = None
# Host-level attempt budget for the fallback chain. The static leg
# backs off adaptively and bounds its retries, but its 429/503
# outcomes never reach the later legs: the probe GET applies no
# rate limiting, and the browser leg backs off only on its own
# tracker buckets (playwright_download_/crawl4ai_download_), which
# never see the static leg's html_download_ outcomes. When the
# static leg gives up with HTTP 429/503, the host's authority is
# marked here for the rest of this downloader's life
# (fetch_and_extract creates one per URL; batch_fetch_and_extract
# and ContentFetcher share one across several URLs), and the probe
# and browser legs are skipped for that host from then on. The
# static leg still runs for each later URL, and the mark never
# rewrites that URL's result. One user action must not keep
# sending requests to a host that is asking us to stop.
self._rate_limited_hosts: set[str] = set()
# Whether the static fetch of the URL that download_with_result()
# is working on gave up on 429/503 itself, as opposed to an earlier
# URL having marked its host. It is per call (download_with_result()
# clears it on entry), and is what lets that method report a
# rate-limited skip reason only for the URL that earned it.
self._static_fetch_rate_limited = False
def _host_rate_limited(self, url: str) -> bool:
"""Whether a static fetch already gave up on 429/503 for this host."""
return rate_limit_authority(url) in self._rate_limited_hosts
def _note_host_rate_limited(self, url: str) -> None:
"""Record that the static leg exhausted its retries on 429/503.
Marks the host for the rest of this downloader's life and flags the
fetch in progress (``_static_fetch_rate_limited``).
"""
self._rate_limited_hosts.add(rate_limit_authority(url))
self._static_fetch_rate_limited = True
def _get_playwright_downloader(self) -> PlaywrightHTMLDownloader:
"""Lazy-init JS rendering downloader for fallback."""
if self._playwright_downloader is None:
self._playwright_downloader = PlaywrightHTMLDownloader(
timeout=self.timeout,
language=self.language,
allow_private_ips=self.allow_private_ips,
block_link_local=self.block_link_local,
)
# The child creates its own SafeSession for robots.txt and other
# auxiliary requests. Keep it bound to the parent run's policy.
self._playwright_downloader.session.egress_context = getattr(
self.session, "egress_context", None
)
return self._playwright_downloader
@staticmethod
def _has_spa_signals(html: str) -> bool:
"""Check if HTML contains signals of a JS-rendered SPA."""
html_lower = html[:5000].lower() # Only check head/early body
return any(signal.lower() in html_lower for signal in SPA_SIGNALS)
def _fetch_html(self, url: str) -> Optional[str]:
"""Fetch HTML statically, storing raw response for SPA detection.
Note: _last_raw_html is instance state read by download()/download_with_result().
This is safe because AutoHTMLDownloader instances are created per-request
in fetch_and_extract/batch_fetch_and_extract — not shared across threads.
"""
self._last_raw_html = None
# Try the normal static fetch. ``on_rate_limited`` fires when the
# static leg exhausts its retries on 429/503, so the host is
# marked before any later leg runs.
html = super()._fetch_html(
url, on_rate_limited=lambda: self._note_host_rate_limited(url)
)
if html:
self._last_raw_html = html
return html
# Static fetch hit recoverable non-HTML content (PDF/text stash for
# download() recovery): a second raw GET would just re-download the
# same bytes, so skip the SPA probe entirely.
if (
getattr(self, "_recovery_payload", None) is not None
and getattr(self, "_recovery_request_url", None) == url
):
return None
# Host-level budget: a static fetch to this host gave up on
# 429/503 — do not spend the probe request on a host that asked us
# to stop.
if self._host_rate_limited(url):
logger.debug(
"auto_html.host_rate_limited_probe_skipped url={url}",
url=redact_url_for_log(url),
)
return None
# Static fetch failed (403, etc.) — try raw GET to check for
# challenge pages / SPA signals even on non-200 responses
try:
response = self.session.get(
url,
timeout=self.timeout,
allow_redirects=True,
)
self._last_raw_html = response.text
except PolicyDeniedError:
raise
except Exception:
# Expected/routine (403s, challenge pages, network blips) —
# keep at DEBUG, not ERROR, to avoid flooding alerting. Still
# drop the exception attachment: a network error here can embed
# the raw/credentialed URL or a cookie value.
logger.opt(exception=False).debug(
"auto_html.raw_fetch_failed url={url}",
url=redact_url_for_log(url),
)
return None
def download(self, url, content_type=None):
"""Try static fetch, fall back to JS rendering if needed."""
from .base import ContentType
if content_type is None:
content_type = ContentType.TEXT
# First: try static fetch (fast)
logger.debug(
"auto_html.static_fetch_started url={url}",
url=redact_url_for_log(url),
)
result = super().download(url, content_type)
if result and len(result) >= self.min_content_length:
logger.debug(
"auto_html.static_fetch_succeeded url={url} content_length={content_length}",
url=redact_url_for_log(url),
content_length=len(result),
)
return result
# Host-level budget: a static fetch to this host gave up on
# 429/503, and the browser leg's own rate limiting never sees those
# outcomes — do not send it to a host that asked us to stop.
if self._host_rate_limited(url):
logger.debug(
"auto_html.host_rate_limited_js_fallback_skipped url={url}",
url=redact_url_for_log(url),
)
return result
# Check if we should retry with JS rendering
raw_html = getattr(self, "_last_raw_html", None)
needs_js = raw_html and self._has_spa_signals(raw_html)
no_content = result is None or len(result) < self.min_content_length
if needs_js or no_content:
if not _browser_transport_allowed(self.session):
logger.debug("auto_html.js_fallback_scope_refused")
return result
if not self.enable_js_rendering:
logger.debug(
"auto_html.js_fallback_disabled url={url}",
url=redact_url_for_log(url),
)
return result
logger.info(
"auto_html.js_fallback_started url={url}",
url=redact_url_for_log(url),
)
pw_dl = self._get_playwright_downloader()
pw_result = pw_dl.download(url, content_type)
if pw_result and len(pw_result) > len(result or b""):
logger.info(
"auto_html.js_succeeded url={url} content_length={content_length}",
url=redact_url_for_log(url),
content_length=len(pw_result),
)
return pw_result
logger.debug(
"auto_html.js_not_improved url={url}",
url=redact_url_for_log(url),
)
return result
def download_with_result(self, url, content_type=None):
"""Try static fetch, fall back to JS rendering if needed."""
from .base import ContentType, DownloadResult
if content_type is None:
content_type = ContentType.TEXT
# The host mark outlives this call, this flag does not: clear it
# before anything can set it, so it can only describe this URL.
self._static_fetch_rate_limited = False
# First: try static fetch (fast)
logger.debug(
"auto_html.static_fetch_started url={url}",
url=redact_url_for_log(url),
)
result = super().download_with_result(url, content_type)
if (
result.is_success
and result.content
and len(result.content) >= self.min_content_length
):
logger.debug(
"auto_html.static_fetch_succeeded url={url} content_length={content_length}",
url=redact_url_for_log(url),
content_length=len(result.content),
)
return result
# Host-level budget: a static fetch to this host gave up on
# 429/503, and the browser leg's own rate limiting never sees those
# outcomes — do not send it to a host that asked us to stop. Only
# the fallback legs are skipped, as in download().
if self._host_rate_limited(url):
logger.debug(
"auto_html.host_rate_limited_js_fallback_skipped url={url}",
url=redact_url_for_log(url),
)
if self._static_fetch_rate_limited:
# This URL's own static fetch gave up on 429/503: say so in
# place of the generic fetch failure.
return DownloadResult(
skip_reason=(
"Host rate-limited the static fetch (HTTP 429/503 "
"after retries); skipping fallback legs"
)
)
# An earlier URL marked the host. That says nothing about this
# URL, so it keeps its own static result, success or failure.
return result
# Check if we should retry with JS rendering
raw_html = getattr(self, "_last_raw_html", None)
needs_js = raw_html and self._has_spa_signals(raw_html)
no_content = (
not result.is_success
or not result.content
or len(result.content) < self.min_content_length
)
if needs_js or no_content:
if not _browser_transport_allowed(self.session):
logger.debug("auto_html.js_fallback_scope_refused")
return result
if not self.enable_js_rendering:
logger.debug(
"auto_html.js_fallback_disabled url={url}",
url=redact_url_for_log(url),
)
return result
logger.info(
"auto_html.js_fallback_started url={url}",
url=redact_url_for_log(url),
)
pw_dl = self._get_playwright_downloader()
pw_result = pw_dl.download_with_result(url, content_type)
if (
pw_result.is_success
and pw_result.content
and len(pw_result.content) > len(result.content or b"")
):
logger.info(
"auto_html.js_succeeded url={url} content_length={content_length}",
url=redact_url_for_log(url),
content_length=len(pw_result.content),
)
return pw_result
logger.debug(
"auto_html.js_not_improved url={url}",
url=redact_url_for_log(url),
)
return result
def close(self):
"""Clean up both static and JS rendering resources."""
if self._playwright_downloader:
self._playwright_downloader.close()
self._playwright_downloader = None
super().close()