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: escape SQL LIKE wildcards in domain and search filters (#3427)

LearningCircuit7c7edecf95c3
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index b597a91e4b..fbe2501d60 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -103,23 +103,36 @@ class LibraryService:
except Exception:
return False
+ @staticmethod
+ def _escape_like(value: str) -> str:
+ """Escape SQL LIKE wildcards (%, _) and the escape char itself.
+
+ Without this, a value like ``my_journal`` would have ``_`` interpreted
+ as a single-character wildcard, and ``%`` would match anything.
+ Used together with ``escape="\\\\"`` on the .like()/.ilike() call.
+ """
+ return (
+ value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
+ )
+
def _apply_domain_filter(self, query, model_class, domain: str):
"""Apply domain filter to query for Document.
The dropdown is fully data-driven (populated from get_unique_domains),
so the filter is a generic substring match against original_url.
"""
- return query.filter(model_class.original_url.like(f"%{domain}%"))
+ pattern = f"%{self._escape_like(domain)}%"
+ return query.filter(model_class.original_url.like(pattern, escape="\\"))
def _apply_search_filter(self, query, model_class, search_query: str):
"""Apply search filter to query for Document."""
- search_pattern = f"%{search_query}%"
+ search_pattern = f"%{self._escape_like(search_query)}%"
return query.filter(
or_(
- model_class.title.ilike(search_pattern),
- model_class.authors.ilike(search_pattern),
- model_class.doi.ilike(search_pattern),
- ResearchResource.title.ilike(search_pattern),
+ model_class.title.ilike(search_pattern, escape="\\"),
+ model_class.authors.ilike(search_pattern, escape="\\"),
+ model_class.doi.ilike(search_pattern, escape="\\"),
+ ResearchResource.title.ilike(search_pattern, escape="\\"),
)
)
diff --git a/tests/research_library/services/test_library_service_domain_filters.py b/tests/research_library/services/test_library_service_domain_filters.py
index 73990afe79..a85f1addeb 100644
--- a/tests/research_library/services/test_library_service_domain_filters.py
+++ b/tests/research_library/services/test_library_service_domain_filters.py
@@ -81,7 +81,7 @@ class TestApplyDomainFilter:
"""Tests for _apply_domain_filter generic substring match."""
def test_filter_uses_like_with_domain_substring(self):
- """Any domain value is wrapped as %domain% and passed to .like()."""
+ """A plain domain is wrapped as %domain% with escape='\\\\'."""
service = _make_service()
mock_query = MagicMock()
mock_model = MagicMock()
@@ -90,20 +90,37 @@ class TestApplyDomainFilter:
mock_query, mock_model, "nature.com"
)
- mock_model.original_url.like.assert_called_once_with("%nature.com%")
+ mock_model.original_url.like.assert_called_once_with(
+ "%nature.com%", escape="\\"
+ )
mock_query.filter.assert_called_once()
assert result == mock_query.filter.return_value
- def test_empty_domain_still_passed_through(self):
- """Empty string domain is wrapped as %% (matches everything)."""
+ def test_filter_escapes_underscore_wildcard(self):
+ """A domain with '_' has it escaped so it's not a SQL wildcard."""
service = _make_service()
mock_query = MagicMock()
mock_model = MagicMock()
- result = service._apply_domain_filter(mock_query, mock_model, "")
+ service._apply_domain_filter(
+ mock_query, mock_model, "my_journal.example.com"
+ )
- mock_model.original_url.like.assert_called_once_with("%%")
- assert result == mock_query.filter.return_value
+ mock_model.original_url.like.assert_called_once_with(
+ "%my\\_journal.example.com%", escape="\\"
+ )
+
+ def test_filter_escapes_percent_wildcard(self):
+ """A domain with '%' has it escaped so it's not a SQL wildcard."""
+ service = _make_service()
+ mock_query = MagicMock()
+ mock_model = MagicMock()
+
+ service._apply_domain_filter(mock_query, mock_model, "100%site.com")
+
+ mock_model.original_url.like.assert_called_once_with(
+ "%100\\%site.com%", escape="\\"
+ )
# ============== _apply_search_filter ==============
@@ -113,7 +130,7 @@ class TestApplySearchFilter:
"""Tests for _apply_search_filter method."""
def test_search_filter_wraps_query_in_wildcards(self, mocker):
- """Search query is wrapped as %search_query%."""
+ """Search query is wrapped as %search_query% with escape='\\\\'."""
service = _make_service()
mock_query = MagicMock()
mock_model = MagicMock()
@@ -132,12 +149,20 @@ class TestApplySearchFilter:
mock_query, mock_model, "quantum computing"
)
- # Verify all 3 model fields called with correct pattern
+ # Verify all 4 fields called with correct pattern + escape kwarg
expected_pattern = "%quantum computing%"
- mock_model.title.ilike.assert_called_once_with(expected_pattern)
- mock_model.authors.ilike.assert_called_once_with(expected_pattern)
- mock_model.doi.ilike.assert_called_once_with(expected_pattern)
- mock_resource.title.ilike.assert_called_once_with(expected_pattern)
+ mock_model.title.ilike.assert_called_once_with(
+ expected_pattern, escape="\\"
+ )
+ mock_model.authors.ilike.assert_called_once_with(
+ expected_pattern, escape="\\"
+ )
+ mock_model.doi.ilike.assert_called_once_with(
+ expected_pattern, escape="\\"
+ )
+ mock_resource.title.ilike.assert_called_once_with(
+ expected_pattern, escape="\\"
+ )
def test_search_filter_uses_four_ilike_conditions(self, mocker):
"""Search filter creates or_() with 4 ilike conditions."""
@@ -162,8 +187,8 @@ class TestApplySearchFilter:
args = mock_or.call_args[0]
assert len(args) == 4
- def test_search_filter_special_chars_not_escaped(self, mocker):
- """Documents behavior: SQL special chars in query are not escaped."""
+ def test_search_filter_escapes_sql_wildcards(self, mocker):
+ """SQL special chars (% and _) in query are escaped, not wildcards."""
service = _make_service()
mock_query = MagicMock()
mock_model = MagicMock()
@@ -177,10 +202,12 @@ class TestApplySearchFilter:
MagicMock(),
)
- service._apply_search_filter(mock_query, mock_model, "%test%")
+ service._apply_search_filter(mock_query, mock_model, "100%_test")
- # Pattern wraps as-is: %%test%%
- mock_model.title.ilike.assert_called_once_with("%%test%%")
+ # Both '%' and '_' get backslash-escaped before being wrapped
+ mock_model.title.ilike.assert_called_once_with(
+ "%100\\%\\_test%", escape="\\"
+ )
def test_search_filter_returns_filtered_query(self, mocker):
"""Return value is the result of query.filter()."""