Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Use ProxyFix instead of inspecting X-Forwarded-For header #542

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions flask_security/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ def login_user(user, remember=None):
return False

if _security.trackable:
if 'X-Forwarded-For' in request.headers:
remote_addr = request.headers.getlist(
"X-Forwarded-For")[0].rpartition(' ')[-1]
else:
remote_addr = request.remote_addr or 'untrackable'
remote_addr = request.remote_addr or 'untrackable'

old_current_login, new_current_login = user.current_login_at, datetime.utcnow()
old_current_ip, new_current_ip = user.current_login_ip, remote_addr
Expand Down
6 changes: 5 additions & 1 deletion tests/test_trackable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import pytest

from utils import authenticate, logout
from werkzeug.contrib.fixers import ProxyFix

pytestmark = pytest.mark.trackable()


def test_trackable_flag(app, client):
app.wsgi_app = ProxyFix(app.wsgi_app, num_proxies=1)
e = 'matt@lp.com'
authenticate(client, email=e)
logout(client)
Expand All @@ -29,11 +31,13 @@ def test_trackable_flag(app, client):


def test_trackable_with_multiple_ips_in_headers(app, client):
app.wsgi_app = ProxyFix(app.wsgi_app, num_proxies=2)

e = 'matt@lp.com'
authenticate(client, email=e)
logout(client)
authenticate(client, email=e, headers={
'X-Forwarded-For': '99.99.99.99, 88.88.88.88'})
'X-Forwarded-For': '99.99.99.99, 88.88.88.88, 77.77.77.77'})

with app.app_context():
user = app.security.datastore.find_user(email=e)
Expand Down