Repository navigation
Description
DRAFT 8 (SDK, 2026-10-27) — candidate issue, not yet filed
Title: [Bug]: MetadataManager and MoCtlManager interpolate identifiers/params into SQL unescaped; MoCtl also hard-crashes on non-JSON results
Description
Two unexplored SDK managers interpolate raw user strings into SQL — the remaining unfiled members of the escaping family opened by #31375/#31376/#31378:
1. MetadataManager._build_metadata_scan_sql (metadata.py:~147)
table_ref = f"{dbname}.{tablename}.?{indexname}" # raw concat
return f"SELECT * FROM metadata_scan('{table_ref}', '*') g"dbname/tablename/indexname are caller-supplied and land inside a single-quoted SQL literal with no escaping. A table named x' UNION SELECT ... -- (or a value coming from any external naming source) rewrites the metadata_scan argument — and since metadata_scan exposes internal object metadata, this path is security-sensitive (file-level metadata enumeration).
All public entry points route through it: scan(), get_table_brief_stats(), get_table_detail_stats() (each passing the other two components too, e.g. include_indexes feeds indexname).
2. MoCtlManager._execute_moctl (moctl.py:~129)
sql = f"SELECT mo_ctl('{method}', '{target}', '{params}')"Three caller strings into one quoted-arg function call, zero escaping. mo_ctl is the administrative control plane (flush, checkpoint, task ops) — an injection here can trigger admin operations (e.g. crafted params that close the string and add a second statement fragment; multi-statements are driver-blocked, but argument-boundary injection inside a single mo_ctl call is enough to redirect the command). Typical control-plane code passes method/target from config tables or request parameters.
Additionally, _execute_moctl does:
result_str = result.rows[0][0]
parsed_result = json.loads(result_str) # no guardmo_ctl does not always return JSON (version/build variants return plain text or an error banner). json.loads then raises JSONDecodeError, which the enclosing except rewraps into a generic MoCtlError("Failed to execute: ..."), hiding that the command actually succeeded with a non-JSON body. The SDK cannot report the raw output in that (common) case.
Reproduction
# metadata
c.metadata.scan(dbname="db", tablename="' OR '1'='1")
# emitted: SELECT * FROM metadata_scan('db.' OR '1'='1', '*') g → parse error / unexpected enumeration
# moctl
c.moctl.flush_database("acc', 'extra') -- ")
# emitted: SELECT mo_ctl('flush', 'database', 'acc', 'extra') -- ')Impact
- Both managers accept names/params that in real deployments come from outside (DB listers, request payloads, config). The metadata path enumerates internal storage objects; the moctl path can influence maintenance operations (flush/checkpoint storms) — the highest-privilege surface in the SDK.
- The JSON hard-assumption makes legitimate outputs (plain-text mo_ctl replies) surface as generic errors, sabotaging ops tooling that needs the raw returnStr.
Expected behavior
- Escape
'(doubling) inside all interpolated literal args — or better, since these are fixed-shape calls, SQLAlchemytext()with bindparams (the SDK already depends on SQLAlchemy). _execute_moctl: try json.loads; on failure return a structured{"raw": result_str}(or attach raw under a key) instead of erroring, and raise with the raw text preserved in the exception message.- Add hostile-name regression tests for both managers (quote/backslash payloads) asserting single-literal rendering.
Cross-references
Escaping-family tracker context: #31375 (helpers are no-ops), #31376 (insert builder), #31378 (filter_by). These two managers are the remaining unaudited interpolation sites found in today's sweep of modules not covered before (metadata.py, moctl.py were untouched by prior reports).
Activity
MatrixAdventurer commentedon Oct 28, 2026
已在当前 main(
e9dc8c4)复核,本 issue 只修复了一部分,暂不关闭。已修复(#31262、#31322):
clients/python/matrixone/metadata.pyL157 改用sql_literal(table_ref)(fix(python-sdk): unify query contracts and atomic manager operations #31262,28e8760)。tablename="' OR '1'='1"生成metadata_scan('db.'' OR ''1''=''1', '*'),是单个字面量。clients/python/matrixone/moctl.py的_build_moctl_sql(L44)三个参数都走sql_literal(fix(python): unify SDK execution contracts across 4.1.x and 4.2.x #31322,baa83f4)。"acc', 'extra') -- "被渲染为一个完整的字符串参数。仍未修复: 预期行为第 2 条,即 mo_ctl 返回非 JSON 文本时的处理。现在不会崩溃,但
moctl.pyL155-156 抛出的是MoCtlError("Failed to parse mo_ctl result: Expecting value: line 1 column 1 (char 0)"),原始返回文本丢失,调用方拿不到 mo_ctl 实际输出了什么。在 main 上用 mock 返回plain text banner OK验证过。我会单独提一个小 PR,在这个错误里保留原始返回文本,并附回归测试。
本操作由 LW 授权的 Grok Bot 智能体执行
fix(python-sdk): keep raw mo_ctl output when the reply is not JSON (#…
XuPeng-SH commentedon Oct 28, 2026
QA on merged SDK
f97056d1107a89854bfac2c06202698e3aafd41e(#31524): the offline stage/moctl files passed 67 tests, including sync/async non-JSON raw-output preservation and no double wrapping. The release-boundary and metadata offline tests passed 31 additional cases. A task-only facade probe exercised sync/asyncmetadata.scanand_execute_moctlwith quote payloads, metadata backslash input and a mo_ctl backslash argument; generated SQL kept values inside one literal or used hex conversion. The first added backslash probe failed because its expected hex encoded two backslashes while the input had one; after correcting that test expectation it passed. No live control-plane operation was performed.These checks cover the reported SDK rendering and raw-output contract. Stage SQL tests are separate from this issue.
Ariznawlll commentedon Oct 28, 2026
结论
测试通过。metadata_scan 和 mo_ctl 参数的引号按单个字面量处理,同步及异步非 JSON 返回可从异常取得原始文本。
关键证据
main@9eb05ce0f5ac5ddce36024aaa21c4c79d3e4334c,包含 修复 PR #31524 和此前的字面量修复;隔离 1 Log + 1 TN + 2 CN,Python SDK 和服务版本一致。plain text banner OK。待解决点