Skip to content

Commit 4e6a5a4

Browse files
committed
Correctly determine remote IP if behind reverse proxy
1 parent 30cbc24 commit 4e6a5a4

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/db_auth.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,14 @@ def __user_is_authorized(self, user, password):
723723
:param User user: User instance
724724
:param str password: Password
725725
"""
726+
remote_addr = request.headers.get("X-Forwarded-For", request.remote_addr).split(",")[0].strip()
727+
self.logger.info("Remote IP is %s" % remote_addr)
726728
# Check if IP blacklisted
727729
if self.ip_blacklist_duration > 0:
728-
entry = ip_blacklist.lookup(request.remote_addr)
730+
entry = ip_blacklist.lookup(remote_addr)
729731
count = entry['value'] if entry else 0
730732
if count >= self.ip_blacklist_max_attempt_count:
731-
self.logger.info("IP %s is blacklisted with %s attempts" % (request.remote_addr, count))
733+
self.logger.info("IP %s is blacklisted with %s attempts" % (remote_addr, count))
732734
return False, i18n.t('auth.ip_blacklisted')
733735

734736
if user is None or user.password_hash is None:
@@ -755,10 +757,10 @@ def __user_is_authorized(self, user, password):
755757

756758
# add to ip blacklist
757759
if self.ip_blacklist_duration > 0:
758-
entry = ip_blacklist.lookup(request.remote_addr)
760+
entry = ip_blacklist.lookup(remote_addr)
759761
count = entry['value'] if entry else 0
760-
ip_blacklist.set(request.remote_addr, count + 1, self.ip_blacklist_duration)
761-
self.logger.info("Attempt count for IP %s: %s" % (request.remote_addr, count + 1))
762+
ip_blacklist.set(remote_addr, count + 1, self.ip_blacklist_duration)
763+
self.logger.info("Attempt count for IP %s: %s" % (remote_addr, count + 1))
762764

763765
# increase failed login attempts counter
764766
user.failed_sign_in_count += 1

0 commit comments

Comments
 (0)