mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-21 00:11:39 +02:00
Fixes IPv6 detection from headers (#14456)
* fixes client ip detection for v6 * adds test for get_client_ip * Employ urlparse() to strip port numbers from IPs --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
committed by
GitHub
parent
fe3f21105c
commit
92bdaa2120
@@ -1,4 +1,5 @@
|
||||
from netaddr import IPAddress
|
||||
from netaddr import AddrFormatError, IPAddress
|
||||
from urllib.parse import urlparse
|
||||
|
||||
__all__ = (
|
||||
'get_client_ip',
|
||||
@@ -17,11 +18,18 @@ def get_client_ip(request, additional_headers=()):
|
||||
)
|
||||
for header in HTTP_HEADERS:
|
||||
if header in request.META:
|
||||
client_ip = request.META[header].split(',')[0].partition(':')[0]
|
||||
ip = request.META[header].split(',')[0].strip()
|
||||
try:
|
||||
return IPAddress(client_ip)
|
||||
except ValueError:
|
||||
raise ValueError(f"Invalid IP address set for {header}: {client_ip}")
|
||||
return IPAddress(ip)
|
||||
except AddrFormatError:
|
||||
# Parse the string with urlparse() to remove port number or any other cruft
|
||||
ip = urlparse(f'//{ip}').hostname
|
||||
|
||||
try:
|
||||
return IPAddress(ip)
|
||||
except AddrFormatError:
|
||||
# We did our best
|
||||
raise ValueError(f"Invalid IP address set for {header}: {ip}")
|
||||
|
||||
# Could not determine the client IP address from request headers
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user