Skip to content

Commit

Permalink
handle ip chains from reverse proxies in get_ip()
Browse files Browse the repository at this point in the history
  • Loading branch information
tykling committed Jun 23, 2024
1 parent e36d450 commit beaa40d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/hitcounter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def get_ip(request: HttpRequest) -> str:
"""
# this will return None if REMOTE_ADDR is missing from the request
ip_address = request.headers.get("X-Forwarded-For", request.META.get("REMOTE_ADDR", ""))
if "," in ip_address:
ip_address = ip_address.split(",")[0]
# this will raise an exception if the IP is not valid
validate_ip(ip_address)
validate_ip(ip_address.strip())
# all good
return ip_address

0 comments on commit beaa40d

Please sign in to comment.