Skip to content

FTS5 query crashes on unescaped metacharacters (apostrophe, quotes) #31

Description

@coreyt

Problem

When a user query contains FTS5 metacharacters (e.g. apostrophe ', double quotes "), text_search() passes the raw string to FTS5, producing:

_fathomdb.SqliteError: fts5: syntax error near "'"

This was hit in production when Memex's retrieval layer searched for User's name — the apostrophe broke the FTS5 query parser.

Reproduction

engine = Engine.open("test.db")
# ... register a collection with text_search support ...
q = engine.nodes("some_kind").text_search("User's name", limit=10)
q.execute()  # -> SqliteError: fts5: syntax error near "'"

Expected behavior

text_search() should escape FTS5 metacharacters in the query string before passing it to the FTS5 MATCH expression. At minimum, double any embedded double-quotes and wrap each token in double quotes, e.g. "User's" "name".

Context

  • Discovered via Memex service logs (fathom_store.py:2544 → _query.py:254)
  • The calling code has no way to pre-escape since the query goes through the AST builder
  • Affects any text_search() call where the query originates from user input

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions