~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
fix(security): reject oversized PDF upload parts before reading them (GHSA-38fh) (#6884)
LearningCircuitb643579f0fdc
diff --git a/changelog.d/6884.security.md b/changelog.d/6884.security.mdnew file mode 100644index 0000000000..ef3df718be--- /dev/null+++ b/changelog.d/6884.security.md@@ -0,0 +1 @@+Reject oversized PDF upload parts before reading them into RAM (GHSA-38fh). The per-file size guard now checks the part's spooled size before file.read(). In Starlette 1.3.x, file.size is the byte count request.form() actually wrote to the part's disk-backed temporary file, not a client-declared value. request.form() still spools the whole part to disk, but an oversized part is no longer read into RAM before it is rejected. A part under the per-file cap is still read fully into RAM, as before.diff --git a/src/local_deep_research/web/routers/rag.py b/src/local_deep_research/web/routers/rag.pyindex cc7d950844..8313b23034 100644--- a/src/local_deep_research/web/routers/rag.py+++ b/src/local_deep_research/web/routers/rag.py@@ -2442,8 +2442,10 @@ async def upload_to_collection({"filename": "", "content": None, "oversized": False})continue- # Per-file size guard before reading into memory (UploadFile.size- # is set when the client sent Content-Length per part).+ # Per-file size guard before reading into memory. On the pinned+ # Starlette range (1.3.x), UploadFile.size is the byte count+ # request.form() actually spooled for this part, not a+ # client-declared value, so it is never None here.if uf.size is not None and uf.size > MAX_FILE_SIZE:files_data.append({diff --git a/src/local_deep_research/web/routers/research.py b/src/local_deep_research/web/routers/research.pyindex d0ba18cd32..406f4092b0 100644--- a/src/local_deep_research/web/routers/research.py+++ b/src/local_deep_research/web/routers/research.py@@ -3564,6 +3564,32 @@ async def upload_pdf(continuetry:+ # Per-file size guard BEFORE reading the spool into+ # RAM (GHSA-38fh finding 2): the request-level+ # precheck keys off Content-Length, which chunked+ # multipart bodies do not carry, so waiting for the+ # post-read validator would read the whole part into+ # RAM before rejecting it. Mirrors rag.py's pre-read+ # guard. On the pinned Starlette range (1.3.x),+ # file.size is the byte count request.form() actually+ # wrote to the part's disk-backed SpooledTemporaryFile+ # (it starts at 0 and grows per chunk), not a+ # client-declared value, so it is never None here.+ # The post-read validator's size check below is+ # therefore only defence in depth for a size of None,+ # which this Starlette range never produces.+ # request.form() has already spooled the whole part+ # to disk by this point; the guard prevents only the+ # read into RAM. A part under the cap is still read+ # fully into RAM, by the cap's design (unchanged by+ # this guard).+ size_ok, size_err = FileUploadValidator.validate_file_size(+ file.size, None+ )+ if not size_ok:+ errors.append(f"{filename}: {size_err}")+ continue+# Read file content (UploadFile spools large bodies to disk)pdf_content = await file.read()diff --git a/tests/pdf_tests/test_pdf_upload.py b/tests/pdf_tests/test_pdf_upload.pyindex 1f4a84a905..6810fd4030 100644--- a/tests/pdf_tests/test_pdf_upload.py+++ b/tests/pdf_tests/test_pdf_upload.py@@ -114,6 +114,58 @@ class TestPDFUpload:else:assert response.status_code == 400, response.status_code+ def test_oversized_file_is_rejected_before_its_bytes_are_read(+ self, authenticated_client, monkeypatch+ ):+ """GHSA-38fh finding 2: the size guard must fire BEFORE the read.++ The request-level precheck keys off ``Content-Length``, which+ chunked multipart bodies do not carry, and the validator saw the+ bytes only after ``await file.read()`` had pulled the whole+ spooled part into RAM — the buffered-then-rejected spike the+ advisory demonstrates (a multi-GB allocation at the default cap+ before the rejection goes out). Mirrors rag.py's per-file+ pre-read guard: a part whose spooled size (``file.size``, the+ byte count Starlette 1.3.x measured while spooling, not a+ client-declared value) exceeds the cap is refused without a+ single read call. The post-read validator's size check is+ defence in depth for a size of None, which the pinned Starlette+ range never produces.+ """+ import starlette.datastructures as starlette_ds+ from local_deep_research.security import FileUploadValidator++ # Patch the cap small (the advisory's PoC shape): the default+ # multi-GB ceiling would need a multi-GB body to exercise.+ monkeypatch.setattr(FileUploadValidator, "MAX_FILE_SIZE", 1024 * 1024)++ read_files = []++ async def _recording_read(self, size=-1):+ read_files.append(self.filename)+ return b""++ monkeypatch.setattr(starlette_ds.UploadFile, "read", _recording_read)++ big = b"%PDF-1.4\n" + (b"x" * (8 * 1024 * 1024)) # 8MB > 1MB cap+ response = authenticated_client.post(+ "/api/upload/pdf",+ data={"files": (io.BytesIO(big), "big.pdf")},+ content_type="multipart/form-data",+ )++ body = response.get_json()+ assert response.status_code == 400 or body.get("errors"), (+ f"unexpected response: {response.status_code} {body}"+ )+ assert any("File too large" in err for err in body.get("errors", [])), (+ f"no size rejection in errors: {body.get('errors')}"+ )+ assert read_files == [], (+ "file.read() was called for an oversized part — the size "+ "guard must reject before reading (GHSA-38fh)"+ )+def test_upload_too_many_files_rejected(self, authenticated_client):"""Test that more than MAX_FILES_PER_REQUEST files are rejected."""from werkzeug.datastructures import MultiDict # noqa: E402diff --git a/tests/web/routers/test_research_mutating_route_effects.py b/tests/web/routers/test_research_mutating_route_effects.pyindex 51c3fc895b..372315691d 100644--- a/tests/web/routers/test_research_mutating_route_effects.py+++ b/tests/web/routers/test_research_mutating_route_effects.py@@ -497,6 +497,10 @@ def test_upload_pdf_returns_the_extracted_text(authenticated_client):validator.MAX_FILE_SIZE = 50 * 1024 * 1024validator.MAX_FILES_PER_REQUEST = 100validator.validate_file_count.return_value = (True, None)+ # The route now consults the per-part size guard (validate_file_size)+ # before reading each spooled part; without this stub the mock returns+ # a non-iterable MagicMock and the guard's tuple unpack raises.+ validator.validate_file_size.return_value = (True, None)validator.validate_upload.return_value = (True, None)with (@@ -572,6 +576,11 @@ def test_upload_pdf_reports_a_failed_extraction_in_errors(validator.MAX_FILE_SIZE = 50 * 1024 * 1024validator.MAX_FILES_PER_REQUEST = 100validator.validate_file_count.return_value = (True, None)+ # Premise updated for the pre-read size guard (GHSA-38fh finding 2):+ # the route calls validate_file_size before file.read(); without this+ # stub the mock's return is not unpackable and the part errors out+ # before the extraction service is reached.+ validator.validate_file_size.return_value = (True, None)validator.validate_upload.return_value = (True, None)with (