fix: allow trusted QQ CDN benchmark IP resolution

This commit is contained in:
yeyitech 2026-04-17 12:11:52 +08:00 committed by Teknium
parent aca81ac7bb
commit a97b08e30c
2 changed files with 52 additions and 3 deletions

View file

@ -29,6 +29,13 @@ _BLOCKED_HOSTNAMES = frozenset({
"metadata.goog",
})
# Exact HTTPS hostnames allowed to resolve to private/benchmark-space IPs.
# This is intentionally narrow: QQ media downloads can legitimately resolve
# to 198.18.0.0/15 behind local proxy/benchmark infrastructure.
_TRUSTED_PRIVATE_IP_HOSTS = frozenset({
"multimedia.nt.qq.com.cn",
})
# 100.64.0.0/10 (CGNAT / Shared Address Space, RFC 6598) is NOT covered by
# ipaddress.is_private — it returns False for both is_private and is_global.
# Must be blocked explicitly. Used by carrier-grade NAT, Tailscale/WireGuard
@ -48,6 +55,11 @@ def _is_blocked_ip(ip: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool:
return False
def _allows_private_ip_resolution(hostname: str, scheme: str) -> bool:
"""Return True when a trusted HTTPS hostname may bypass IP-class blocking."""
return scheme == "https" and hostname in _TRUSTED_PRIVATE_IP_HOSTS
def is_safe_url(url: str) -> bool:
"""Return True if the URL target is not a private/internal address.
@ -56,7 +68,8 @@ def is_safe_url(url: str) -> bool:
"""
try:
parsed = urlparse(url)
hostname = (parsed.hostname or "").strip().lower()
hostname = (parsed.hostname or "").strip().lower().rstrip(".")
scheme = (parsed.scheme or "").strip().lower()
if not hostname:
return False
@ -65,6 +78,8 @@ def is_safe_url(url: str) -> bool:
logger.warning("Blocked request to internal hostname: %s", hostname)
return False
allow_private_ip = _allows_private_ip_resolution(hostname, scheme)
# Try to resolve and check IP
try:
addr_info = socket.getaddrinfo(hostname, None, socket.AF_UNSPEC, socket.SOCK_STREAM)
@ -81,13 +96,19 @@ def is_safe_url(url: str) -> bool:
except ValueError:
continue
if _is_blocked_ip(ip):
if not allow_private_ip and _is_blocked_ip(ip):
logger.warning(
"Blocked request to private/internal address: %s -> %s",
hostname, ip_str,
)
return False
if allow_private_ip:
logger.debug(
"Allowing trusted hostname despite private/internal resolution: %s",
hostname,
)
return True
except Exception as exc: