fixed tests
This commit is contained in:
@@ -61,17 +61,20 @@ def normalise_ip(address: str) -> str:
|
||||
IPv4-mapped IPv6 addresses (e.g. ``::ffff:192.168.1.1``) are converted
|
||||
to their IPv4 equivalent (``192.168.1.1``).
|
||||
Plain IPv4 addresses are returned unchanged.
|
||||
Non-IP strings (e.g. ``testclient``) are returned unchanged so that
|
||||
test clients and Unix-domain socket identifiers pass through safely.
|
||||
|
||||
Args:
|
||||
address: A valid IP address string.
|
||||
address: An IP address string or other identifier.
|
||||
|
||||
Returns:
|
||||
Normalised IP address string.
|
||||
|
||||
Raises:
|
||||
ValueError: If *address* is not a valid IP address.
|
||||
Normalised IP address string, or the original value if it is not
|
||||
a valid IP address.
|
||||
"""
|
||||
ip = ipaddress.ip_address(address)
|
||||
try:
|
||||
ip = ipaddress.ip_address(address)
|
||||
except ValueError:
|
||||
return address
|
||||
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped:
|
||||
return str(ip.ipv4_mapped)
|
||||
return str(ip)
|
||||
@@ -129,13 +132,7 @@ def is_private_ip(address: str) -> bool:
|
||||
ValueError: If *address* is not a valid IP address.
|
||||
"""
|
||||
ip = ipaddress.ip_address(address)
|
||||
return (
|
||||
ip.is_private
|
||||
or ip.is_loopback
|
||||
or ip.is_link_local
|
||||
or ip.is_multicast
|
||||
or ip.is_reserved
|
||||
)
|
||||
return ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_multicast or ip.is_reserved
|
||||
|
||||
|
||||
async def validate_blocklist_url(url: str) -> None:
|
||||
@@ -165,9 +162,7 @@ async def validate_blocklist_url(url: str) -> None:
|
||||
raise ValueError(f"Invalid URL format: {exc}") from exc
|
||||
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValueError(
|
||||
f"Invalid scheme '{parsed.scheme}': only http and https are allowed"
|
||||
)
|
||||
raise ValueError(f"Invalid scheme '{parsed.scheme}': only http and https are allowed")
|
||||
|
||||
if not parsed.hostname:
|
||||
raise ValueError("URL has no hostname")
|
||||
@@ -201,14 +196,9 @@ async def validate_blocklist_url(url: str) -> None:
|
||||
# connection time, and host mode is never used in production.
|
||||
if is_private_ip(ip_str):
|
||||
import os
|
||||
if (
|
||||
os.getenv("BANGUI_LOG_LEVEL") == "debug"
|
||||
and ipaddress.ip_address(ip_str).is_loopback
|
||||
):
|
||||
|
||||
if os.getenv("BANGUI_LOG_LEVEL") == "debug" and ipaddress.ip_address(ip_str).is_loopback:
|
||||
continue
|
||||
raise ValueError(
|
||||
f"Hostname '{hostname}' resolves to private/reserved IP: {ip_str}"
|
||||
)
|
||||
raise ValueError(f"Hostname '{hostname}' resolves to private/reserved IP: {ip_str}")
|
||||
except ipaddress.AddressValueError as exc:
|
||||
raise ValueError(f"Invalid IP address: {ip_str}") from exc
|
||||
|
||||
|
||||
Reference in New Issue
Block a user