Skip to content

Commit

Permalink
Update django-ipware configuration flags to new AXES_IPWARE_ prefixes
Browse files Browse the repository at this point in the history
Use explicit new AXES_IPWARE_ referencing configuration flag names
in place of the old plain implicit AXES_ name prefixes
  • Loading branch information
wannacfuture committed Apr 28, 2023
1 parent 2612db5 commit 3ac6272
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
6 changes: 6 additions & 0 deletions axes/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def axes_deprecation_check(app_configs, **kwargs): # pylint: disable=unused-arg
deprecated_settings = [
"AXES_DISABLE_SUCCESS_ACCESS_LOG",
"AXES_LOGGER",
# AXES_PROXY_ and AXES_META_ parameters were updated to more explicit
# AXES_IPWARE_PROXY_ and AXES_IPWARE_META_ prefixes in version 6.x
"AXES_PROXY_ORDER",
"AXES_PROXY_COUNT",
"AXES_PROXY_TRUSTED_IPS",
"AXES_META_PRECEDENCE_ORDER",
]

for deprecated_setting in deprecated_settings:
Expand Down
59 changes: 41 additions & 18 deletions axes/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,6 @@
),
)

# if your deployment is using reverse proxies, set this value to 'left-most' or 'right-most' per your configuration
settings.AXES_PROXY_ORDER = getattr(settings, "AXES_PROXY_ORDER", "left-most")

# if your deployment is using reverse proxies, set this value to the number of proxies in front of Django
settings.AXES_PROXY_COUNT = getattr(settings, "AXES_PROXY_COUNT", None)

# if your deployment is using reverse proxies, set to your trusted proxy IP addresses prefixes if needed
settings.AXES_PROXY_TRUSTED_IPS = getattr(settings, "AXES_PROXY_TRUSTED_IPS", None)

# set to the names of request.META attributes that should be checked for the IP address of the client
# if your deployment is using reverse proxies, ensure that the header attributes are securely set by the proxy
# ensure that the client can not spoof the headers by setting them and sending them through the proxy
settings.AXES_META_PRECEDENCE_ORDER = getattr(
settings,
"AXES_META_PRECEDENCE_ORDER",
getattr(settings, "IPWARE_META_PRECEDENCE_ORDER", ("REMOTE_ADDR",)),
)

# set CORS allowed origins when calling authentication over ajax
settings.AXES_ALLOWED_CORS_ORIGINS = getattr(settings, "AXES_ALLOWED_CORS_ORIGINS", "*")

Expand All @@ -147,3 +129,44 @@
settings.AXES_RESET_COOL_OFF_ON_FAILURE_DURING_LOCKOUT = getattr(
settings, "AXES_RESET_COOL_OFF_ON_FAILURE_DURING_LOCKOUT", True
)


###
# django-ipware settings for client IP address calculation and proxy detection
# there are old AXES_PROXY_ and AXES_META_ legacy keys present for backwards compatibility
# see https://github.com/un33k/django-ipware for further details
###

# if your deployment is using reverse proxies, set this value to 'left-most' or 'right-most' per your configuration
settings.AXES_IPWARE_PROXY_ORDER = getattr(
settings,
"AXES_IPWARE_PROXY_ORDER",
getattr(settings, "AXES_PROXY_ORDER", "left-most"),
)

# if your deployment is using reverse proxies, set this value to the number of proxies in front of Django
settings.AXES_IPWARE_PROXY_COUNT = getattr(
settings,
"AXES_IPWARE_PROXY_COUNT",
getattr(settings, "AXES_PROXY_COUNT", None),
)

# if your deployment is using reverse proxies, set to your trusted proxy IP addresses prefixes if needed
settings.AXES_IPWARE_PROXY_TRUSTED_IPS = getattr(
settings,
"AXES_IPWARE_PROXY_TRUSTED_IPS",
getattr(settings, "AXES_PROXY_TRUSTED_IPS", None),
)

# set to the names of request.META attributes that should be checked for the IP address of the client
# if your deployment is using reverse proxies, ensure that the header attributes are securely set by the proxy
# ensure that the client can not spoof the headers by setting them and sending them through the proxy
settings.AXES_IPWARE_META_PRECEDENCE_ORDER = getattr(
settings,
"AXES_IPWARE_META_PRECEDENCE_ORDER",
getattr(
settings,
"AXES_META_PRECEDENCE_ORDER",
getattr(settings, "IPWARE_META_PRECEDENCE_ORDER", ("REMOTE_ADDR",)),
),
)
8 changes: 4 additions & 4 deletions axes/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ def get_client_ip_address(

client_ip_address, _ = ipware.ip.get_client_ip(
request,
proxy_order=settings.AXES_PROXY_ORDER,
proxy_count=settings.AXES_PROXY_COUNT,
proxy_trusted_ips=settings.AXES_PROXY_TRUSTED_IPS,
request_header_order=settings.AXES_META_PRECEDENCE_ORDER,
proxy_order=settings.AXES_IPWARE_PROXY_ORDER,
proxy_count=settings.AXES_IPWARE_PROXY_COUNT,
proxy_trusted_ips=settings.AXES_IPWARE_PROXY_TRUSTED_IPS,
request_header_order=settings.AXES_IPWARE_META_PRECEDENCE_ORDER,
)
return client_ip_address

Expand Down
6 changes: 3 additions & 3 deletions docs/4_configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ and uses some conservative configuration parameters by default for security.
If you are using reverse proxies, you will need to configure one or more of the
following settings to suit your set up to correctly resolve client IP addresses:

* ``AXES_PROXY_COUNT``: The number of reverse proxies in front of Django as an integer. Default: ``None``
* ``AXES_META_PRECEDENCE_ORDER``: The names of ``request.META`` attributes as a tuple of strings
* ``AXES_IPWARE_PROXY_COUNT``: The number of reverse proxies in front of Django as an integer. Default: ``None``
* ``AXES_IPWARE_META_PRECEDENCE_ORDER``: The names of ``request.META`` attributes as a tuple of strings
to check to get the client IP address. Check the Django documentation for header naming conventions.
Default: ``IPWARE_META_PRECEDENCE_ORDER`` setting if set, else ``('REMOTE_ADDR', )``

Expand All @@ -114,7 +114,7 @@ following settings to suit your set up to correctly resolve client IP addresses:
.. code-block:: python
# refer to the Django request and response objects documentation
AXES_META_PRECEDENCE_ORDER = [
AXES_IPWARE_META_PRECEDENCE_ORDER = [
'HTTP_X_FORWARDED_FOR',
'REMOTE_ADDR',
]
Expand Down

0 comments on commit 3ac6272

Please sign in to comment.