Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(library): clamp documents API limit/offset to prevent unbounded load (#4560) (#4596)

LearningCircuit46ab20010ca6
diff --git a/changelog.d/+clamp-library-documents-limit.bugfix.md b/changelog.d/+clamp-library-documents-limit.bugfix.md
new file mode 100644
index 0000000000..368b2251a6
--- /dev/null
+++ b/changelog.d/+clamp-library-documents-limit.bugfix.md
@@ -0,0 +1 @@
+The library documents API now clamps the `limit`/`offset` query parameters, so a negative `limit` (which SQLite treats as "no limit") can no longer bypass pagination and load an entire collection into memory.
diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.py
index 4b6086eba3..f01b870150 100644
--- a/src/local_deep_research/research_library/routes/library_routes.py
+++ b/src/local_deep_research/research_library/routes/library_routes.py
@@ -339,8 +339,13 @@ def get_documents():
file_type = request.args.get("file_type")
favorites_only = request.args.get("favorites") == "true"
search_query = request.args.get("search")
- limit = int(request.args.get("limit", 100))
- offset = int(request.args.get("offset", 0))
+ # Clamp pagination. SQLite treats LIMIT -1 as "no limit", so an
+ # unclamped negative limit (e.g. ?limit=-1) would bypass pagination and
+ # load the whole collection — including large Document.text_content
+ # bodies — into memory (#4560). Mirrors the clamp in history_routes.
+ limit = request.args.get("limit", 100, type=int)
+ limit = max(1, min(limit, 1000))
+ offset = max(0, request.args.get("offset", 0, type=int))
documents = service.get_documents(
research_id=research_id,
diff --git a/tests/research_library/routes/test_library_routes_deep_coverage.py b/tests/research_library/routes/test_library_routes_deep_coverage.py
index b6c438c622..0ab8753ff4 100644
--- a/tests/research_library/routes/test_library_routes_deep_coverage.py
+++ b/tests/research_library/routes/test_library_routes_deep_coverage.py
@@ -385,6 +385,29 @@ class TestGetDocumentsPagination:
offset=5,
)
+ def test_negative_limit_is_clamped_not_unbounded(self, app):
+ """?limit=-1 must never reach the service: SQLite treats LIMIT -1 as
+ "no limit", which would load the whole collection into memory. It is
+ clamped to >= 1 and offset to >= 0 (#4560)."""
+ lib_svc = Mock()
+ lib_svc.get_documents.return_value = []
+ with _auth_client(app, library_service=lib_svc) as (client, _):
+ resp = client.get("/library/api/documents?limit=-1&offset=-5")
+ assert resp.status_code == 200
+ _, kwargs = lib_svc.get_documents.call_args
+ assert kwargs["limit"] == 1
+ assert kwargs["offset"] == 0
+
+ def test_oversized_limit_is_capped(self, app):
+ """A huge limit is capped to the upper bound so it can't load an
+ unbounded number of rows (#4560)."""
+ lib_svc = Mock()
+ lib_svc.get_documents.return_value = []
+ with _auth_client(app, library_service=lib_svc) as (client, _):
+ client.get("/library/api/documents?limit=999999")
+ _, kwargs = lib_svc.get_documents.call_args
+ assert kwargs["limit"] == 1000
+
def test_returns_empty_list_when_no_documents(self, app):
lib_svc = Mock()
lib_svc.get_documents.return_value = []