File size: 12,075 Bytes
41ffb08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | """SSRF-hardened HTTP GET for Web_Fetch.
Defence in depth:
1. URL check before every request and every redirect hop: http/https only,
no credentials in the URL, allowed ports only, no internal host names,
and every address the host resolves to must be public.
2. Connection-time check: the urllib3 connection re-resolves the host, rejects
any non-public address and connects to the vetted IP literal. That closes
the DNS-rebinding gap between step 1 and the actual connect.
3. Redirects are followed by hand (max 5) so each hop passes step 1.
4. Environment proxies are ignored (trust_env=False), so the connect really
goes to the vetted address.
5. Body size (5 MB, measured after decompression) and total wall time (15 s)
are capped.
"""
from __future__ import annotations
import ipaddress
import socket
import time
from dataclasses import dataclass
from urllib.parse import urljoin, urlsplit
import requests
from requests.adapters import HTTPAdapter
from urllib3.connection import HTTPConnection, HTTPSConnection
from urllib3.connectionpool import HTTPConnectionPool, HTTPSConnectionPool
from urllib3.exceptions import NewConnectionError
from urllib3.util import connection as urllib3_connection
MAX_BYTES = 5 * 1024 * 1024
TOTAL_TIMEOUT_SECONDS = 15.0
CONNECT_TIMEOUT_SECONDS = 6.0
READ_TIMEOUT_SECONDS = 10.0
MAX_REDIRECTS = 5
ALLOWED_PORTS = {80, 443, 8080, 8443}
BLOCKED_HOST_SUFFIXES = (
".localhost",
".internal",
".local",
".localdomain",
".home.arpa",
".intranet",
".corp",
".lan",
)
BLOCKED_HOSTS = {"localhost", "localhost.localdomain", "ip6-localhost", "ip6-loopback", "metadata", "metadata.google.internal"}
_BLOCK_TAG = "[blocked-address]"
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/128.0.0.0 Safari/537.36"
),
"Accept-Language": "en-US,en;q=0.9",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,text/plain;q=0.8,*/*;q=0.5",
"Accept-Encoding": "gzip, deflate",
"DNT": "1",
"Upgrade-Insecure-Requests": "1",
}
class FetchBlocked(Exception):
"""The URL points somewhere this tool refuses to go."""
class FetchFailed(Exception):
"""The request failed for an ordinary network/HTTP reason."""
def is_public_ip(value: str) -> bool:
try:
ip = ipaddress.ip_address(value.split("%", 1)[0])
except ValueError:
return False
if isinstance(ip, ipaddress.IPv6Address):
embedded = ip.ipv4_mapped or (ip.sixtofour if ip.sixtofour else None)
if ip.teredo:
embedded = ip.teredo[1]
if embedded is not None and not is_public_ip(str(embedded)):
return False
if ip.is_multicast or ip.is_unspecified or ip.is_loopback or ip.is_link_local:
return False
if ip.is_private or ip.is_reserved:
return False
return bool(ip.is_global)
def _resolve(host: str, port: int) -> list[str]:
infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
return list(dict.fromkeys(info[4][0] for info in infos))
def check_url(url: str) -> str:
"""Return the normalised URL or raise FetchBlocked with a readable reason."""
try:
parts = urlsplit(url.strip())
except ValueError as exc:
raise FetchBlocked(f"Malformed URL ({exc}).") from exc
scheme = (parts.scheme or "").lower()
if scheme not in ("http", "https"):
raise FetchBlocked(f"Only http:// and https:// URLs are supported (got '{scheme or 'no scheme'}').")
if parts.username or parts.password:
raise FetchBlocked("URLs containing credentials (user:pass@host) are not allowed.")
host = (parts.hostname or "").rstrip(".").lower()
if not host:
raise FetchBlocked("URL has no host name.")
try:
port = parts.port or (443 if scheme == "https" else 80)
except ValueError as exc:
raise FetchBlocked("URL has an invalid port.") from exc
if port not in ALLOWED_PORTS:
raise FetchBlocked(f"Port {port} is not allowed (allowed: {', '.join(map(str, sorted(ALLOWED_PORTS)))}).")
if host in BLOCKED_HOSTS or host.endswith(BLOCKED_HOST_SUFFIXES):
raise FetchBlocked(f"Host '{host}' is a local or internal name and cannot be fetched.")
try:
addresses = _resolve(host, port)
except (socket.gaierror, UnicodeError) as exc:
raise FetchFailed(f"Could not resolve host '{host}'.") from exc
if not addresses:
raise FetchFailed(f"Could not resolve host '{host}'.")
for addr in addresses:
if not is_public_ip(addr):
raise FetchBlocked(f"Host '{host}' resolves to a private, loopback, link-local or reserved address ({addr}).")
return parts.geturl()
class _GuardedConnectionMixin:
"""Re-resolve at connect time, refuse non-public addresses, connect to the vetted IP."""
def _new_conn(self): # type: ignore[override]
host = self._dns_host # type: ignore[attr-defined]
port = self.port # type: ignore[attr-defined]
try:
addresses = _resolve(host, port)
except socket.gaierror as exc:
raise NewConnectionError(self, f"Name resolution failed for {host}: {exc}") from exc # type: ignore[arg-type]
bad = [a for a in addresses if not is_public_ip(a)]
if bad or not addresses:
raise NewConnectionError(self, f"{_BLOCK_TAG} {host} resolves to non-public address {bad[0] if bad else '?'}") # type: ignore[arg-type]
last_exc: Exception | None = None
for addr in addresses:
try:
return urllib3_connection.create_connection(
(addr, port),
self.timeout, # type: ignore[attr-defined]
source_address=self.source_address, # type: ignore[attr-defined]
socket_options=self.socket_options, # type: ignore[attr-defined]
)
except OSError as exc:
last_exc = exc
raise NewConnectionError(self, f"Failed to establish a new connection: {last_exc}") # type: ignore[arg-type]
class _GuardedHTTPConnection(_GuardedConnectionMixin, HTTPConnection):
pass
class _GuardedHTTPSConnection(_GuardedConnectionMixin, HTTPSConnection):
pass
class _GuardedHTTPPool(HTTPConnectionPool):
ConnectionCls = _GuardedHTTPConnection
class _GuardedHTTPSPool(HTTPSConnectionPool):
ConnectionCls = _GuardedHTTPSConnection
class _GuardedAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs): # type: ignore[override]
super().init_poolmanager(*args, **kwargs)
self.poolmanager.pool_classes_by_scheme = {"http": _GuardedHTTPPool, "https": _GuardedHTTPSPool}
def _new_session() -> requests.Session:
session = requests.Session()
session.trust_env = False # never route through env proxies or read .netrc
adapter = _GuardedAdapter(max_retries=0, pool_connections=4, pool_maxsize=8)
session.mount("http://", adapter)
session.mount("https://", adapter)
session.headers.update(HEADERS)
return session
@dataclass
class FetchResult:
url: str
status: int
content_type: str
declared_charset: str | None
body: bytes
size_capped: bool
def _status_error(status: int) -> str:
if status == 403:
return "Access forbidden (HTTP 403). The website may be blocking automated requests."
if status == 404:
return "Page not found (HTTP 404). Please check the URL."
if status == 429:
return "Rate limited by the website (HTTP 429). Try again in a few minutes."
if status >= 500:
return f"The website returned a server error (HTTP {status})."
return f"HTTP error {status}."
def _is_textual(content_type: str) -> bool:
ctype = content_type.split(";", 1)[0].strip().lower()
return ctype.startswith("text/") or ctype.endswith(("+xml", "+json", "/xml", "/json", "/xhtml")) or "html" in ctype
def safe_get(url: str) -> FetchResult:
"""GET a public http(s) URL with manual, re-validated redirects and hard caps."""
started = time.monotonic()
current = check_url(url)
session = _new_session()
try:
for _hop in range(MAX_REDIRECTS + 1):
remaining = TOTAL_TIMEOUT_SECONDS - (time.monotonic() - started)
if remaining <= 0:
raise FetchFailed(f"Request timed out (over {TOTAL_TIMEOUT_SECONDS:.0f} s).")
try:
resp = session.get(
current,
allow_redirects=False,
stream=True,
timeout=(min(CONNECT_TIMEOUT_SECONDS, remaining), min(READ_TIMEOUT_SECONDS, remaining)),
)
except requests.exceptions.ConnectionError as exc:
if _BLOCK_TAG in str(exc):
raise FetchBlocked("The host resolved to a private, loopback, link-local or reserved address.") from exc
if isinstance(exc, requests.exceptions.ConnectTimeout):
raise FetchFailed("Connection timed out. The website took too long to respond.") from exc
raise FetchFailed("Connection error. The site may be down or unreachable.") from exc
except requests.exceptions.Timeout as exc:
raise FetchFailed("Request timed out. The website took too long to respond.") from exc
except requests.exceptions.RequestException as exc:
raise FetchFailed(f"Request failed: {str(exc)[:200]}") from exc
if resp.is_redirect or resp.status_code in (301, 302, 303, 307, 308):
location = resp.headers.get("Location")
resp.close()
if not location:
raise FetchFailed(f"Redirect (HTTP {resp.status_code}) without a Location header.")
current = check_url(urljoin(current, location))
continue
if resp.status_code >= 400:
status = resp.status_code
resp.close()
raise FetchFailed(_status_error(status))
content_type = resp.headers.get("Content-Type", "")
if content_type and not _is_textual(content_type):
# Do not download bodies (PDFs, images, archives) the tool cannot use.
resp.close()
return FetchResult(current, resp.status_code, content_type, None, b"", False)
declared = requests.utils.get_encoding_from_headers({"content-type": content_type}) if "charset" in content_type.lower() else None
chunks: list[bytes] = []
total = 0
capped = False
try:
for chunk in resp.iter_content(chunk_size=65536):
if not chunk:
continue
chunks.append(chunk)
total += len(chunk)
if total >= MAX_BYTES:
capped = True
break
if time.monotonic() - started > TOTAL_TIMEOUT_SECONDS:
raise FetchFailed(f"Download exceeded {TOTAL_TIMEOUT_SECONDS:.0f} s and was stopped.")
except requests.exceptions.RequestException as exc:
raise FetchFailed(f"Download failed: {str(exc)[:200]}") from exc
finally:
resp.close()
body = b"".join(chunks)[:MAX_BYTES]
return FetchResult(current, resp.status_code, content_type, declared, body, capped)
raise FetchFailed(f"Too many redirects (more than {MAX_REDIRECTS}).")
finally:
session.close()
__all__ = ["FetchBlocked", "FetchFailed", "FetchResult", "safe_get", "check_url", "is_public_ip", "MAX_BYTES"]
|