fix: sanitize FTS5 queries and close mirror DB connections
Two bugs fixed:
1. search_messages() crashes with OperationalError when user queries
contain FTS5 special characters (+, ", (, {, dangling AND/OR, etc).
Added _sanitize_fts5_query() to strip dangerous operators and a
fallback try-except for edge cases.
2. _append_to_sqlite() in mirror.py creates a new SessionDB per call
but never closes it, leaking SQLite connections. Added finally block
to ensure db.close() is always called.
This commit is contained in:
parent
b98301677a
commit
33cfe1515d
4 changed files with 112 additions and 1 deletions
|
|
@ -160,3 +160,27 @@ class TestMirrorToSession:
|
|||
result = mirror_to_session("telegram", "123", "msg")
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestAppendToSqlite:
|
||||
def test_connection_is_closed_after_use(self, tmp_path):
|
||||
"""Verify _append_to_sqlite closes the SessionDB connection."""
|
||||
from gateway.mirror import _append_to_sqlite
|
||||
mock_db = MagicMock()
|
||||
|
||||
with patch("hermes_state.SessionDB", return_value=mock_db):
|
||||
_append_to_sqlite("sess_1", {"role": "assistant", "content": "hello"})
|
||||
|
||||
mock_db.append_message.assert_called_once()
|
||||
mock_db.close.assert_called_once()
|
||||
|
||||
def test_connection_closed_even_on_error(self, tmp_path):
|
||||
"""Verify connection is closed even when append_message raises."""
|
||||
from gateway.mirror import _append_to_sqlite
|
||||
mock_db = MagicMock()
|
||||
mock_db.append_message.side_effect = Exception("db error")
|
||||
|
||||
with patch("hermes_state.SessionDB", return_value=mock_db):
|
||||
_append_to_sqlite("sess_1", {"role": "assistant", "content": "hello"})
|
||||
|
||||
mock_db.close.assert_called_once()
|
||||
|
|
|
|||
|
|
@ -179,6 +179,54 @@ class TestFTS5Search:
|
|||
assert isinstance(results[0]["context"], list)
|
||||
assert len(results[0]["context"]) > 0
|
||||
|
||||
def test_search_special_chars_do_not_crash(self, db):
|
||||
"""FTS5 special characters in queries must not raise OperationalError."""
|
||||
db.create_session(session_id="s1", source="cli")
|
||||
db.append_message("s1", role="user", content="How do I use C++ templates?")
|
||||
|
||||
# Each of these previously caused sqlite3.OperationalError
|
||||
dangerous_queries = [
|
||||
'C++', # + is FTS5 column filter
|
||||
'"unterminated', # unbalanced double-quote
|
||||
'(problem', # unbalanced parenthesis
|
||||
'hello AND', # dangling boolean operator
|
||||
'***', # repeated wildcard
|
||||
'{test}', # curly braces (column reference)
|
||||
'OR hello', # leading boolean operator
|
||||
'a AND OR b', # adjacent operators
|
||||
]
|
||||
for query in dangerous_queries:
|
||||
# Must not raise — should return list (possibly empty)
|
||||
results = db.search_messages(query)
|
||||
assert isinstance(results, list), f"Query {query!r} did not return a list"
|
||||
|
||||
def test_search_sanitized_query_still_finds_content(self, db):
|
||||
"""Sanitization must not break normal keyword search."""
|
||||
db.create_session(session_id="s1", source="cli")
|
||||
db.append_message("s1", role="user", content="Learning C++ templates today")
|
||||
|
||||
# "C++" sanitized to "C" should still match "C++"
|
||||
results = db.search_messages("C++")
|
||||
# The word "C" appears in the content, so FTS5 should find it
|
||||
assert isinstance(results, list)
|
||||
|
||||
def test_sanitize_fts5_query_strips_dangerous_chars(self):
|
||||
"""Unit test for _sanitize_fts5_query static method."""
|
||||
from hermes_state import SessionDB
|
||||
s = SessionDB._sanitize_fts5_query
|
||||
assert s('hello world') == 'hello world'
|
||||
assert '+' not in s('C++')
|
||||
assert '"' not in s('"unterminated')
|
||||
assert '(' not in s('(problem')
|
||||
assert '{' not in s('{test}')
|
||||
# Dangling operators removed
|
||||
assert s('hello AND') == 'hello'
|
||||
assert s('OR world') == 'world'
|
||||
# Leading bare * removed
|
||||
assert s('***') == ''
|
||||
# Valid prefix kept
|
||||
assert s('deploy*') == 'deploy*'
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# Session search and listing
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue