Skip to content

Commit 88f9f99

Browse files
author
Inbal Tako
committed
Normalize ip extraction
1 parent f19a344 commit 88f9f99

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

securenative/utils/request_utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class RequestUtils(object):
55
SECURENATIVE_COOKIE = "_sn"
66
SECURENATIVE_HEADER = "x-securenative"
7-
IP_HEADERS = ["x-forwarded-for", "x-client-ip", "x-real-ip", "x-forwarded", "x-cluster-client-ip", "forwarded-for", "forwarded", "via"]
7+
IP_HEADERS = ["HTTP_X_FORWARDED_FOR", "X_FORWARDED_FOR", "REMOTE_ADDR", "x-forwarded-for", "x-client-ip", "x-real-ip", "x-forwarded", "x-cluster-client-ip", "forwarded-for", "forwarded", "via"]
88

99
@staticmethod
1010
def get_secure_header_from_request(headers):
@@ -34,17 +34,19 @@ def get_client_ip_from_request(request, options):
3434

3535
for header in RequestUtils.IP_HEADERS:
3636
try:
37-
ips = request.headers[header].split(',')
37+
ips = request.headers.get(header).split(',')
3838
return RequestUtils.get_valid_ip(ips)
3939
except Exception:
4040
continue
4141

42-
try:
43-
for header in RequestUtils.IP_HEADERS:
44-
ips = request.META.get[header].split(',')
42+
for header in RequestUtils.IP_HEADERS:
43+
try:
44+
ips = request.META.get(header).split(',')
4545
return RequestUtils.get_valid_ip(ips)
46-
except Exception:
47-
return ""
46+
except Exception:
47+
continue
48+
49+
return ""
4850

4951
@staticmethod
5052
def get_remote_ip_from_request(request):

0 commit comments

Comments
 (0)