~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(library): stop returning the server's absolute path for a document (#6502)
Lukasd6a80587f965
diff --git a/changelog.d/6462.security.md b/changelog.d/6462.security.mdnew file mode 100644index 0000000000..f3fc5127d9--- /dev/null+++ b/changelog.d/6462.security.md@@ -0,0 +1,11 @@+The library document API and the document details page no longer return the+absolute server-side path of a stored file. `GET /library/api/documents` and+the document details payload both carried `file_absolute_path`, the resolved+location of the file on the server, which describes the host's directory layout+rather than the document (#6462).++`file_path`, the path relative to the library root, still identifies where a+document sits inside the reader's own library, and the absolute path is still+resolved internally to decide whether the file is available. This matches what+`/library/api/check-downloads` already does for the same value, and what #6443+does for the research status, details and report endpoints.diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.pyindex 862da4bd1b..f1b20eefaf 100644--- a/src/local_deep_research/research_library/services/library_service.py+++ b/src/local_deep_research/research_library/services/library_service.py@@ -444,7 +444,11 @@ class LibraryService:# Prefer the resource matched via Document.resource_id FK;# fall back to the one matched via ResearchResource.document_id.resource = res_by_fk or res_by_doc- # Determine availability flags - use Document.file_path directly+ # Determine availability flags - use Document.file_path directly.+ # Resolved here and NOT returned: the absolute path is the server's+ # own directory layout, which /library/api/check-downloads already+ # withholds for the same reason (#6462). Callers fetch the file+ # through /document/{id}/pdf, so they never need it.file_absolute_path = Noneif (doc.file_path@@ -491,7 +495,6 @@ class LibraryService:"pmid": doc.pmid,# File info"file_path": doc.file_path,- "file_absolute_path": file_absolute_path,"file_name": Path(doc.file_path).nameif doc.file_pathand doc.file_path not in FILE_PATH_SENTINELS@@ -986,9 +989,6 @@ class LibraryService:"original_url": doc.original_urlor (resource.url if resource else None),"file_path": doc.file_path,- "file_absolute_path": self._get_safe_absolute_path(- doc.file_path- ),"file_name": Path(doc.file_path).nameif doc.file_pathand doc.file_path not in FILE_PATH_SENTINELSdiff --git a/src/local_deep_research/web/templates/pages/document_details.html b/src/local_deep_research/web/templates/pages/document_details.htmlindex a08a868cf6..324caa0fed 100644--- a/src/local_deep_research/web/templates/pages/document_details.html+++ b/src/local_deep_research/web/templates/pages/document_details.html@@ -241,20 +241,11 @@<h2><i aria-hidden="true" class="fas fa-folder-open"></i> File Location</h2><div class="ldr-metadata-grid"><div class="ldr-metadata-item">- <div class="ldr-metadata-label">File Path</div>+ <div class="ldr-metadata-label">File Path (relative to the library root)</div><div class="ldr-metadata-value"><code>{{ document.file_path }}</code></div></div>-- {% if document.file_absolute_path %}- <div class="ldr-metadata-item">- <div class="ldr-metadata-label">Absolute Path</div>- <div class="ldr-metadata-value">- <code>{{ document.file_absolute_path }}</code>- </div>- </div>- {% endif %}</div></div>{% endif %}diff --git a/tests/security/test_library_document_path_disclosure.py b/tests/security/test_library_document_path_disclosure.pynew file mode 100644index 0000000000..5362c31bda--- /dev/null+++ b/tests/security/test_library_document_path_disclosure.py@@ -0,0 +1,198 @@+"""The library document API must not disclose the server's own file paths.++``LibraryService.get_documents`` (the list behind ``GET /library/api/documents``)+and ``LibraryService.get_document_by_id`` (the document details page) both+returned ``file_absolute_path``: the resolved absolute path of the stored file,+which is the server's directory layout rather than anything about the document.++The codebase already treats exactly this as a bug on a neighbouring endpoint.+``/library/api/check-downloads`` withholds it in so many words -- "it's the+absolute server path, which leaks directory layout to any authenticated user.+The client fetches via ``/document/{id}/pdf`` ... so the path is never needed+client-side" -- and #6443 removes the same class of value from the research+status, details and report endpoints (#6462).++``file_path`` stays. It is the path *relative to the library root*, which is+what tells a reader where a document sits inside their own library, and the+resolution still happens internally so the availability flag is unchanged; only+the resolved value stops being returned.+"""++import re+from contextlib import contextmanager+from datetime import datetime, timezone+from pathlib import Path+from unittest.mock import patch++import pytest+from sqlalchemy import create_engine+from sqlalchemy.orm import sessionmaker+from sqlalchemy.pool import StaticPool++_SERVICE = "local_deep_research.research_library.services.library_service"++# A path no test data can produce by accident, so finding it anywhere in a+# payload means the resolved absolute path was carried out of the service.+SENTINEL_ROOT = "/srv/ldr-private-root-9d41c2"+SENTINEL_ABS = f"{SENTINEL_ROOT}/pdfs/42.pdf"+RELATIVE_PATH = "pdfs/42.pdf"+++def session():+ from local_deep_research.database.models import Base++ engine = create_engine(+ "sqlite:///:memory:",+ connect_args={"check_same_thread": False},+ poolclass=StaticPool,+ )+ Base.metadata.create_all(engine)+ db = sessionmaker(bind=engine)()+ try:+ yield db+ finally:+ db.close()+ engine.dispose()+++def _seed(db):+ """One completed document in the default collection, stored on disk."""+ from local_deep_research.database.models import (+ Collection,+ Document,+ DocumentCollection,+ )++ now = datetime.now(timezone.utc)+ collection = Collection(+ id="col-1", name="Library", created_at=now, updated_at=now+ )+ document = Document(+ id="doc-1",+ source_type_id=1,+ document_hash="hash-1",+ title="A Paper",+ filename="42.pdf",+ file_path=RELATIVE_PATH,+ file_size=1234,+ file_type="pdf",+ status="completed",+ processed_at=now,+ created_at=now,+ updated_at=now,+ )+ db.add_all(+ [+ collection,+ document,+ DocumentCollection(+ document_id="doc-1", collection_id="col-1", added_at=now+ ),+ ]+ )+ db.commit()+++@contextmanager+def _service(db):+ """A LibraryService whose DB is `db` and whose library root is the sentinel."""+ from local_deep_research.research_library.services.library_service import (+ LibraryService,+ )++ @contextmanager+ def _session(*_args, **_kwargs):+ yield db++ with (+ patch(f"{_SERVICE}.get_user_db_session", _session),+ patch(+ f"{_SERVICE}.get_absolute_path_from_settings",+ return_value=Path(SENTINEL_ABS),+ ),+ patch(f"{_SERVICE}.get_settings_manager", return_value=object()),+ patch(+ "local_deep_research.database.library_init.get_default_library_id",+ return_value="col-1",+ ),+ ):+ yield LibraryService(username="alice")+++def _documents(db):+ with _service(db) as service:+ return service.get_documents(collection_id="col-1")+++def _document(db):+ with _service(db) as service:+ return service.get_document_by_id("doc-1")+++class TestTheResolvedPathIsNotReturned:+ def test_the_document_list_carries_no_absolute_path(self, session):+ _seed(session)++ documents = _documents(session)++ assert len(documents) == 1+ assert "file_absolute_path" not in documents[0]+ # Not only the key: the value must be absent from the payload however+ # it is spelled or nested.+ assert SENTINEL_ROOT not in repr(documents)++ def test_the_details_payload_carries_no_absolute_path(self, session):+ _seed(session)++ document = _document(session)++ assert document is not None+ assert "file_absolute_path" not in document+ assert SENTINEL_ROOT not in repr(document)+++class TestWhatTheReaderStillGets:+ """Accept controls. Returning an empty payload would satisfy every+ assertion above, and so would a change that stopped resolving the path at+ all and broke the availability flag with it.+ """++ def test_the_library_relative_path_still_identifies_the_file(self, session):+ _seed(session)++ documents = _documents(session)+ document = _document(session)++ for payload in (documents[0], document):+ assert payload["file_path"] == RELATIVE_PATH+ assert payload["file_name"] == "42.pdf"++ def test_availability_still_depends_on_resolving_the_path(self, session):+ """``has_pdf`` in the list is computed FROM the resolved absolute path.+ It has to stay true, which is what separates "stopped returning it"+ from "stopped resolving it".+ """+ _seed(session)++ documents = _documents(session)++ assert documents[0]["has_pdf"] is True+++class TestTheDocumentPageDoesNotRenderItEither:+ def test_the_template_has_no_absolute_path_field(self):+ """The service is one of two readers; the details template printed the+ same value under an "Absolute Path" label.+ """+ template = (+ Path(__file__).resolve().parents[2]+ / "src/local_deep_research/web/templates/pages/document_details.html"+ )+ body = template.read_text(encoding="utf-8")++ assert "file_absolute_path" not in body+ assert not re.search(r"Absolute\s+Path", body)+ # The relative path is still shown, so this is a removal of one field+ # rather than of the File Location section.+ assert "document.file_path" in body