Skip to content

Commit c6a251c

Browse files
committed
Use HTTP_X_FORWARDED_FOR to handle proxied ips
Signed-off-by: Kipchirchir Sigei <[email protected]>
1 parent 541a978 commit c6a251c

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

onadata/apps/api/tests/viewsets/test_connect_viewset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def test_login_attempts(self, send_account_lockout_email, mock_logger, rpt_mock)
478478
self.assertEqual(cache.get(safe_key(f"login_attempts-{request_ip}-bob")), 2)
479479

480480
request = self._get_request_session_with_auth(
481-
view, auth, extra={"HTTP_X_REAL_IP": "5.6.7.8"}
481+
view, auth, extra={"HTTP_X_FORWARDED_FOR": "5.6.7.8"}
482482
)
483483
# login attempts are tracked separately for other IPs
484484
response = view(request)

onadata/libs/authentication.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,10 @@ def retrieve_user_identification(request) -> Tuple[Optional[str], Optional[str]]
259259
Retrieve user information from a HTTP request.
260260
"""
261261
ip_address = None
262+
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
262263

263-
if request.headers.get("X-Real-Ip"):
264-
ip_address = request.headers["X-Real-Ip"].split(",")[0]
264+
if x_forwarded_for:
265+
ip_address = x_forwarded_for.split(",")[0]
265266
else:
266267
ip_address = request.META.get("REMOTE_ADDR")
267268

0 commit comments

Comments
 (0)