Compare commits

...
Author SHA1 Message Date
shamoon b92f0159ac Fix: correct token throttling behind trusted proxies 2026-11-24 16:59:49 -08:00
4 changed files with 80 additions and 0 deletions

No files matched your search

+6
View File
@@ -551,6 +551,12 @@ when its IPv4 and IPv6 addresses are both listed in
Only trust `X-Forwarded-For` when untrusted clients cannot connect directly Only trust `X-Forwarded-For` when untrusted clients cannot connect directly
to Paperless-ngx. to Paperless-ngx.
This count also controls which address Django REST Framework uses from
`X-Forwarded-For` for the API token throttle. With a count of zero, the
throttle uses the connecting address instead. If
`PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER` is set, the token throttle
uses the connecting address regardless of this count.
Defaults to the number of entries in `PAPERLESS_TRUSTED_PROXIES`. Defaults to the number of entries in `PAPERLESS_TRUSTED_PROXIES`.
#### [`PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER=<header-name>`](#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER) {#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER} #### [`PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER=<header-name>`](#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER) {#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER}
+5
View File
@@ -505,6 +505,11 @@ ALLAUTH_TRUSTED_PROXY_COUNT = _get_allauth_trusted_proxy_count(TRUSTED_PROXIES)
ALLAUTH_TRUSTED_CLIENT_IP_HEADER = os.getenv( ALLAUTH_TRUSTED_CLIENT_IP_HEADER = os.getenv(
"PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER", "PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER",
) )
# Do not use client-supplied X-Forwarded-For values for the API token throttle.
# A dedicated allauth header does not imply that X-Forwarded-For is trusted.
REST_FRAMEWORK["NUM_PROXIES"] = (
0 if ALLAUTH_TRUSTED_CLIENT_IP_HEADER else ALLAUTH_TRUSTED_PROXY_COUNT
)
USE_X_FORWARDED_HOST = get_bool_from_env("PAPERLESS_USE_X_FORWARD_HOST", "false") USE_X_FORWARDED_HOST = get_bool_from_env("PAPERLESS_USE_X_FORWARD_HOST", "false")
USE_X_FORWARDED_PORT = get_bool_from_env("PAPERLESS_USE_X_FORWARD_PORT", "false") USE_X_FORWARDED_PORT = get_bool_from_env("PAPERLESS_USE_X_FORWARD_PORT", "false")
@@ -90,6 +90,15 @@ def test_allauth_trusted_proxy_count_defaults_to_trusted_proxies(
assert _get_allauth_trusted_proxy_count(["proxy-v4", "proxy-v6"]) == 2 assert _get_allauth_trusted_proxy_count(["proxy-v4", "proxy-v6"]) == 2
def test_token_throttle_proxy_count_uses_trusted_proxy_configuration() -> None:
expected = (
0
if paperless_settings.ALLAUTH_TRUSTED_CLIENT_IP_HEADER
else paperless_settings.ALLAUTH_TRUSTED_PROXY_COUNT
)
assert paperless_settings.REST_FRAMEWORK["NUM_PROXIES"] == expected
def test_allauth_trusted_proxy_count_can_be_overridden( def test_allauth_trusted_proxy_count_can_be_overridden(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None:
+60
View File
@@ -1,14 +1,74 @@
import uuid import uuid
from unittest import mock
from django.conf import settings
from django.core.cache.backends.locmem import LocMemCache
from django.test import TestCase from django.test import TestCase
from django.test import override_settings from django.test import override_settings
from django.urls import resolve from django.urls import resolve
from django.urls import reverse from django.urls import reverse
from rest_framework import status from rest_framework import status
from rest_framework.throttling import ScopedRateThrottle
from paperless_testing.factories import UserFactory from paperless_testing.factories import UserFactory
@override_settings(
REST_FRAMEWORK={
**settings.REST_FRAMEWORK,
"NUM_PROXIES": 0,
},
)
class TestTokenThrottle(TestCase):
def setUp(self):
super().setUp()
self.enterContext(
mock.patch.object(ScopedRateThrottle, "THROTTLE_RATES", {"login": "5/min"}),
)
self.enterContext(
mock.patch.object(
ScopedRateThrottle,
"cache",
LocMemCache(uuid.uuid4().hex, {}),
),
)
def _request_token(self, forwarded_for, remote_addr="203.0.113.10"):
return self.client.post(
"/api/token/",
{"username": "unknown", "password": "wrong"},
HTTP_X_FORWARDED_FOR=forwarded_for,
REMOTE_ADDR=remote_addr,
)
def test_untrusted_forwarded_for_cannot_bypass_limit(self):
for attempt in range(5):
response = self._request_token(f"198.51.100.{attempt}")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = self._request_token("198.51.100.99")
self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)
@override_settings(
REST_FRAMEWORK={
**settings.REST_FRAMEWORK,
"NUM_PROXIES": 1,
},
)
def test_trusted_proxy_uses_client_address(self):
for attempt in range(5):
response = self._request_token(
f"198.51.100.{attempt}, 192.0.2.20",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = self._request_token("198.51.100.99, 192.0.2.20")
self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)
response = self._request_token("192.0.2.21")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
class TestApiAuthViews(TestCase): class TestApiAuthViews(TestCase):
def test_api_auth_login_uses_allauth_login_view(self): def test_api_auth_login_uses_allauth_login_view(self):
response = self.client.get(reverse("rest_framework:login")) response = self.client.get(reverse("rest_framework:login"))