~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""POST /library/api/collections/{id}/upload must enforce the SAME limits
GET /api/config/limits advertises to the frontend as authoritative.
Regression fence: rag.py's ``upload_to_collection`` used to hardcode its own
per-file (100MB), per-request-count (50), and total (5GB) upload caps instead
of reading them from ``FileUploadValidator`` — the same class
research.py's ``GET /api/config/limits`` exposes and documents as "the
backend's authoritative limits ... allowing the frontend to stay in sync".
That meant a file the frontend was told (via /api/config/limits) is
acceptable could be silently rejected by this route with a different,
undocumented cap. origin/main's Flask handler for this exact route used
``FileUploadValidator.validate_file_count`` / ``validate_file_size``
uniformly; the FastAPI port dropped that and hardcoded new constants.
These tests patch ``FileUploadValidator.MAX_FILE_SIZE`` /
``MAX_FILES_PER_REQUEST`` to small values rather than setting
``LDR_SECURITY_UPLOAD_MAX_FILE_SIZE_MB``: ``FileUploadValidator.MAX_FILE_SIZE``
is resolved from the env var exactly once, at class-definition time (module
import), so setting the env var mid-test-process has no effect without
reloading the module — and reloading it would leave every module that
already did ``from ...security import FileUploadValidator`` (rag.py,
research.py, fastapi_app.py) holding a stale class object, since the
``security`` package's own namespace binding wouldn't be updated by
reloading the submodule alone. ``tests/security/test_file_upload_validator.py``
hits the same constraint and uses ``patch.object(FileUploadValidator,
"MAX_FILE_SIZE", ...)`` for it (see its lines ~114, ~137, ~353) — this file
follows that established, already-idiomatic pattern instead of fighting
Python's import caching. Patching the class attribute is the exact effect
setting the env var before process start would have produced.
"""
import uuid
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from local_deep_research.security.file_upload_validator import (
FileUploadValidator,
)
@pytest.fixture(scope="module")
def auth_client():
"""Authenticated test client (same pattern as test_collection_upload_http.py)."""
from local_deep_research.web.fastapi_app import app
c = TestClient(app, raise_server_exceptions=False)
user = f"test_rag_limits_{uuid.uuid4().hex[:8]}"
pw = "TestPassword123!" # noqa: S105
def _csrf():
c.get("/auth/login")
r = c.get("/auth/csrf-token")
return r.json().get("csrf_token", "") if r.status_code == 200 else ""
c.post(
"/auth/register",
data={
"username": user,
"password": pw,
"confirm_password": pw,
"acknowledge": "true",
"csrf_token": _csrf(),
},
follow_redirects=False,
)
resp = c.post(
"/auth/login",
data={"username": user, "password": pw, "csrf_token": _csrf()},
follow_redirects=False,
)
if resp.status_code != 302:
pytest.fail(
f"Login bootstrap failed: expected 302, got {resp.status_code}: "
f"{resp.text[:500]}"
)
csrf_resp = c.get("/auth/csrf-token")
if csrf_resp.status_code == 200:
token = csrf_resp.json().get("csrf_token")
if token:
c.headers.update({"X-CSRFToken": token})
yield c
c.post("/auth/logout", follow_redirects=False)
@pytest.fixture
def collection_id(auth_client):
create = auth_client.post(
"/library/api/collections",
json={"name": f"limits-fence-{uuid.uuid4().hex[:6]}"},
)
assert create.status_code == 200, create.text
return create.json()["collection"]["id"]
@pytest.mark.timeout(120)
def test_config_limits_matches_file_upload_validator(auth_client):
"""Sanity check: /api/config/limits is a direct read of FileUploadValidator
(established by research.py already) — establishes the baseline this
file proves the RAG upload route agrees with.
"""
resp = auth_client.get("/api/config/limits")
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["max_file_size"] == FileUploadValidator.MAX_FILE_SIZE
assert body["max_files"] == FileUploadValidator.MAX_FILES_PER_REQUEST
@pytest.mark.timeout(120)
def test_rag_upload_enforces_advertised_file_size_limit(
auth_client, collection_id
):
"""A file just over FileUploadValidator.MAX_FILE_SIZE must be rejected
by the RAG collection upload route with THAT limit (not a hardcoded,
different one) — i.e. the enforced cap tracks whatever
/api/config/limits advertises.
Small cap (2MB) so the oversized test file itself stays tiny — never
generate a hundred-MB+ body on this machine.
"""
small_cap = 2 * 1024 * 1024 # 2 MB
with patch.object(FileUploadValidator, "MAX_FILE_SIZE", small_cap):
# Confirm the advertised limit moved too (same source of truth).
limits_resp = auth_client.get("/api/config/limits")
assert limits_resp.json()["max_file_size"] == small_cap
oversized_content = b"x" * (small_cap + 1024)
resp = auth_client.post(
f"/library/api/collections/{collection_id}/upload",
files={"files": ("oversized.txt", oversized_content, "text/plain")},
data={"pdf_storage": "none"},
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body.get("success") is True, body
errors = body.get("errors", [])
assert len(errors) == 1, (
f"expected exactly one oversized-file error, got: {body}"
)
# The enforced cap must be reported as the SAME value that was just
# advertised via /api/config/limits (2MB), not the old hardcoded 100MB.
assert errors[0]["error"] == "File too large (max 2MB)", (
f"enforced limit does not match the advertised limit: {errors[0]!r} "
"-- rag.py appears to be using a hardcoded cap again instead of "
"FileUploadValidator.MAX_FILE_SIZE"
)
assert body.get("summary", {}).get("failed") == 1, body
@pytest.mark.timeout(120)
def test_rag_upload_accepts_file_within_advertised_limit(
auth_client, collection_id
):
"""Companion to the oversized case: a file UNDER the (patched, small)
advertised limit must still succeed — proves the fix didn't just make
everything fail.
"""
small_cap = 2 * 1024 * 1024 # 2 MB
with patch.object(FileUploadValidator, "MAX_FILE_SIZE", small_cap):
small_content = b"well under the cap\n"
resp = auth_client.post(
f"/library/api/collections/{collection_id}/upload",
files={"files": ("fits.txt", small_content, "text/plain")},
data={"pdf_storage": "none"},
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body.get("success") is True, body
assert body.get("summary", {}).get("successful") == 1, body
assert body.get("summary", {}).get("failed") == 0, body
@pytest.mark.timeout(120)
def test_rag_upload_enforces_advertised_file_count_limit(
auth_client, collection_id
):
"""Same source-of-truth check for the file-COUNT cap: rag.py used to
hardcode 50; it must now track FileUploadValidator.MAX_FILES_PER_REQUEST
(also advertised via /api/config/limits), whatever that value is.
"""
small_count_cap = 2
with patch.object(
FileUploadValidator, "MAX_FILES_PER_REQUEST", small_count_cap
):
limits_resp = auth_client.get("/api/config/limits")
assert limits_resp.json()["max_files"] == small_count_cap
files = [
(f"f{i}.txt", b"tiny", "text/plain")
for i in range(small_count_cap + 1)
]
resp = auth_client.post(
f"/library/api/collections/{collection_id}/upload",
files=[("files", f) for f in files],
data={"pdf_storage": "none"},
)
assert resp.status_code == 400, resp.text
body = resp.json()
assert body.get("success") is False, body
assert body.get("error") == "Too many files. Max 2 per upload.", (
f"enforced count limit does not match the advertised one: {body!r} "
"-- rag.py appears to be using a hardcoded file-count cap again"
)