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(security): validate file size and count in RAG upload endpoint (#3938)

LearningCircuit89613f9f4016
diff --git a/changelog.d/+rag-upload-validator.security.md b/changelog.d/+rag-upload-validator.security.md
new file mode 100644
index 0000000000..aa1ee14828
--- /dev/null
+++ b/changelog.d/+rag-upload-validator.security.md
@@ -0,0 +1 @@
+RAG collection upload endpoint now validates per-file size (50MB limit) and per-request file count (200 limit), matching the research upload endpoint. Previously only the request-level `MAX_CONTENT_LENGTH` (10GB) was enforced, allowing a single oversized file or a request with thousands of zero-byte files to reach the per-file processing loop. Pre-flight `Content-Length` check rejects oversized files before reading bytes into memory.
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index 9a5ebeb3f0..4ff6f0f569 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -39,6 +39,7 @@ from ...web.utils.request_helpers import parse_bool_arg
from ...utilities.db_utils import get_settings_manager
from ..services.library_rag_service import LibraryRAGService
from ...settings.manager import SettingsManager
+from ...security.file_upload_validator import FileUploadValidator
from ...security.path_validator import PathValidator
from ...security.rate_limiter import (
upload_rate_limit_ip,
@@ -1340,6 +1341,16 @@ def upload_to_collection(collection_id):
{"success": False, "error": "No files selected"}
), 400
+ # Bound the per-request file count BEFORE doing any work. The
+ # request-level MAX_CONTENT_LENGTH gate covers total bytes, but
+ # not file *count*; a request with 10000 zero-byte files would
+ # otherwise reach the loop below.
+ is_valid, error_msg = FileUploadValidator.validate_file_count(
+ len(files)
+ )
+ if not is_valid:
+ return jsonify({"success": False, "error": error_msg}), 400
+
username = session["username"]
with get_user_db_session(username) as db_session:
# Verify collection exists in this user's database
@@ -1401,10 +1412,39 @@ def upload_to_collection(collection_id):
continue
try:
+ # Pre-flight size check on Content-Length BEFORE reading
+ # bytes into memory. Cheap rejection for oversized files;
+ # avoids loading 50MB+ into memory just to discard it.
+ is_valid, error_msg = (
+ FileUploadValidator.validate_file_size(
+ content_length=file.content_length,
+ file_content=None,
+ )
+ )
+ if not is_valid:
+ errors.append(
+ {"filename": filename, "error": error_msg}
+ )
+ continue
+
# Read file content
file_content = file.read()
file.seek(0) # Reset for potential re-reading
+ # Post-read size check (Content-Length can be missing or
+ # spoofed; the actual byte count is authoritative).
+ is_valid, error_msg = (
+ FileUploadValidator.validate_file_size(
+ content_length=None,
+ file_content=file_content,
+ )
+ )
+ if not is_valid:
+ errors.append(
+ {"filename": filename, "error": error_msg}
+ )
+ continue
+
# Calculate file hash for deduplication
file_hash = hashlib.sha256(file_content).hexdigest()
diff --git a/tests/research_library/routes/test_rag_routes_upload_coverage.py b/tests/research_library/routes/test_rag_routes_upload_coverage.py
index 583a973673..0e9ed69017 100644
--- a/tests/research_library/routes/test_rag_routes_upload_coverage.py
+++ b/tests/research_library/routes/test_rag_routes_upload_coverage.py
@@ -207,6 +207,75 @@ class TestUploadToCollection:
assert data["success"] is False
assert "No files provided" in data["error"]
+ def test_upload_rejects_too_many_files(self, app):
+ """File count over MAX_FILES_PER_REQUEST is rejected with 400."""
+ from local_deep_research.security.file_upload_validator import (
+ FileUploadValidator,
+ )
+
+ # Patch the limit low so the test doesn't have to ship 201 files.
+ with patch.object(FileUploadValidator, "MAX_FILES_PER_REQUEST", 3):
+ with _auth_client(app) as (client, ctx):
+ # Send 4 files (one over the patched limit).
+ resp = client.post(
+ "/library/api/collections/coll-1/upload",
+ data={
+ "files": [
+ (BytesIO(b"a"), f"f{i}.txt") for i in range(4)
+ ]
+ },
+ content_type="multipart/form-data",
+ )
+ assert resp.status_code == 400
+ data = resp.get_json()
+ assert data["success"] is False
+ assert "Too many files" in data["error"]
+
+ def test_upload_rejects_oversized_file(self, app):
+ """Per-file size over MAX_FILE_SIZE is rejected; other files still process."""
+ from local_deep_research.security.file_upload_validator import (
+ FileUploadValidator,
+ )
+
+ mock_coll = Mock()
+ mock_coll.id = "coll-1"
+
+ db_session = _make_db_session()
+ q = _build_mock_query(first_result=mock_coll)
+ db_session.query = Mock(return_value=q)
+
+ mock_password_store = Mock()
+ mock_password_store.get_session_password.return_value = None
+
+ # 100-byte limit; send a 200-byte file → rejected.
+ with patch.object(FileUploadValidator, "MAX_FILE_SIZE", 100):
+ with _auth_client(
+ app,
+ mock_db_session=db_session,
+ extra_patches=[
+ patch(
+ f"{_DB_PASS}.session_password_store",
+ mock_password_store,
+ ),
+ ],
+ ) as (client, ctx):
+ resp = client.post(
+ "/library/api/collections/coll-1/upload",
+ data={
+ "files": (BytesIO(b"x" * 200), "big.txt"),
+ },
+ content_type="multipart/form-data",
+ )
+
+ assert resp.status_code == 200
+ data = resp.get_json()
+ # Oversized file appears in the per-file errors list, NOT 400 — so a
+ # batch with one oversized + others can still succeed for the rest.
+ assert data["success"] is True
+ assert any(
+ "File too large" in e.get("error", "") for e in data["errors"]
+ )
+
def test_upload_empty_files_list(self, app):
"""File with empty filename is silently skipped; response is success with 0 uploaded."""
mock_coll = Mock()