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: apply security verification to research_library file writes

LearningCircuitf987f293d3b3
diff --git a/.github/scripts/check-file-writes.sh b/.github/scripts/check-file-writes.sh
index 85a4e97872..a7c96d74f6 100755
--- a/.github/scripts/check-file-writes.sh
+++ b/.github/scripts/check-file-writes.sh
@@ -59,7 +59,6 @@ EXCLUDE_DIRS=(
"scripts"
".github"
"cookiecutter-docker"
- "research_library"
)
# Files to exclude
diff --git a/src/local_deep_research/research_library/downloaders/base.py b/src/local_deep_research/research_library/downloaders/base.py
index 216e6bf0d6..80b6511dc7 100644
--- a/src/local_deep_research/research_library/downloaders/base.py
+++ b/src/local_deep_research/research_library/downloaders/base.py
@@ -315,8 +315,7 @@ class BaseDownloader(ABC):
def _extract_text_from_pdf(self, pdf_content: bytes) -> Optional[str]:
"""
- Extract text from PDF content using Langchain's document loader.
- This ensures consistency with local search functionality.
+ Extract text from PDF content using in-memory processing.
Args:
pdf_content: PDF file content as bytes
@@ -326,58 +325,24 @@ class BaseDownloader(ABC):
"""
try:
import io
- import tempfile
- from pathlib import Path
+ # Use pypdf for in-memory PDF text extraction (no disk writes)
try:
- # Use Langchain's UnstructuredFileLoader for consistency with local search
- from langchain_community.document_loaders import (
- UnstructuredFileLoader,
- )
+ from pypdf import PdfReader
+ except ImportError:
+ from PyPDF2 import PdfReader
- # Create a temporary file to load from
- with tempfile.NamedTemporaryFile(
- suffix=".pdf", delete=False
- ) as tmp_file:
- tmp_file.write(pdf_content)
- tmp_path = tmp_file.name
-
- try:
- # Load and extract text using UnstructuredFileLoader
- loader = UnstructuredFileLoader(tmp_path)
- documents = loader.load()
-
- # Combine all content from the documents
- full_text = "\n\n".join(
- [doc.page_content for doc in documents]
- )
- return full_text if full_text.strip() else None
+ pdf_file = io.BytesIO(pdf_content)
+ pdf_reader = PdfReader(pdf_file)
- finally:
- # Clean up temporary file
- Path(tmp_path).unlink(missing_ok=True)
+ text_content = []
+ for page in pdf_reader.pages:
+ text = page.extract_text()
+ if text:
+ text_content.append(text)
- except ImportError:
- # Fallback to pypdf if Langchain is not available
- logger.warning(
- "langchain_community not available, falling back to pypdf"
- )
- try:
- from pypdf import PdfReader
- except ImportError:
- from PyPDF2 import PdfReader
-
- pdf_file = io.BytesIO(pdf_content)
- pdf_reader = PdfReader(pdf_file)
-
- text_content = []
- for page in pdf_reader.pages:
- text = page.extract_text()
- if text:
- text_content.append(text)
-
- full_text = "\n".join(text_content)
- return full_text if full_text.strip() else None
+ full_text = "\n".join(text_content)
+ return full_text if full_text.strip() else None
except Exception:
logger.exception("Failed to extract text from PDF")
diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.py
index c9fdc1bc04..dde8c3db14 100644
--- a/src/local_deep_research/research_library/services/download_service.py
+++ b/src/local_deep_research/research_library/services/download_service.py
@@ -553,9 +553,17 @@ class DownloadService:
if not validated_path:
raise ValueError("Invalid file path")
- # Write the PDF file
- with open(validated_path, "wb") as f:
- f.write(content)
+ # Write the PDF file with security verification
+ from ...security.file_write_verifier import write_file_verified
+
+ write_file_verified(
+ validated_path,
+ content,
+ "research_library.enable_pdf_storage",
+ True,
+ "library PDF storage",
+ mode="wb",
+ )
return str(validated_path)
@@ -614,17 +622,6 @@ class DownloadService:
Returns:
Absolute path to saved text file or None if storage disabled or save fails
"""
- # Check if text file storage is enabled
- enable_txt_storage = self.settings.get_setting(
- "research_library.enable_txt_storage", False
- )
-
- if not enable_txt_storage:
- logger.info(
- "Text file storage disabled - text will only be stored in database"
- )
- return None
-
try:
# Generate filename similar to PDF but with .txt extension
domain = urlparse(resource.url).netloc.replace(".", "_")
@@ -668,15 +665,24 @@ class DownloadService:
if not validated_path:
raise ValueError("Invalid file path")
- # Write the text file with UTF-8 encoding
- with open(validated_path, "w", encoding="utf-8") as f:
- # Add metadata header
- f.write(f"# Source: {resource.url}\n")
- f.write(f"# Title: {resource.title}\n")
- f.write(f"# Extracted: {datetime.now(UTC).isoformat()}\n")
- f.write(f"# Resource ID: {resource.id}\n")
- f.write("-" * 80 + "\n\n")
- f.write(text)
+ # Build content with metadata header
+ from ...security.file_write_verifier import write_file_verified
+
+ text_content = f"# Source: {resource.url}\n"
+ text_content += f"# Title: {resource.title}\n"
+ text_content += f"# Extracted: {datetime.now(UTC).isoformat()}\n"
+ text_content += f"# Resource ID: {resource.id}\n"
+ text_content += "-" * 80 + "\n\n"
+ text_content += text
+
+ # Write with security verification
+ write_file_verified(
+ validated_path,
+ text_content,
+ "research_library.enable_txt_storage",
+ True,
+ "library text extraction",
+ )
logger.info(f"Saved text extraction to: {validated_path}")
return str(validated_path)