fix: sanitize FTS5 queries and close mirror DB connections#565
Merged
teknium1 merged 1 commit intoNousResearch:mainfrom Mar 10, 2026
Merged
Conversation
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.
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two bugs fixed:
search_messages()raisessqlite3.OperationalErrorwhen user queries contain FTS5 special characters (C++,"unterminated,(problem,hello AND,***,{test}). Added_sanitize_fts5_query()to strip dangerous operators, plus a fallback try-except for edge cases._append_to_sqlite()ingateway/mirror.pycreates a newSessionDB()per call but never closes it, leaking SQLite connections over time. Addedfinallyblock to ensuredb.close()is always called.Reproduction
FTS5 crash (before fix):
Test plan
test_search_special_chars_do_not_crash— 8 dangerous queries that previously crashed now return liststest_search_sanitized_query_still_finds_content— sanitized queries still find matching contenttest_sanitize_fts5_query_strips_dangerous_chars— unit test for the sanitizertest_connection_is_closed_after_use— verifiesdb.close()is called after mirror writetest_connection_closed_even_on_error— verifiesdb.close()is called even when append failstests/test_hermes_state.py+tests/gateway/test_mirror.py)