From b92f0159ace120d0aec2136f52b3d9af213293dc Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:37:47 -0800 Subject: [PATCH] Fix: correct token throttling behind trusted proxies --- docs/configuration.md | 6 ++ src/paperless/settings/__init__.py | 5 ++ src/paperless/tests/settings/test_settings.py | 9 +++ src/paperless/tests/test_api_auth.py | 60 +++++++++++++++++++ 4 files changed, 80 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index a2b064faf9..5b43c85401 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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 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`. #### [`PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER=`](#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER) {#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER} diff --git a/src/paperless/settings/__init__.py b/src/paperless/settings/__init__.py index 5279684f12..d7405545c4 100644 --- a/src/paperless/settings/__init__.py +++ b/src/paperless/settings/__init__.py @@ -505,6 +505,11 @@ ALLAUTH_TRUSTED_PROXY_COUNT = _get_allauth_trusted_proxy_count(TRUSTED_PROXIES) ALLAUTH_TRUSTED_CLIENT_IP_HEADER = os.getenv( "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_PORT = get_bool_from_env("PAPERLESS_USE_X_FORWARD_PORT", "false") diff --git a/src/paperless/tests/settings/test_settings.py b/src/paperless/tests/settings/test_settings.py index 232aea2663..c7a56df06f 100644 --- a/src/paperless/tests/settings/test_settings.py +++ b/src/paperless/tests/settings/test_settings.py @@ -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 +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( monkeypatch: pytest.MonkeyPatch, ) -> None: diff --git a/src/paperless/tests/test_api_auth.py b/src/paperless/tests/test_api_auth.py index f0a98dc7ed..a2eae1fc60 100644 --- a/src/paperless/tests/test_api_auth.py +++ b/src/paperless/tests/test_api_auth.py @@ -1,14 +1,74 @@ 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 override_settings from django.urls import resolve from django.urls import reverse from rest_framework import status +from rest_framework.throttling import ScopedRateThrottle 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): def test_api_auth_login_uses_allauth_login_view(self): response = self.client.get(reverse("rest_framework:login"))