🕵️♂️ (2-in-1) Email & Username OSINT suite featuring native MCP support for deep data extraction just from a single Email/Username. Analyzes 2720+ actively maintained scan vectors (210+ email / 2510+ username) for security research, investigations, and digital footprinting.
Python cybersecuritycybersecurity-toolsemail-osintethical-hackingethical-hacking-toolsidentificationinfosecosintosint-emailosint-toolosint-toolsosint-usernamepentestingpythonredteam-toolsredteamingthreat-intelligence
fix(pdf): validate media URLs against SSRF and safely follow redirects (#695)
Kaif327c19c6b6e2
diff --git a/tests/test_pdf_generator.py b/tests/test_pdf_generator.pyindex 97b2573..c8ee788 100644--- a/tests/test_pdf_generator.py+++ b/tests/test_pdf_generator.py@@ -109,6 +109,7 @@ def test_fetch_and_resize_image_routes_through_proxy(monkeypatch):def mock_get(url, **kwargs):recorded_kwargs.update(kwargs)resp = MagicMock()+ resp.is_redirect = Falseresp.status_code = 404return resp@@ -118,7 +119,7 @@ def test_fetch_and_resize_image_routes_through_proxy(monkeypatch):set_proxy_manager(proxies=["http://10.0.0.1:8080"])set_global_timeout(7.5)- res = fetch_and_resize_image("https://example.com/avatar.png")+ res = fetch_and_resize_image("https://8.8.8.8/avatar.png")assert res is Noneassert recorded_kwargs.get("proxy") == "http://10.0.0.1:8080"assert recorded_kwargs.get("timeout") == 7.5@@ -140,6 +141,7 @@ def test_fetch_and_resize_image_no_proxy(monkeypatch):def mock_get(url, **kwargs):recorded_kwargs.update(kwargs)resp = MagicMock()+ resp.is_redirect = Falseresp.status_code = 404return resp@@ -148,7 +150,7 @@ def test_fetch_and_resize_image_no_proxy(monkeypatch):set_proxy_manager(proxies=None)set_global_timeout(None)- res = fetch_and_resize_image("https://example.com/avatar.png")+ res = fetch_and_resize_image("https://8.8.8.8/avatar.png")assert res is Noneassert recorded_kwargs.get("proxy") is Noneassert recorded_kwargs.get("timeout") == 5.0@@ -169,16 +171,150 @@ def test_fetch_and_resize_image_proxy_failure_returns_none(monkeypatch):try:set_proxy_manager(proxies=["http://10.0.0.1:8080"])- res = fetch_and_resize_image("https://example.com/avatar.png")+ res = fetch_and_resize_image("https://8.8.8.8/avatar.png")assert res is Nonefinally:set_proxy_manager(proxies=None)+ "unsafe_url",+ [+ "http://127.0.0.1/avatar.png",+ "http://localhost/avatar.png",+ "http://[::1]/avatar.png",+ "http://10.0.0.1/avatar.png",+ "http://192.168.1.1/avatar.png",+ "http://172.16.0.1/avatar.png",+ "http://169.254.169.254/latest/meta-data/",+ "http://0.0.0.0/avatar.png",+ "file:///etc/passwd",+ "ftp://example.com/avatar.png",+ "gopher://127.0.0.1:70/",+ "javascript:alert(1)",+ ],+)+def test_is_safe_media_url_rejects_unsafe_destinations(unsafe_url):+ from user_scanner.core.pdf_generator import is_safe_media_url++ assert is_safe_media_url(unsafe_url) is False+++ "safe_url",+ [+ "http://8.8.8.8/avatar.png",+ "http://1.1.1.1/avatar.png",+ "https://8.8.8.8/avatar.png",+ ],+)+def test_is_safe_media_url_accepts_public_ips(safe_url):+ from user_scanner.core.pdf_generator import is_safe_media_url++ assert is_safe_media_url(safe_url) is True+++def test_fetch_and_resize_image_rejects_loopback(monkeypatch):+ import user_scanner.core.pdf_generator as pdf_gen++ monkeypatch.setattr(pdf_gen, "PIL_AVAILABLE", True)+ res = pdf_gen.fetch_and_resize_image("http://127.0.0.1:8080/avatar.png")+ assert res is None+++def test_fetch_and_resize_image_rejects_redirect_to_private(monkeypatch):+ from unittest.mock import MagicMock+ import httpx+ import user_scanner.core.pdf_generator as pdf_gen++ monkeypatch.setattr(pdf_gen, "PIL_AVAILABLE", True)++ def mock_get(url, **kwargs):+ resp = MagicMock()+ resp.is_redirect = True+ resp.status_code = 302+ resp.headers = {"Location": "http://127.0.0.1:9000/internal-pfp"}+ return resp++ monkeypatch.setattr(httpx, "get", mock_get)++ res = pdf_gen.fetch_and_resize_image("http://8.8.8.8/avatar.png")+ assert res is None+++def test_fetch_and_resize_image_follows_safe_redirect(monkeypatch):+ from unittest.mock import MagicMock+ import httpx+ import user_scanner.core.pdf_generator as pdf_gen++ monkeypatch.setattr(pdf_gen, "PIL_AVAILABLE", True)+ urls_requested = []++ def mock_get(url, **kwargs):+ urls_requested.append(url)+ resp = MagicMock()+ if url == "http://8.8.8.8/initial":+ resp.is_redirect = True+ resp.status_code = 302+ resp.headers = {"Location": "http://1.1.1.1/final.png"}+ else:+ resp.is_redirect = False+ resp.status_code = 404+ return resp++ monkeypatch.setattr(httpx, "get", mock_get)++ res = pdf_gen.fetch_and_resize_image("http://8.8.8.8/initial")+ assert res is None+ assert urls_requested == ["http://8.8.8.8/initial", "http://1.1.1.1/final.png"]++def test_fetch_and_resize_image_without_pil_returns_none(monkeypatch):import user_scanner.core.pdf_generator as pdf_genmonkeypatch.setattr(pdf_gen, "PIL_AVAILABLE", False)- assert pdf_gen.fetch_and_resize_image("https://example.com/avatar.png") is None+ assert pdf_gen.fetch_and_resize_image("https://8.8.8.8/avatar.png") is None++[email protected](not REPORTLAB_AVAILABLE, reason="ReportLab not installed")+def test_pdf_fetches_loopback_media_and_redirect_blocked():+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+ from PIL import Image as PILImage+ import threading+ import io++ image = io.BytesIO()+ PILImage.new("RGB", (2, 2), "red").save(image, format="PNG")+ seen = []++ class Handler(BaseHTTPRequestHandler):+ def do_GET(self):+ seen.append(self.path)+ if self.path == "/redirect":+ self.send_response(302)+ self.send_header("Location", "/private-image")+ self.end_headers()+ else:+ self.send_response(200)+ self.send_header("Content-Type", "image/png")+ self.end_headers()+ self.wfile.write(image.getvalue())++ def log_message(self, *args):+ pass++ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ thread = threading.Thread(target=server.serve_forever)+ thread.start()+ try:+ base = f"http://127.0.0.1:{server.server_port}"+ result = Result.taken(site_name="Example", username="audit", media={"avatar": base + "/redirect"})+ data = generate_pdf_report("audit", "Username", [result])+ assert data.startswith(b"%PDF-")+ assert seen == [] # Loopback server must NEVER be contacted!+ finally:+ server.shutdown()+ server.server_close()+ thread.join()diff --git a/user_scanner/core/pdf_generator.py b/user_scanner/core/pdf_generator.pyindex c066ea1..a99c3b0 100644--- a/user_scanner/core/pdf_generator.py+++ b/user_scanner/core/pdf_generator.py@@ -1,9 +1,12 @@import concurrent.futures+import htmlimport io+import ipaddressimport json-import html+import socket+import urllib.parsefrom datetime import datetime-from typing import List, Any, Optional+from typing import List, Any, Optional, Unionimport httpxfrom user_scanner.core.helpers import get_global_timeout, get_proxy@@ -63,58 +66,139 @@ def clean_metadata(extra: Any) -> List[tuple]:return [(k, v) for k, v in extra.items() if not any(x in k.lower() for x in IMAGE_HEURISTIC_KEYS)]+def is_safe_ip(ip: Union[ipaddress.IPv4Address, ipaddress.IPv6Address]) -> bool:+ """Check if an IP address is a safe, publicly-routable destination."""+ if (+ ip.is_loopback+ or ip.is_private+ or ip.is_link_local+ or ip.is_reserved+ or ip.is_multicast+ or ip.is_unspecified+ ):+ return False+ if isinstance(ip, ipaddress.IPv6Address):+ if ip.ipv4_mapped:+ return is_safe_ip(ip.ipv4_mapped)+ if ip.sixtofour:+ return is_safe_ip(ip.sixtofour)+ return True+++def is_safe_media_url(url: str) -> bool:+ """Validate that a URL uses HTTP/HTTPS and resolves exclusively to safe public IPs."""+ if not url or not isinstance(url, str):+ return False+ try:+ parsed = urllib.parse.urlsplit(url)+ if parsed.scheme.lower() not in ("http", "https"):+ return False++ hostname = parsed.hostname+ if not hostname:+ return False++ hostname = hostname.strip("[]").lower()+ if hostname == "localhost":+ return False++ # Try parsing directly as an IP address+ try:+ ip = ipaddress.ip_address(hostname)+ return is_safe_ip(ip)+ except ValueError:+ pass++ # Resolve hostname to all associated IPs via DNS+ addrinfo = socket.getaddrinfo(hostname, None, proto=socket.IPPROTO_TCP)+ if not addrinfo:+ return False++ for entry in addrinfo:+ sockaddr = entry[4]+ ip_str = sockaddr[0]+ ip = ipaddress.ip_address(ip_str)+ if not is_safe_ip(ip):+ return False++ return True+ except Exception:+ return False++def fetch_and_resize_image(url: str, max_size: tuple = (600, 600), timeout: float = 5.0) -> Optional[Any]:if not PIL_AVAILABLE:return Noneglobal_timeout = get_global_timeout()effective_timeout = global_timeout if global_timeout is not None else timeoutproxy = get_proxy()+ max_redirects = 5+ current_url = url+try:- resp = httpx.get(- url,- headers={- "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"- },- proxy=proxy,- timeout=effective_timeout,- follow_redirects=True,- )- if resp.status_code == 200:- content_type = resp.headers.get("Content-Type", "")- if SVGLIB_AVAILABLE and ("svg" in content_type.lower() or url.lower().endswith(".svg")):- try:- drawing = svg2rlg(io.BytesIO(resp.content))- if drawing and hasattr(drawing, "width") and hasattr(drawing, "height") and drawing.width > 0 and drawing.height > 0:- s = min(max_size[0] / drawing.width, max_size[1] / drawing.height)- if s < 1.0:- drawing.scale(s, s)- drawing.width = drawing.width * s- drawing.height = drawing.height * s- return drawing- except Exception:- pass-- img_raw = PILImage.open(io.BytesIO(resp.content))- img = img_raw.convert("RGB")-- # Crop to 1:1 square- min_dim = min(img.width, img.height)- left = (img.width - min_dim) / 2- top = (img.height - min_dim) / 2- right = (img.width + min_dim) / 2- bottom = (img.height + min_dim) / 2- cropped = img.crop((left, top, right, bottom))-- # Scale down only if larger than max_size limit (600x600)- if cropped.width > max_size[0] or cropped.height > max_size[1]:- final_img = cropped.resize(max_size, PILImage.Resampling.LANCZOS)- else:- final_img = cropped-- img_byte_arr = io.BytesIO()- final_img.save(img_byte_arr, format="JPEG", quality=90)- img_byte_arr.seek(0)- return img_byte_arr+ headers = {+ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"+ }+ resp = None++ for _ in range(max_redirects + 1):+ if not is_safe_media_url(current_url):+ return None++ resp = httpx.get(+ current_url,+ headers=headers,+ proxy=proxy,+ timeout=effective_timeout,+ follow_redirects=False,+ )++ if resp.is_redirect:+ location = resp.headers.get("Location")+ if not location:+ return None+ current_url = urllib.parse.urljoin(current_url, location)+ continue+ break++ if resp is None or resp.status_code != 200:+ return None++ content_type = resp.headers.get("Content-Type", "")+ if SVGLIB_AVAILABLE and ("svg" in content_type.lower() or current_url.lower().endswith(".svg")):+ try:+ drawing = svg2rlg(io.BytesIO(resp.content), resolve_entities=False)+ if drawing and hasattr(drawing, "width") and hasattr(drawing, "height") and drawing.width > 0 and drawing.height > 0:+ s = min(max_size[0] / drawing.width, max_size[1] / drawing.height)+ if s < 1.0:+ drawing.scale(s, s)+ drawing.width = drawing.width * s+ drawing.height = drawing.height * s+ return drawing+ except Exception:+ pass++ img_raw = PILImage.open(io.BytesIO(resp.content))+ img = img_raw.convert("RGB")++ # Crop to 1:1 square+ min_dim = min(img.width, img.height)+ left = (img.width - min_dim) / 2+ top = (img.height - min_dim) / 2+ right = (img.width + min_dim) / 2+ bottom = (img.height + min_dim) / 2+ cropped = img.crop((left, top, right, bottom))++ # Scale down only if larger than max_size limit (600x600)+ if cropped.width > max_size[0] or cropped.height > max_size[1]:+ final_img = cropped.resize(max_size, PILImage.Resampling.LANCZOS)+ else:+ final_img = cropped++ img_byte_arr = io.BytesIO()+ final_img.save(img_byte_arr, format="JPEG", quality=90)+ img_byte_arr.seek(0)+ return img_byte_arrexcept Exception:passreturn None