Skip to content

[Bug]: fulltext builders never escape terms or the AGAINST literal #31399

Description

@Ariznawlll

DRAFT 21 (SDK, 2026-10-27) — to be filed

Title: [Bug]: fulltext search terms/queries are never escaped — apostrophes break AGAINST, boolean operators in terms rewrite query semantics

Description

The fulltext builder chain never escapes user content at any of the three boundaries where it matters. All evidence from matrixone/sqlalchemy_ext/fulltext_search.py:

  1. Terms are embedded raw into the boolean query (FulltextElement.build() → f"{operator}{content}", groups built with no content guard):
    • boolean_match('body').must("O'Brien") → generated MATCH(body) AGAINST('+O'Brien' IN BOOLEAN MODE) at the final compile() — the apostrophe terminates the SQL string literal, producing broken SQL (+'O' remainder + dangling quotes), the same unescaped-string-literal family as today's other reports, but on the most common user-data character in English text (don't, O'Brien, it's).
    • Terms containing boolean-mode operators are silently repurposed: must("c++") builds +c++ (operator soup), must("stop-fire") builds +stop-fire — the -fire half becomes an exclusion inside the must clause, so a search for the literal product name "stop-fire" == excludes 'fire' and requires 'stop'. Real-world impact on product/part-number searches is direct.
    • phrase('a "quoted" thing') → "a "quoted" thing" — inner double quotes break phrase delimiting.
  2. The AGAINST string literal itself is unescaped in FulltextFilter.compile():
    return f"MATCH({columns_str}) AGAINST('{query_string}' IN BOOLEAN MODE)"
    Even withoutboolean-operator issues, any single character ' anywhere in any term breaks out of the literal — SQL syntax error or (in composed contexts) injection.
  3. Column names in MATCH({columns_str}) are raw-joined (", ".join of caller strings) — no backtick quoting; a column named key/order or one containing special chars fails.

Per MySQL boolean-mode documentation, double-quoted term wrappers are the standard escape for literal operator characters (+"c++" style), and apostrophes inside the AGAINST literal need '' doubling — none of that machinery exists here.

(Related-but-different: #31259 covered the group() OR semantics producing wrong-shaped queries; this report covers escaping/semantic-safety of user term content — the reproduction classes do not overlap.)

Reproduction

from matrixone.sqlalchemy_ext.fulltext_search import boolean_match
f = boolean_match('body').must("O'Brien")
f.compile()
# "MATCH(body) AGAINST('+O'Brien' IN BOOLEAN MODE)"   ← literal broken

f2 = boolean_match('body').must("stop-fire").compile()
# "+stop-fire" — 'fire' becomes an exclusion; literal product search misfires

Impact

  • Any application passing user input into these documented builders (the intended use) hits apostrophe-breakage or operator-hijack on ordinary English input; searchable-content domains (product names, titles, error strings) routinely contain -, +, quotes, parentheses.
  • Errors from (1) arrive as server-side syntax errors that do not mention the term; semantic misfires from (2) return wrong rows with no error at all — the search quietly means something else.
  • The three modes (NATURAL_LANGUAGE/BOOLEAN/QUERY_EXPANSION) share the same unescaped AGAINST('{query}') frame, so all are affected.

Expected behavior

  1. Escape ' in the assembled query string before wrapping ('' doubling) in all three compile paths.
  2. Per-term escaping for boolean mode: wrap terms containing operator characters in double quotes ("stop-fire", "c++") or escape per MySQL rules; phrase() should guard embedded double quotes.
  3. Quote column names in MATCH(...) with backticks (doubling any embedded backtick).
  4. Regression table: terms O'Brien, stop-fire, c++, a "quoted" x, column order — assert the compiled SQL is a single well-formed statement and the boolean semantics match the caller's intent.

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. Ariznawlll commented on Oct 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    Partial-verify against current main (2026-10-27): the apostrophe escaping shown in this report is now fixed — boolean_match('body').must("O'Brien").compile() renders AGAINST('+O''Brien' ...) on main. The remaining unverified halves of the report: (a) boolean-operator hijacking — a term like stop-fire still compiles to +stop-fire which boolean mode reads as (require stop, exclude fire); (b) MATCH() column names are still unquoted raw joins. Recommend keeping this open only for those two residual pieces (or splitting them if triage prefers).

  5. MatrixAdventurer commented on Oct 28, 2026

    @MatrixAdventurer
    Contributor

    已在当前 main(e9dc8c4)复核,结论和上面 @Ariznawlll 的部分复核一致:只修复了一部分,暂不关闭。

    已修复: AGAINST 字面量的单引号转义。#31262(28e8760)在 clients/python/matrixone/sqlalchemy_ext/fulltext_search.py 的 L645、L657、L897 改用 fulltext_term_token(...)。boolean_match('body').must("O'Brien") 生成 AGAINST('+O''Brien' IN BOOLEAN MODE)。

    仍未修复(在 main 上本地编译验证):

    1. 布尔运算符劫持: 词项仍然原样拼接(FulltextElement.build,L121-122)。must("stop-fire") 生成 '+stop-fire',must("c++") 生成 '+c++'。
    2. phrase 内嵌双引号: phrase('a "quoted" thing') 生成 '"a "quoted" thing"',短语边界被破坏。
    3. MATCH 列名未加引号: boolean_match('order', 'body') 生成 MATCH(order, body)。

    建议保留本 issue 跟踪这三项。

    本操作由 LW 授权的 Grok Bot 智能体执行

  6. added a commit that references this issue on Oct 30, 2026
    bbfe556
  7. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31774 (main bbfe55669a1440424914d45a283a1d0e36f722fa): please verify fulltext boolean term validation, embedded quotes and MATCH identifier quoting. This isolated QA run inspected the issue and merged source but did not run tests or a live MatrixOne service at this SHA because resource headroom under the existing guard was insufficient. Earlier QA counterexamples and PR author-reported validation remain scoped to their recorded versions and are not results of this QA run.

  8. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。全文检索 builder 的 MATCH 列名仍未正确引用,合法的保留字列名导致 SDK 生成的查询无法执行。

    关键证据

    • 官方 main@d39d8a40343ccd40bf665036388df7a40ee867f8;相关修复 PR fix(python): unify SDK query and analytics lifecycle contracts #31774 的合并提交 bbfe55669a1440424914d45a283a1d0e36f722fa 在其祖先链。实际二进制与 SDK 同完整 SHA;本地隔离 1 CN / 1 TN / 1 LogService,Python 3.12。
    • 三个独立库创建含保留字列 order 的表和真实全文索引。手写 MATCH(`order`) AGAINST('+alpha' IN BOOLEAN MODE) 均返回 id 1;boolean_match('order').must('alpha').compile() 实际生成 MATCH(order) AGAINST('+alpha' IN BOOLEAN MODE),在真实服务均报 1064,3/3。
    • 每次失败后源表两行逐值不变,连接 SELECT 1 正常,测试库清理。此项是原 issue 中已列出的列名引用边界,不另建重复 issue;本轮未据此判断布尔词项其他语义。

    待解决点

    • 请让 MATCH 构造正确处理保留字及特殊标识符,并同时验证 SQLAlchemy 列对象和一般列名;修复前的失败断言未加入 MOTR 成功结果。本轮未运行完整 MOTR suite。
  9. added a commit that references this issue on Oct 30, 2026
    91dfab2
  10. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    新修复转 QA:#31889 已合入 main,合并提交 91dfab2132c4c4fcd3f9a115dbfa968b196b8b14。

    四个MATCH入口统一引用列名/SQLAlchemy列对象,保留qualified字符串与atomic列名;对应旧d39d8a4保留字order的真实失败路径。

    已核对 issue 正文、完整评论及当前合并源码/测试内容。历史失败按其原 SHA 保留;新修复直接覆盖上述残留,未发现适用于新修复的已验证反例或尚未完成的开发 follow-up。本轮仅源码/测试内容静态核对,没有运行 MatrixOne 服务或 SDK 回归;作者在 PR 内的运行结果是作者证据,不是本轮独立测试。转 Ariznawlll 继续验证,不代表验收通过。

    QA 待验范围:order/特殊标识符、列对象、单引号与布尔词项/phrase边界、真实全文索引语义和源行不变,保留dotted-name建索引原生限制。

  11. Ariznawlll commented on Oct 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。SDK 已覆盖的保留字列和词项场景通过,但原 issue 要求的含反引号特殊列名无法完成端到端全文检索验证:原生全文索引创建先报语法错误。

    关键证据

    • 版本:官方 main@6e5f2ce0fa52b834e39fb4ada00984fa57c02c14,本地隔离双 CN / 单 TN / LogService,服务二进制和 SDK 源码对应该 SHA。
    • order 保留字列的真实索引上,三个 SDK 构造入口以及 O'Brien、stop-fire、c++ 的词项结果与原生查询对照一致;嵌入引号的非法 phrase 按预期拒绝。两 CN 各 3 轮均通过,源表保持不变;可执行部分已补入 MOTR #298 的 issue_31399_fulltext_terms,实际运行 3/3。
    • 新建含 `tickcol `` 列的表并执行 ``CREATE FULLTEXT INDEX ft_tick ON special_docs (tickcol`),原生服务在索引创建阶段 3/3 报语法错误,内部 SQL 的报错位置指向列名中反引号后的 col;因此没有全文索引可供 SDK 执行该特殊列名查询。这是服务端前置步骤的实测失败,不证明 SDK 的列名转义本身仍有错。

    待解决点

    • 请研发确认并处理反引号列名的原生全文索引建索引路径,然后重跑 SDK MATCH 引用及真实结果验证;当前 MOTR 用例不把这一未覆盖边界算作通过。
  12. XuPeng-SH commented on Oct 31, 2026

    @XuPeng-SH
    Contributor

    新修复转 QA:#31953 已合入 main,merge SHA 569d521cfae73ba80c42f58366c172ce4ef84ed7。

    本次原生 fulltext population、post-DML 和 CDC SQL 统一原子标识符引用,并携带 tokenizer 配置,覆盖旧 main 含反引号列名建索引前置失败。请复验原生创建、SDK MATCH、保留字/反引号、O'Brien/stop-fire/c++、phrase 拒绝、增删改及异步索引追平。4.2.5 的已知原生限制不是 main 修复已通过,也不自动要求 backport。

    验证证据:作者记录离线多 SQLAlchemy 矩阵、187 个 native 合同、19 个全文 SQL 检查;当前 head 独立 reviewer 记录 315 个 focused Python tests、11 个 SDK online cases 及三个 Go owner package 通过。上述均为 PR 作者/评审者报告,不是本轮独立本地实测。本轮读取 issue 完整评论、修复范围及远端合并状态,未运行服务或测试;未发现适用于新合并版本的已验证反例或未完成的开发 follow-up。转 @Ariznawlll 继续验证,不代表验收通过。完整 BVT、多 CN、长稳和生产环境验证不在本轮证据范围内。

  13. Ariznawlll commented on Nov 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。全文搜索构造器能正确引用保留字和含反引号的列,特殊词项与原生查询一致;此前阻塞验收的反引号列建索引场景现已通过。

    关键证据

    • 版本与环境:官方 main@35202093259793e48dcd7abc8ff418f07190f746,修复 fix(python): restore SDK SQL and execution contracts #31953 的 merge 569d521cfae73ba80c42f58366c172ce4ef84ed7 为祖先;本地隔离单 CN / TN / LogService,服务 SQL 版本及二进制构建 SHA 一致,vcs.modified=false。Python 3.12,SQLAlchemy 1.4.54 与 2.1.6。
    • Happy Path:真实表分别对保留字 order 和含反引号列 tick\col建立全文索引;保留字列的三个 SDK 构造入口查询alpha均返回 id 1、2。特殊词项O'Brien、stop-fire、c++` 的 SDK 结果逐项与原生 MATCH 对照。反引号列 SDK MATCH 与原生 MATCH 均返回 id 1、2;更新 id 3、插入 id 4、删除 id 4 后等待索引追平,各阶段均对照完整结果。两种 SQLAlchemy 版本经 MOTR runner 各 3/3。
    • Unhappy Path:内含引号的非法 phrase 和带模糊布尔操作符的词项均在 SDK 层拒绝;之后源表 id 检查为 1、2、3,未被错误请求修改。测试每轮删除独立数据库,结束后 qa_terms_ 前缀测试库计数为 0。
    • SDK 现有 test_fulltext_embedded_backtick_column 还分别通过同步和异步客户端执行反引号列的建索引及插入、更新、删除后查询;SQLAlchemy 1.4.54 和 2.1.6 各 3 轮通过。
    • 回归:MOTR #298 已扩展全文检索用例,显式启用 SDK 时实服运行,默认跳过。三次是独立用例执行,不是生产规模压力测试。
    • MOTR 离线 CI通过;该 CI 不运行默认跳过的 SDK 实服用例。

    待解决点

    • 本轮在单 CN、本地小数据执行;未覆盖多 CN、其他 tokenizer、长期压力或 4.2.5 发布线,也不将这些范围算作通过。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions