ai-api-testing / backend /core /url_guard.py
Amitkasar's picture
Claude Sonnet 4.6
Security & reliability: Phase 1–3 fixes (Items 1–15)
c6f3411
Raw History Blame Contribute Delete
2.03 kB
import ipaddress
import socket
from urllib.parse import urlparse
from fastapi import HTTPException
_BLOCKED_NETS = [
ipaddress.ip_network("127.0.0.0/8"),
ipaddress.ip_network("10.0.0.0/8"),
ipaddress.ip_network("172.16.0.0/12"),
ipaddress.ip_network("192.168.0.0/16"),
ipaddress.ip_network("0.0.0.0/8"),
ipaddress.ip_network("169.254.0.0/16"), # AWS/GCP metadata
ipaddress.ip_network("::1/128"),
ipaddress.ip_network("fc00::/7"),
]
_ALLOWED_SCHEMES = {"http", "https"}
def resolve_safe_ip(url: str) -> str:
"""
Resolve the hostname to an IP, verify it's not private, and return the IP string.
Raises HTTP 400 if the URL is malformed, uses a disallowed scheme, or resolves to a
private/internal address. The caller should use this IP directly when making the
request to prevent DNS rebinding (the hostname is resolved only once here).
"""
try:
parsed = urlparse(url)
except Exception:
raise HTTPException(status_code=400, detail=f"Malformed URL: {url}")
if parsed.scheme not in _ALLOWED_SCHEMES:
raise HTTPException(
status_code=400,
detail=f"URL scheme '{parsed.scheme}' is not allowed. Only http/https are permitted.",
)
hostname = parsed.hostname
if not hostname:
raise HTTPException(status_code=400, detail="URL has no hostname.")
try:
ip_str = socket.getaddrinfo(hostname, None)[0][4][0]
ip = ipaddress.ip_address(ip_str)
except Exception:
raise HTTPException(status_code=400, detail=f"Could not resolve hostname: {hostname}")
for net in _BLOCKED_NETS:
if ip in net:
raise HTTPException(
status_code=400,
detail=f"Requests to private/internal addresses are not allowed (resolved to {ip}).",
)
return ip_str
def assert_safe_url(url: str) -> None:
"""Raise HTTP 400 if the URL targets a private/internal address or disallowed scheme."""
resolve_safe_ip(url)