| 1 |
.. _rate_limits: |
| 2 |
|
| 3 |
Rate Limits |
| 4 |
=========== |
| 5 |
|
| 6 |
In order to be secure out of the box various rate limits are in place. The rate |
| 7 |
limit mechanism is backed by a Django cache. Hence, rate limiting will not work |
| 8 |
properly if you are using the `DummyCache`. |
| 9 |
|
| 10 |
When rate limits are hit the ``429.html`` template is rendered, |
| 11 |
alternatively, you can configure a custom handler by declaring |
| 12 |
a ``handler429`` view in your root URLconf. |
| 13 |
|
| 14 |
Rate limits are consumed by triggering actions, the full list of which is |
| 15 |
documented below. Per action, the rate can be configured. The rate itself is an |
| 16 |
amount, per time unit, per either IP address, user or action-specific key. |
| 17 |
|
| 18 |
For example, requesting a password reset is an action that is both limited |
| 19 |
globally by IP address, as well as per email. Here, the email address used is |
| 20 |
the specific key. |
| 21 |
|
| 22 |
|
| 23 |
.. danger:: |
| 24 |
|
| 25 |
Rate limits rely on accurate client IP address detection to function correctly. |
| 26 |
However, **it is not possible for django-allauth to reliably determine the |
| 27 |
client IP address out of the box** because the correct method varies depending |
| 28 |
on your deployment architecture (direct connections, load balancers, reverse |
| 29 |
proxies, CDNs, etc.). |
| 30 |
|
| 31 |
The ``X-Forwarded-For`` header cannot be used to determine the client IP, |
| 32 |
as **this header can be trivially spoofed by malicious actors**, allowing them |
| 33 |
to completely bypass rate limits. |
| 34 |
|
| 35 |
To ensure rate limits work correctly: |
| 36 |
|
| 37 |
1. **Review and adjust the rate limit configuration settings** documented below |
| 38 |
(``ALLAUTH_TRUSTED_PROXY_COUNT``, ``ALLAUTH_TRUSTED_CLIENT_IP_HEADER``) |
| 39 |
to match your security requirements and deployment architecture. |
| 40 |
|
| 41 |
2. If the settings are not sufficient, **override the account adapter's** |
| 42 |
``get_client_ip()`` method to implement custom logic for extracting the |
| 43 |
real client IP address from the correct header(s) for your specific |
| 44 |
infrastructure. See the :doc:`adapter documentation <../account/adapter>` for |
| 45 |
guidance on implementing a custom adapter. |
| 46 |
|
| 47 |
|
| 48 |
Configuration |
| 49 |
------------- |
| 50 |
|
| 51 |
``ALLAUTH_TRUSTED_PROXY_COUNT`` (default: ``0``) |
| 52 |
As the ``X-Forwarded-For`` header can be spoofed, you need to |
| 53 |
configure the number of proxies that are under your control and hence, |
| 54 |
can be trusted. The default is 0, meaning, no proxies are trusted. As a |
| 55 |
result, the ``X-Forwarded-For`` header will be disregarded by default. |
| 56 |
|
| 57 |
``ALLAUTH_TRUSTED_CLIENT_IP_HEADER`` (default: ``None``) |
| 58 |
If your service is running behind a trusted proxy that sets a custom header |
| 59 |
containing the client IP address, specify that header name here. The client |
| 60 |
IP will be extracted from this header instead of ``X-Forwarded-For``. |
| 61 |
Examples: ``"CF-Connecting-IP"`` (Cloudflare), ``"X-Real-IP"`` (nginx). |
| 62 |
|
| 63 |
|
| 64 |
Implementation Notes |
| 65 |
-------------------- |
| 66 |
|
| 67 |
The builtin rate limitting relies on a cache and uses non-atomic operations, |
| 68 |
making it vulnerable to race conditions. As a result, users may occasionally |
| 69 |
bypass the intended rate limit due to concurrent access. However, such race |
| 70 |
conditions are rare in practice. For example, if the limit is set to 10 requests |
| 71 |
per minute and a large number of parallel processes attempt to test that limit, |
| 72 |
you may occasionally observe slight overruns—such as 11 or 12 requests slipping |
| 73 |
through. Nevertheless, exceeding the limit by a large margin is highly unlikely |
| 74 |
due to the low probability of many processes entering the critical non-atomic |
| 75 |
code section simultaneously. |
| 76 |
|
| 77 |
|
| 78 |
Testing |
| 79 |
------- |
| 80 |
|
| 81 |
Unless the rate limit is disabled or the default limits are increased, you might |
| 82 |
run intro problems if you're running unit tests that are dependant on |
| 83 |
funcionalities covered by the rate limits. For example, if you're testing the |
| 84 |
`confirm_email` functionality in your unit tests and you're testing if the |
| 85 |
verification email is sent twice after requesting it twice, only one of the |
| 86 |
emails will be sent. |