mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-29 08:13:19 +00:00
Fix: cap autocomplete limit so a huge value cannot abort the process (#14905)
This commit is contained in:
1 parent
ca32553478
commit
5cea561838
4 files changed
+21
-3
No files matched your search
@@ -74,6 +74,10 @@ _LOCK_BACKOFF_CAP: Final[float] = 10.0 # seconds
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
# Upper bound on autocomplete suggestions. tantivy sizes its collector from the
|
||||
# requested limit, so an unbounded value aborts the whole process on allocation.
|
||||
MAX_AUTOCOMPLETE_LIMIT = 1000
|
||||
|
||||
|
||||
class ViewerGrant(NamedTuple):
|
||||
"""Direct user and group view grants for a single document.
|
||||
@@ -998,7 +1002,8 @@ class TantivyBackend:
|
||||
|
||||
Args:
|
||||
term: Prefix to match against autocomplete words
|
||||
limit: Maximum number of suggestions to return
|
||||
limit: Maximum number of suggestions to return, capped at
|
||||
MAX_AUTOCOMPLETE_LIMIT
|
||||
user: User for permission filtering (None for no filtering)
|
||||
|
||||
Returns:
|
||||
@@ -1021,7 +1026,7 @@ class TantivyBackend:
|
||||
"autocomplete_word",
|
||||
normalized_term,
|
||||
permission_query,
|
||||
limit,
|
||||
min(limit, MAX_AUTOCOMPLETE_LIMIT),
|
||||
)
|
||||
|
||||
return [x[0] for x in matches]
|
||||
|
||||
@@ -1069,6 +1069,13 @@ class TestAutocomplete:
|
||||
results = backend.autocomplete("micro", limit=10)
|
||||
assert "microsoft" in results
|
||||
|
||||
def test_huge_limit_is_clamped(self, backend: TantivyBackend) -> None:
|
||||
"""An oversized limit must be capped, not handed to tantivy as-is."""
|
||||
doc = DocumentFactory.create(title="Microsoft invoice", content="payment")
|
||||
backend.add_or_update(doc)
|
||||
|
||||
assert "microsoft" in backend.autocomplete("micro", limit=10**15)
|
||||
|
||||
def test_results_ordered_by_document_frequency(
|
||||
self,
|
||||
backend: TantivyBackend,
|
||||
|
||||
@@ -996,6 +996,9 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase):
|
||||
response = self.client.get("/api/search/autocomplete/?term=test&limit=-1")
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
response = self.client.get("/api/search/autocomplete/?term=test&limit=null")
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
response = self.client.get("/api/search/autocomplete/")
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
@@ -3737,7 +3737,10 @@ class SearchAutoCompleteView(GenericAPIView[Any]):
|
||||
return HttpResponseBadRequest("Term required")
|
||||
|
||||
if "limit" in request.query_params:
|
||||
limit = int(request.query_params["limit"])
|
||||
try:
|
||||
limit = int(request.query_params["limit"])
|
||||
except ValueError:
|
||||
return HttpResponseBadRequest("Invalid limit")
|
||||
if limit <= 0:
|
||||
return HttpResponseBadRequest("Invalid limit")
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user