From 988e86c45b01d25e29409d0f404df11429a78921 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 7 Dec 2023 05:24:40 +0530 Subject: [PATCH] fixes client ip detection for v6 --- netbox/utilities/request.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/netbox/utilities/request.py b/netbox/utilities/request.py index 0f8ee9cae..63ba788f8 100644 --- a/netbox/utilities/request.py +++ b/netbox/utilities/request.py @@ -1,4 +1,4 @@ -from netaddr import IPAddress +from netaddr import AddrFormatError, IPAddress __all__ = ( 'get_client_ip', @@ -17,10 +17,15 @@ 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() + # Check if the IP address is v6 or v4 + if ip.count(':') > 1: + client_ip = ip + else: + client_ip = ip.partition(':')[0] try: return IPAddress(client_ip) - except ValueError: + except (AddrFormatError, ValueError): raise ValueError(f"Invalid IP address set for {header}: {client_ip}") # Could not determine the client IP address from request headers