Skip to content

[Bug]: hidden composite-PK column __mo_cpkey_col is writable via INSERT #31270

Closed

Description

@Ariznawlll

DRAFT ISSUE 35 (候选, 不要提交)

标题候选: [Bug]: internal composite-PK column __mo_cpkey_col is user-visible and writable — INSERT can store the hidden key disassociated from the declared logical key

标签: kind/bug, needs-triage

Relation to #26993 (CLOSED)

#26993 fixed the information_schema.columns metadata leakage of __mo_cpkey_col (through ODBC SQLColumns). That issue's verification only covered the catalog view (SHOW COLUMNS/SELECT * metadata) — it did not cover (and the fix did not remove) direct SQL access to the column, and it did not cover the write path at all. This report files the remaining, more dangerous half: name-addressable read AND write of the internal column through ordinary SQL on composite-PK tables. Reproduced on current main (2026-10-26), well after #26993 closed.

Description

For tables with composite primary keys, MatrixOne materializes the key as an internal hidden column __mo_cpkey_col. Unlike genuinely virtual internals, this column is visible and writable through ordinary user SQL, allowing callers to specify a primary-key value that does not correspond to the row's declared (a,b) contents:

CREATE TABLE cpk3 (a INT, b INT, PRIMARY KEY(a,b));
INSERT INTO cpk3 VALUES (1,10);   -- hidden col auto-computed: hex = 3A15013A150A

INSERT INTO cpk3 (__mo_cpkey_col, a, b) VALUES (unhex('0001'), 1, 99);
-- succeeds! the row now has:
--   a=1, b=99, __mo_cpkey_col = 0x0001  (not the encoding of (1,99))

SELECT a, b, hex(__mo_cpkey_col) FROM cpk3 ORDER BY b;
-- 1  10  3A15013A150A     ← correct hidden key
-- 1  99  0001             ← caller-injected, inconsistent with (a,b)

Interesting nuance found while probing (worth keeping in mind before fixes):

  • Single-column-PK tables do not expose __mo_cpkey_col — SELECT ... FROM cpk2 errors with column __mo_cpkey_col does not exist (matching MySQL behavior of rejecting unknown columns). So the exposure is specific to the composite-key implementation.
  • The logical uniqueness appears to still hold (inserting a duplicate (1,10) correctly failed with 1062 even after the tampering), suggesting the check uses the (a,b) path rather than the stored cpkey — which raises the question of what the stored cpkey is then used for (merge/compaction, repl, index-joins). If any physical-layer dedup or index path relies on cpkey bytes, inconsistent cpkeys can double-count, orphan, or corrupt those rows in ways invisible until a flush/merge.

Reproduction

As above — any two-column composite PK table, one insert naming __mo_cpkey_col explicitly with a small hex literal.

Impact

  • Application/ORM accidents: column-list-driven inserts that enumerate information_schema columns (some generic sync tools do) will pick up __mo_cpkey_col as a real column when reflecting a composite-PK table — turning a well-behaved sync job into one that reproduces stale cpkeys (the value from a previous timestamp of the row), the exact class of bug that breaks replicas silently.
  • Potential physical-layer integrity: if compaction/index builds use cpkey bytes as identity, caller-mismatched cpkeys can produce index anomalies. Even if today's code paths all derive the key from columns, allowing user writes to an internal column is a standing hazard against future refactors.
  • Read exposure alone leaks implementation (encoding format is visible), which is usually tolerable but is distinct from allowing writes.

Expected behavior

Mirror the single-column behavior uniformly:

  • INSERT INTO t (__mo_cpkey_col, ...) on a composite-PK table should error as unknown column (the column is not part of the declared table's public schema), and
  • ideally SELECT __mo_cpkey_col too (or at least make it readable-only via a documented system-suffix exception); exposing the system column should be opt-in (e.g. SELECT * INCLUDING SYSTEM style or a session flag), not default-visible.

If the composite-key needs a user-visible materialization for debugging, provide a function (e.g. mo_pk(a,b)) or a documented pseudo-column with read-only semantics instead of the writable internal.

Activity

  1. added
    kind/bug
    on Oct 26, 2026
  2. added this to the MO-202610 milestone on Oct 26, 2026
  3. added theissue type on Oct 26, 2026
  4. changed the title [-][Bug]: composite-PK hidden column __mo_cpkey_col is name-addressable and writable — INSERT can store a cpkey inconsistent with the row's declared key (reopens the write-path half of #26993)[/-] [+][Bug]: hidden composite-PK column __mo_cpkey_col is writable via INSERT[/+] on Oct 26, 2026
  5. added a commit that references this issue on Oct 27, 2026
    00c103b
  6. XuPeng-SH commented on Oct 28, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31321 (00c103b68bd2b39194147508445956f5ece16dda): affected hidden-column INSERT/REPLACE/UPDATE/LOAD SET and internal ALTER COPY tests were not run in this environment. Large planner links and a current-head service were blocked by the existing resource guard; no product failure or full acceptance is inferred. Please verify the original hidden storage-column write contract and unchanged internal authority paths.

  7. Ariznawlll commented on Oct 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。显式写入隐藏存储键被拒绝,失败后数据不变;正常写入和内部 COPY 路径保持可用。

    关键证据

    • 版本:官方 main@72d99c85751dc5b9b7775cb50571e7efd6400487,包含已合入修复 fix(plan): reject explicit writes to hidden storage columns #31321;macOS arm64、隔离单进程 LogService / TN / CN,实际二进制 git_version() 为 72d99c8575。
    • 原复现 INSERT INTO t(__mo_cpkey_col,a,b,v) ... 在复合主键表上 3/3 返回未知列,已有 (1,10,7) 和计算出的内部键保持不变。INSERT ... SELECT、REPLACE、INSERT IGNORE、UPDATE、ODKU 和预编译显式目标均被拒绝;每次拒绝后检查完整行和内部键,3/3。
    • fake key 的 INSERT、UPDATE、ODKU 和两种 LOAD DATA INLINE 目标均被拒绝且原行不变;普通更新、ODKU、插入、二级索引查询及 ALTER ... ALGORITHM=COPY 保留内部 fake key,3/3。
    • MOTR PR #287、回归源码:定向运行 3/3、0 跳过,六项相关用例以两个 worker 并行运行 6/6 通过;该提交 Python 3.10/3.12/3.13 和 Go 离线 CI 均通过。未运行完整 MOTR suite。
    • 官方 main 随后前进至 9e29011e7313a57962adc00963baa659d1e81e37,重建并启动对应二进制后,MOTR 该回归补跑 3/3、0 跳过;先前结果保留对应 72d99c… 的版本信息。

    待解决点

    无。

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