Repository navigation
Description
DRAFT 5 (SDK, 2026-10-27) — candidate issue, not yet filed
Title: [Bug]: ORM filter_by() interpolates strings without escaping and silently ignores unknown columns
Description
(filter_by silently dropping predicates for unknown fields / string-table queries is already filed as #31207 — CLOSED. This report covers the remaining unfiled defect: unescaped raw interpolation. The silent-drop aspect is context here, since it multiplies with the quoting flaw below.)
BaseMatrixOneQuery.filter_by(**kwargs) (matrixone/orm.py) has two defects:
1. String values are interpolated raw — no quote escaping
if isinstance(value, str):
self._where_conditions.append(f"{key} = '{value}'") # raw f-string, no '' doublingAny string value containing a single quote breaks the literal or injects:
q.filter_by(name="o'rien") # → WHERE name = 'o'rien' (broken/injectable)
q.filter_by(tag="x' OR '1'='1") # → WHERE tag = 'x' OR '1'='1' (predicate rewritten)This is the same escaping-family defect filed today in #31375 (_escape_string no-op) and #31376 (single-row _build_insert_sql) — filter_by takes the third path: it doesn't call the (no-op) helper at all, it interpolates inline. Note the sibling method filter() correctly routes string ?-params through substitution (which itself has the same quoting flaw), so the codebase has three different unescaped interpolation sites in one ORM class.
2. Unknown columns are silently ignored
if key in self._columns:
...append condition...
# else: nothing — no error, no warningA typo in a filter key (filter_by(nmae="x")) produces a query with no WHERE clause at all — the equivalent of SELECT * FROM users returning the entire table. SQLAlchemy's filter_by raises AttributeError/InvalidRequestError for unknown attributes; here the mistake is silently swallowed.
Verified statically (both code paths quoted above from orm.py); behavior follows directly from the source. Combined worst case: caller builds filter_by(active=true, acount_id=42) (typo), the acount_id filter vanishes, and the query runs unfiltered against the table.
Reproduction
from matrixone import Client
c = Client(); c.connect(...)
q = c.query(User).filter_by(name="o'rien")
sql, params = q._build_select_sql() # inspect emitted SQL: name = 'o'rien'
# typo behavior (silently no WHERE): already covered by #31207; the *new* defect here is the raw interpolation aboveImpact
- The
filter_byAPI is the friendly/Pythonic entry point (users pass values, not SQL fragments); it is exactly where unescaped interpolation hurts most, because values are application data (user names, tags, search terms) rather than trusted SQL. - The silent-ignore on unknown keys turns every typo into a full-table operation — a correctness footgun magnified by the fact that ORM users expect
filter_by(hostile_input)to constrain results. - int/float bypass quoting (fine), but
bool,None,datetimefall into the?-param path — combined with the?-substitution bug from the same class, those values are also at risk (already filed separately where measured).
Expected behavior
- Escape single quotes in string interpolation (
value.replace("'", "''")), or better: keepkey = ?for all types and route values through_where_paramsuniformly — the substitution machinery then owns quoting in one place. - Raise on unknown keys (SQLAlchemy parity): a clear
ValueError(f"Unknown column {key!r} for model ...")instead of silence. If backward compatibility requires leniency, at least emit a warning.
Relation to today's other finds
- [Bug]: SDK _escape_string/_escape_identifier are no-ops that only wrap quotes #31375:
_escape_string/_escape_identifierare no-ops (the escape helpers themselves). - [Bug]: single-row _build_insert_sql does not escape quotes, crashes on batch key mismatch, quotes numerics #31376: single-row
_build_insert_sqlinterpolates raw. - This issue:
filter_byinterpolates raw on a third path and silently drops unknown keys (the new, distinct part).
Filed separately because the silent-ignore defect has no overlap at all, and the fix sites differ (helper vs builder vs ORM method).
Activity
MatrixAdventurer commentedon Oct 28, 2026
已在当前 main(
e9dc8c4)复核,本 issue 的两个缺陷都已修复:28e8760)修复。clients/python/matrixone/orm.py中MatrixOneQuery.filter_by现在通过sql_literal(value)渲染值(L981)。filter_by(name="x' OR '1'='1")生成WHERE name = 'x'' OR ''1''=''1',单引号被正确加倍,谓词没有被改写。Query.filter_by改为= ?加参数绑定(L205)。a772c02)修复。三个 Query 类(Query、BaseMatrixOneQuery、MatrixOneQuery)的filter_by(nmae='x')都会抛出ValueError: Unknown model field: nmae(L199、L970)。验证方式: 在 main 上用本地脚本对三个类分别检查了上面两种输入;另外运行
pytest tests/offline -k filter_by,结果为 5 passed。因此按已完成关闭。
本操作由 LW 授权的 Grok Bot 智能体执行
fix(python): unify SDK query and analytics lifecycle contracts (#31774)
XuPeng-SH commentedon Oct 30, 2026
QA handoff for merged #31774 (main
bbfe55669a1440424914d45a283a1d0e36f722fa): please verify the public SDK contract and failure/recovery controls for ORM filter_by interpolates string values without escaping. This isolated QA run inspected the issue and source but did not run functional tests or a live MatrixOne service at this SHA because the existing resource guard left insufficient headroom. Author-reported PR validation is separate evidence, not a result of this QA run.Ariznawlll commentedon Oct 30, 2026
结论
测试通过。同步 ORM filter_by 对引号等数据按字面值过滤,未知模型字段明确拒绝,没有静默去掉条件。
关键证据
main@d39d8a40343ccd40bf665036388df7a40ee867f8;修复 PR fix(python-sdk): unify query contracts and atomic manager operations #31262、fix(python-sdk): preserve legacy filter_by predicates #31427 与后续 fix(python): unify SDK query and analytics lifecycle contracts #31774 均已合入且 merge commit 在该版本祖先链。实际二进制与 SDK 同完整 SHA,本地隔离 1 CN / 1 TN / 1 LogService,Python 3.12。o'rien、x' OR '1'='1、普通字符串、反斜线和换行字符串均只匹配其各自 id 1、2、3、5、6;不存在谓词被改写导致的额外命中。nmae两种查询均抛异常;完整六行含 NULL 的原数据逐值不变,测试库每轮清理。回归已加入 MOTR #295,定向 3/3 通过、0 skipped。待解决点