Skip to content

[Bug]: single-row _build_insert_sql does not escape quotes, crashes on batch key mismatch, quotes numerics #31376

Closed

Description

@Ariznawlll

DRAFT 2 (SDK, 2026-10-27) — candidate issue, not yet filed

Title: [Bug]: single-row _build_insert_sql does not escape quotes (batch version does) and batch silently drops unknown columns / crashes on missing ones

Description

(Scope note: the "silently drops extra columns in later batch rows" half of this report was already filed as #31106; this issue covers the remaining, distinct defects: the unescaped single-row builder, the KeyError crash on missing keys, and numeric-value quoting.)

BaseMatrixOneClient._build_insert_sql (single-row) and _build_batch_insert_sql sit in the same file with opposite escaping behavior — evidence that a past injection fix patched only the batch path: (single-row) and _build_batch_insert_sql sit in the same file with opposite escaping behavior — evidence that a past injection fix patched only the batch path:

# base_client.py:88 (single)  — NO escaping:
formatted_values.append(f"'{str(value)}'")
# base_client.py:136 (batch)  — escaped:
escaped_value = str(value).replace("'", "''")

Verified side by side:

b._build_insert_sql("t", {"name": "o'rien"})       # → INSERT INTO t (name) VALUES ('o'rien')      ← broken/injectable
b._build_batch_insert_sql("t", [{"name": "o'rien"}]) # → INSERT INTO t (name) VALUES ('o''rien')     ← correct

session.insert() / client.insert() route through the single-row builder, so the common path is the injectable one.

Additional defects in the same pair:

  1. All values are string-quoted, including numerics ({"count": 5} → VALUES ('5')). MySQL tolerance hides this for INT columns, but the quote-wrapping selects string semantics — e.g. width checks, comparison coercion and strict-mode writes to numeric columns behave differently from batch inserts of unquoted numbers, and it also re-invokes the numeric-string comparison bugs ([Bug]: Default collation is binary/case-sensitive; explicit _ci collations are ignored #25096 family) for predicate columns written this way.
  2. Batch insert with heterogeneous dicts: columns are taken from the first row only.
  3. Vector list values are joined without quoting elements — an element containing ] or , corrupts the vector literal.

Reproduction

from matrixone import Client
c = Client(); c.connect(...)
c.execute("CREATE TABLE t (id INT, name VARCHAR(20))")
c.insert("t", {"id": 1, "name": "o'rien"})        # SQL syntax broken by literal
c.batch_insert("t", [{"id":1,"name":"a"}, {"id":2}])   # KeyError('name')
c.batch_insert("t", [{"id":1}, {"id":2,"extra":99}])   # extra column silently discarded

Impact

  • The single-row path is the everyday insert API (session.insert, client.insert, ORM flush paths building through it); any string with an apostrophe — names (O'Brien), free-text fields, JSON payloads with quotes — breaks the statement or becomes an injection vector, exactly what [Bug]: Python SDK builders interpolate unsafe SQL and misrender typed parameter values #31186 was filed against at the builder layer; this is the same vulnerability one layer down, still unpatched.
  • Heterogeneous batch payloads are the norm in ETL (rows from loosely-typed sources); today you either crash on the first missing key or silently drop keys that exist in later rows only — silent divergence between input and stored data.
  • The numeric-quotes issue makes single and batch semantics differ (batch quotes too, so behavior matches at least; but both diverge from normal unquoted numeric literal SQL and interact badly with strict compare semantics).

Expected behavior

  1. Single-row _build_insert_sql must escape quotes exactly like batch (ideally: share one formatting function so they can't diverge again).
  2. Batch should either (a) union the keys of all rows (fill missing with NULL/DEFAULT) or (b) validate and raise a precise error ("row 3 misses column 'name'"); silently dropping is the worst option.
  3. Emit numerics unquoted (int/float/bool → str(value)), date/datetime via proper literal, bytes via hex; only strings get quoted+escaped.
  4. Vector elements need escaping or a strict numeric-element assertion.

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

    Verified against current main (2026-10-27): the single-row _build_insert_sql in base_client.py now routes through _sql_literals.sql_literal/sql_identifier with proper '' doubling; the escaping gap (first half of this report) is fixed. Closing as fixed on main. NOTE: if the KeyError-on-missing-column batch behavior is still of interest, it is tracked separately in the batch-semantics consolidation (please see the unified batch-insert semantics issue) — flagging so this half doesn't get lost.

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