Skip to content

Commit 9e2977f

Browse files
committed
Correctly determine remote IP if behind reverse proxy
1 parent a159629 commit 9e2977f

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
@@ -695,12 +695,14 @@ def __user_is_authorized(self, user, password):
695695
:param User user: User instance
696696
:param str password: Password
697697
"""
698+
remote_addr = request.headers.get("X-Forwarded-For", request.remote_addr).split(",")[0].strip()
699+
self.logger.info("Remote IP is %s" % remote_addr)
698700
# Check if IP blacklisted
699701
if self.ip_blacklist_duration > 0:
700-
entry = ip_blacklist.lookup(request.remote_addr)
702+
entry = ip_blacklist.lookup(remote_addr)
701703
count = entry['value'] if entry else 0
702704
if count >= self.ip_blacklist_max_attempt_count:
703-
self.logger.info("IP %s is blacklisted with %s attempts" % (request.remote_addr, count))
705+
self.logger.info("IP %s is blacklisted with %s attempts" % (remote_addr, count))
704706
return False, i18n.t('auth.ip_blacklisted')
705707

706708
if user is None or user.password_hash is None:
@@ -724,10 +726,10 @@ def __user_is_authorized(self, user, password):
724726

725727
# add to ip blacklist
726728
if self.ip_blacklist_duration > 0:
727-
entry = ip_blacklist.lookup(request.remote_addr)
729+
entry = ip_blacklist.lookup(remote_addr)
728730
count = entry['value'] if entry else 0
729-
ip_blacklist.set(request.remote_addr, count + 1, self.ip_blacklist_duration)
730-
self.logger.info("Attempt count for IP %s: %s" % (request.remote_addr, count + 1))
731+
ip_blacklist.set(remote_addr, count + 1, self.ip_blacklist_duration)
732+
self.logger.info("Attempt count for IP %s: %s" % (remote_addr, count + 1))
731733

732734
# increase failed login attempts counter
733735
user.failed_sign_in_count += 1

0 commit comments

Comments
 (0)