Compare commits

..
Author SHA1 Message Date
shamoon b92f0159ac Fix: correct token throttling behind trusted proxies 2026-11-24 16:59:49 -08:00
6 changed files with 88 additions and 49 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
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=<header-name>`](#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER) {#PAPERLESS_ALLAUTH_TRUSTED_CLIENT_IP_HEADER}
+1 -7
View File
@@ -71,7 +71,7 @@ dependencies = [
"scikit-learn~=1.9.0",
"sentence-transformers>=5.6.1",
"setproctitle~=1.3.4",
"sqlite-vec==0.1.10+paperless.3",
"sqlite-vec==0.1.9",
"tantivy~=0.26.0",
"tika-client[httpx]~=1.0",
"torch>=2.13,<2.15",
@@ -164,12 +164,6 @@ psycopg-c = [
{ url = "https://github.com/paperless-ngx/builder/releases/download/psycopg-trixie-3.3.8/psycopg_c-3.3.8-cp314-cp314-manylinux_2_38_x86_64.whl", marker = "sys_platform == 'linux' and platform_machine == 'x86_64' and python_version == '3.14'" },
{ url = "https://github.com/paperless-ngx/builder/releases/download/psycopg-trixie-3.3.8/psycopg_c-3.3.8-cp314-cp314-manylinux_2_38_aarch64.whl", marker = "sys_platform == 'linux' and platform_machine == 'aarch64' and python_version == '3.14'" },
]
# Patched sqlite-vec builds
sqlite-vec = [
{ url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_x86_64.whl", marker = "sys_platform == 'linux' and platform_machine == 'x86_64'" },
{ url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_aarch64.whl", marker = "sys_platform == 'linux' and platform_machine == 'aarch64'" },
{ url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-macosx_11_0_arm64.whl", marker = "sys_platform == 'darwin' and platform_machine == 'arm64'" },
]
torch = [
{ index = "pytorch-cpu" },
]
+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(
"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")
@@ -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:
+60
View File
@@ -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"))
Generated
+7 -42
View File
@@ -2562,9 +2562,7 @@ dependencies = [
{ name = "scikit-learn" },
{ name = "sentence-transformers" },
{ name = "setproctitle" },
{ name = "sqlite-vec", version = "0.1.10+paperless.3", source = { url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-macosx_11_0_arm64.whl" }, marker = "sys_platform != 'linux'" },
{ name = "sqlite-vec", version = "0.1.10+paperless.3", source = { url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_aarch64.whl" }, marker = "platform_machine != 'x86_64' and sys_platform == 'linux'" },
{ name = "sqlite-vec", version = "0.1.10+paperless.3", source = { url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_x86_64.whl" }, marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
{ name = "sqlite-vec" },
{ name = "tantivy" },
{ name = "tika-client", extra = ["httpx"] },
{ name = "torch", version = "2.14.0", source = { registry = "https://download.pytorch.org/whl/cpu" }, marker = "sys_platform != 'linux'" },
@@ -2723,10 +2721,7 @@ requires-dist = [
{ name = "scikit-learn", specifier = "~=1.9.0" },
{ name = "sentence-transformers", specifier = ">=5.6.1" },
{ name = "setproctitle", specifier = "~=1.3.4" },
{ name = "sqlite-vec", marker = "(platform_machine != 'aarch64' and platform_machine != 'arm64' and platform_machine != 'x86_64') or (platform_machine == 'aarch64' and sys_platform == 'darwin') or (platform_machine == 'x86_64' and sys_platform == 'darwin') or (platform_machine == 'arm64' and sys_platform == 'linux') or (sys_platform != 'darwin' and sys_platform != 'linux')", specifier = "==0.1.10+paperless.3" },
{ name = "sqlite-vec", marker = "platform_machine == 'arm64' and sys_platform == 'darwin'", url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-macosx_11_0_arm64.whl" },
{ name = "sqlite-vec", marker = "platform_machine == 'aarch64' and sys_platform == 'linux'", url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_aarch64.whl" },
{ name = "sqlite-vec", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'", url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_x86_64.whl" },
{ name = "sqlite-vec", specifier = "==0.1.9" },
{ name = "tantivy", specifier = "~=0.26.0" },
{ name = "tika-client", extras = ["httpx"], specifier = "~=1.0" },
{ name = "torch", specifier = ">=2.13,<2.15", index = "https://download.pytorch.org/whl/cpu" },
@@ -4113,42 +4108,12 @@ asyncio = [
[[package]]
name = "sqlite-vec"
version = "0.1.10+paperless.3"
source = { url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-macosx_11_0_arm64.whl" }
resolution-markers = [
"python_full_version >= '3.15' and platform_machine == 'arm64' and platform_python_implementation == 'CPython' and sys_platform == 'darwin'",
"python_full_version < '3.15' and platform_machine == 'arm64' and platform_python_implementation == 'CPython' and sys_platform == 'darwin'",
]
version = "0.1.9"
source = { registry = "https://pypi.org/simple" }
wheels = [
{ url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-macosx_11_0_arm64.whl", hash = "sha256:8cd483975df4e833cfa10d3026bb6a7e71be2cbe3fcb7546952178849d45fa03" },
]
[[package]]
name = "sqlite-vec"
version = "0.1.10+paperless.3"
source = { url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_aarch64.whl" }
resolution-markers = [
"python_full_version >= '3.15' and platform_machine == 'aarch64' and platform_python_implementation == 'CPython' and sys_platform == 'linux'",
"python_full_version == '3.14.*' and platform_machine == 'aarch64' and platform_python_implementation == 'CPython' and sys_platform == 'linux'",
"python_full_version == '3.12.*' and platform_machine == 'aarch64' and platform_python_implementation == 'CPython' and sys_platform == 'linux'",
"(python_full_version < '3.12' and platform_machine == 'aarch64' and platform_python_implementation == 'CPython' and sys_platform == 'linux') or (python_full_version == '3.13.*' and platform_machine == 'aarch64' and platform_python_implementation == 'CPython' and sys_platform == 'linux')",
]
wheels = [
{ url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_aarch64.whl", hash = "sha256:71508033874aef98ef1a4006b4e1675be9bd9f866b3444491db480ce870a62c2" },
]
[[package]]
name = "sqlite-vec"
version = "0.1.10+paperless.3"
source = { url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_x86_64.whl" }
resolution-markers = [
"python_full_version >= '3.15' and platform_machine == 'x86_64' and platform_python_implementation == 'CPython' and sys_platform == 'linux'",
"python_full_version == '3.14.*' and platform_machine == 'x86_64' and platform_python_implementation == 'CPython' and sys_platform == 'linux'",
"python_full_version == '3.12.*' and platform_machine == 'x86_64' and platform_python_implementation == 'CPython' and sys_platform == 'linux'",
"(python_full_version < '3.12' and platform_machine == 'x86_64' and platform_python_implementation == 'CPython' and sys_platform == 'linux') or (python_full_version == '3.13.*' and platform_machine == 'x86_64' and platform_python_implementation == 'CPython' and sys_platform == 'linux')",
]
wheels = [
{ url = "https://github.com/paperless-ngx/builder/releases/download/sqlite-vec-0.1.10%2Bpaperless.3/sqlite_vec-0.1.10%2Bpaperless.3-py3-none-manylinux_2_29_x86_64.whl", hash = "sha256:9c8972c37295498460223ed592e6d8ab8e81d093604e645821fd36ec4ca30090" },
{ url = "https://files.pythonhosted.org/packages/a4/3d/3677e0cd2f92e5ebc43cd29fbf565b75582bff1ccfa0b8327c7508e1084f/sqlite_vec-0.1.9-py3-none-macosx_11_0_arm64.whl", hash = "sha256:1d52e30513bae4cc9778ddbf6145610434081be4c3afe57cd877893bad9f6b6c", size = 165434, upload-time = "2026-03-31T08:02:32.712Z" },
{ url = "https://files.pythonhosted.org/packages/00/d4/f2b936d3bdc38eadcbd2a87875815db36430fab0363182ba5d12cd8e0b51/sqlite_vec-0.1.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4e921e592f24a5f9a18f590b6ddd530eb637e2d474e3b1972f9bbeb773aa3cb9", size = 160076, upload-time = "2026-03-31T08:02:33.796Z" },
{ url = "https://files.pythonhosted.org/packages/6f/ad/6afd073b0f817b3e03f9e37ad626ae341805891f23c74b5292818f49ac63/sqlite_vec-0.1.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux1_x86_64.whl", hash = "sha256:1515727990b49e79bcaf75fdee2ffc7d461f8b66905013231251f1c8938e7786", size = 163388, upload-time = "2026-03-31T08:02:34.888Z" },
]
[[package]]