axes: Minor fixes to configuration for IP blocking

- Use the X-Forwarded-For header only if specified in the configuration. This
  makes FreedomBox safe to use when not behind a reverse proxy server (although
  we are unlikely to do this).

- When fetching the IP address to reset after successful login, use the
  X-Forwarded-For header only if specified in the configuration.

- Minor flake8 refactorings.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2019-01-28 17:17:32 -05:00
1 parent 22dd2d26f3
commit dc9ab52edc
7 files changed
+44 -23

No files matched your search

+16 -1
View File
@@ -72,6 +72,10 @@ def init():
if cfg.secure_proxy_ssl_header:
secure_proxy_ssl_header = (cfg.secure_proxy_ssl_header, 'https')
ipware_meta_precedence_order = ('REMOTE_ADDR', )
if cfg.use_x_forwarded_for:
ipware_meta_precedence_order = ('HTTP_X_FORWARDED_FOR', )
pwd = 'django.contrib.auth.password_validation'
django.conf.settings.configure(
@@ -113,7 +117,7 @@ def init():
DEBUG=cfg.develop,
FORCE_SCRIPT_NAME=cfg.server_dir,
INSTALLED_APPS=applications,
IPWARE_META_PRECEDENCE_ORDER=('HTTP_X_FORWARDED_FOR', ),
IPWARE_META_PRECEDENCE_ORDER=ipware_meta_precedence_order,
LANGUAGES=get_languages(),
LOGGING=log.get_configuration(),
LOGIN_URL='users:login',
@@ -191,3 +195,14 @@ def get_wsgi_application():
def get_static_url():
"""Return Django static URL."""
return django.conf.settings.STATIC_URL
def get_ip_address_from_request(request):
"""Return the IP address of the original client."""
if cfg.use_x_forwarded_for:
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
ip_address = x_forwarded_for.split(',')[0]
else:
ip_address = request.META.get('REMOTE_ADDR')
return ip_address