Skip to content

fix: escape SQL string literals to prevent injection in Data Branch operations - #26218

Merged
mergify[bot] merged 5 commits into
matrixorigin:mainfrom
VioletQwQ-0:fix/issue-26194-sql-injection
Jul 27, 2026
Merged

mergify[bot] merged 5 commits into
matrixorigin:mainfrom
VioletQwQ-0:fix/issue-26194-sql-injection

Conversation

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #26194

What this PR does / why we need it:

修复 Data Branch 操作中的 SQL 字符串字面量注入漏洞。

问题描述

在创建 Data Branch 快照、检查外键关系等操作时,代码直接将用户可控的标识符(数据库名、表名、账户名)插入到 SQL 字符串字面量中,没有进行适当的转义。虽然这些标识符通过了 MO parser 的验证,但它们可以合法地包含单引号(当使用反引号括起时,如 table'name),这会导致 SQL 语法错误或潜在的注入攻击。

修复内容

  1. pkg/frontend/data_branch_snapshot.go:在 createBranchProtectSnapshot 中对所有字符串值使用 quoteSQLStringLiteral() 进行转义
  2. pkg/sql/plan/build_dml_util.go:添加 quoteSQLStringLiteral() 辅助函数,并在 GetSqlForFkReferredTo() 中应用
  3. pkg/frontend/snapshot.go:在 checkTableIsMaster() 和 checkDatabaseIsMaster() 中应用转义

quoteSQLStringLiteral() 函数将单引号转义为双单引号(''),将反斜杠转义为双反斜杠(\\),然后用引号包裹结果,遵循标准 SQL 字符串字面量转义规则。

验证

已在本地验证:

  • 创建包含单引号的表名(如 src'test)成功
  • Data Branch 创建操作对于正常表名正常工作
  • mo_snapshots 表中的数据正确插入

已知限制

DATA BRANCH CREATE 语句在解析带单引号的表名时存在独立的问题(查找表时错误地将 src'test 转换为 src''test),这是一个独立的 bug,不在本 PR 的修复范围内。

…perations

Fixes matrixorigin#26194

When creating Data Branch snapshots, the code directly interpolated
user-controllable identifiers (database names, table names, account
names) into SQL string literals without proper escaping. Although
these identifiers pass through the MO parser, they can legally contain
apostrophes when backtick-quoted (e.g., `table'name`), leading to
malformed SQL and potential injection.

Changes:
- pkg/frontend/data_branch_snapshot.go: Use quoteSQLStringLiteral()
  for all string values in createBranchProtectSnapshot INSERT
- pkg/sql/plan/build_dml_util.go: Add quoteSQLStringLiteral() helper
  and apply it in GetSqlForFkReferredTo()
- pkg/frontend/snapshot.go: Apply escaping in checkTableIsMaster()
  and checkDatabaseIsMaster()

The quoteSQLStringLiteral() function escapes backslashes and single
quotes (replacing ' with ''), then wraps the result in quotes,
following standard SQL string literal escaping rules.

Co-Authored-By: Claude Opus 5 <[email protected]>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep-reviewed at head 2534963. The generated branch snapshot and foreign-key lookup SQL now preserve quoted identifiers safely, including apostrophes and backslashes; the remaining directly formatted snapshot name and UUID are internally generated. Targeted frontend and plan tests pass. No blocking production issue found.

@mergify mergify Bot added the queued label Jul 27, 2026
@mergify

mergify Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-27 08:43 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ❌ Checks failed · in-place
  • 🚫 Left the queue — 2026-07-27 12:32 UTC · at 943d8fdcb7b1d7111180059fa1d09aa7e9372c45

This pull request spent 3 hours 48 minutes 43 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection] (documentation)
  • github-review-decision = APPROVED [🛡 GitHub branch protection] (documentation)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

Reason

The merge conditions cannot be satisfied due to failing checks

Failing checks:

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

@mergify
mergify Bot merged commit f280784 into matrixorigin:main Jul 27, 2026
25 of 26 checks passed
@mergify mergify Bot removed the queued label Jul 27, 2026
XuPeng-SH pushed a commit that referenced this pull request Jul 28, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes #26073

## What this PR does / why we need it:

#26218 already fixed branch-snapshot SQL literal escaping on `main` and
added
parser-based unit coverage for quotes, backslashes, and injection-like
input.
After rebasing onto that implementation, this PR contains only the
end-to-end BVT coverage that is not present in #26218.

The BVT creates database and table identifiers containing backslashes,
creates
a Data Branch table, and verifies:

- the exact database-name bytes stored in `mo_catalog.mo_snapshots`;
- the exact table-name bytes stored in `mo_catalog.mo_snapshots`;
- the branch snapshot `obj_id` references the source table.

No production code or unit tests are changed.

### Validation

- `make build`
- `protect_11.sql` against rebased commit `e88eb003b`: 13/13 statements
passed
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@VioletQwQ-0
VioletQwQ-0 deleted the fix/issue-26194-sql-injection branch July 29, 2026 08:18
mergify Bot pushed a commit that referenced this pull request Jul 30, 2026
…#26422)

COPY-mode `ALTER TABLE` replaces a relation with a new physical table. Before #26330, this could lose or split foreign-key metadata, and clone/snapshot/PITR restore could miss dependency edges after an affected ALTER.

This backport:

- transfers child-side `mo_foreign_keys` rows across the COPY replacement and restores the logical source table name;
- keeps parent-side names stable so self references do not point at the temporary relation;
- canonicalizes parent reverse-reference mutations and preserves self-reference sentinel `0`;
- derives clone and restore dependency edges from historical table DDL in addition to catalog rows;
- enumerates ordinary snapshot source databases at the source timestamp;
- includes the unit and BVT regression coverage from #26330.

Backport notes:

- cherry-picked squash commit `eeaa44641d8984a248e6447b93e4e80d9f6a2f71`;
- resolved test conflicts by retaining only tests introduced by #26330, excluding unrelated newer main-branch tests;
- included the `pkg/sql/plan` SQL string literal quoting helper required by #26330. The helper originated in #26218, but the rest of that unrelated change is not included.

Approved by: @XuPeng-SH, @heni02
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…perations (matrixorigin#26218)

修复 Data Branch 操作中的 SQL 字符串字面量注入漏洞。

### 问题描述

在创建 Data Branch 快照、检查外键关系等操作时,代码直接将用户可控的标识符(数据库名、表名、账户名)插入到 SQL 字符串字面量中,没有进行适当的转义。虽然这些标识符通过了 MO parser 的验证,但它们可以合法地包含单引号(当使用反引号括起时,如 `table'name`),这会导致 SQL 语法错误或潜在的注入攻击。

### 修复内容

1. **pkg/frontend/data_branch_snapshot.go**:在 `createBranchProtectSnapshot` 中对所有字符串值使用 `quoteSQLStringLiteral()` 进行转义
2. **pkg/sql/plan/build_dml_util.go**:添加 `quoteSQLStringLiteral()` 辅助函数,并在 `GetSqlForFkReferredTo()` 中应用
3. **pkg/frontend/snapshot.go**:在 `checkTableIsMaster()` 和 `checkDatabaseIsMaster()` 中应用转义

`quoteSQLStringLiteral()` 函数将单引号转义为双单引号(`''`),将反斜杠转义为双反斜杠(`\\`),然后用引号包裹结果,遵循标准 SQL 字符串字面量转义规则。

### 验证

已在本地验证:
- 创建包含单引号的表名(如 `src'test`)成功
- Data Branch 创建操作对于正常表名正常工作
- `mo_snapshots` 表中的数据正确插入

### 已知限制

DATA BRANCH CREATE 语句在解析带单引号的表名时存在独立的问题(查找表时错误地将 `src'test` 转换为 `src''test`),这是一个独立的 bug,不在本 PR 的修复范围内。

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…26196)

## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#26073

## What this PR does / why we need it:

matrixorigin#26218 already fixed branch-snapshot SQL literal escaping on `main` and
added
parser-based unit coverage for quotes, backslashes, and injection-like
input.
After rebasing onto that implementation, this PR contains only the
end-to-end BVT coverage that is not present in matrixorigin#26218.

The BVT creates database and table identifiers containing backslashes,
creates
a Data Branch table, and verifies:

- the exact database-name bytes stored in `mo_catalog.mo_snapshots`;
- the exact table-name bytes stored in `mo_catalog.mo_snapshots`;
- the branch snapshot `obj_id` references the source table.

No production code or unit tests are changed.

### Validation

- `make build`
- `protect_11.sql` against rebased commit `e88eb003b`: 13/13 statements
passed
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…perations (matrixorigin#26218)

修复 Data Branch 操作中的 SQL 字符串字面量注入漏洞。

### 问题描述

在创建 Data Branch 快照、检查外键关系等操作时,代码直接将用户可控的标识符(数据库名、表名、账户名)插入到 SQL 字符串字面量中,没有进行适当的转义。虽然这些标识符通过了 MO parser 的验证,但它们可以合法地包含单引号(当使用反引号括起时,如 `table'name`),这会导致 SQL 语法错误或潜在的注入攻击。

### 修复内容

1. **pkg/frontend/data_branch_snapshot.go**:在 `createBranchProtectSnapshot` 中对所有字符串值使用 `quoteSQLStringLiteral()` 进行转义
2. **pkg/sql/plan/build_dml_util.go**:添加 `quoteSQLStringLiteral()` 辅助函数,并在 `GetSqlForFkReferredTo()` 中应用
3. **pkg/frontend/snapshot.go**:在 `checkTableIsMaster()` 和 `checkDatabaseIsMaster()` 中应用转义

`quoteSQLStringLiteral()` 函数将单引号转义为双单引号(`''`),将反斜杠转义为双反斜杠(`\\`),然后用引号包裹结果,遵循标准 SQL 字符串字面量转义规则。

### 验证

已在本地验证:
- 创建包含单引号的表名(如 `src'test`)成功
- Data Branch 创建操作对于正常表名正常工作
- `mo_snapshots` 表中的数据正确插入

### 已知限制

DATA BRANCH CREATE 语句在解析带单引号的表名时存在独立的问题(查找表时错误地将 `src'test` 转换为 `src''test`),这是一个独立的 bug,不在本 PR 的修复范围内。

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…26196)

## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#26073

## What this PR does / why we need it:

matrixorigin#26218 already fixed branch-snapshot SQL literal escaping on `main` and
added
parser-based unit coverage for quotes, backslashes, and injection-like
input.
After rebasing onto that implementation, this PR contains only the
end-to-end BVT coverage that is not present in matrixorigin#26218.

The BVT creates database and table identifiers containing backslashes,
creates
a Data Branch table, and verifies:

- the exact database-name bytes stored in `mo_catalog.mo_snapshots`;
- the exact table-name bytes stored in `mo_catalog.mo_snapshots`;
- the branch snapshot `obj_id` references the source table.

No production code or unit tests are changed.

### Validation

- `make build`
- `protect_11.sql` against rebased commit `e88eb003b`: 13/13 statements
passed
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…perations (matrixorigin#26218)

修复 Data Branch 操作中的 SQL 字符串字面量注入漏洞。

### 问题描述

在创建 Data Branch 快照、检查外键关系等操作时,代码直接将用户可控的标识符(数据库名、表名、账户名)插入到 SQL 字符串字面量中,没有进行适当的转义。虽然这些标识符通过了 MO parser 的验证,但它们可以合法地包含单引号(当使用反引号括起时,如 `table'name`),这会导致 SQL 语法错误或潜在的注入攻击。

### 修复内容

1. **pkg/frontend/data_branch_snapshot.go**:在 `createBranchProtectSnapshot` 中对所有字符串值使用 `quoteSQLStringLiteral()` 进行转义
2. **pkg/sql/plan/build_dml_util.go**:添加 `quoteSQLStringLiteral()` 辅助函数,并在 `GetSqlForFkReferredTo()` 中应用
3. **pkg/frontend/snapshot.go**:在 `checkTableIsMaster()` 和 `checkDatabaseIsMaster()` 中应用转义

`quoteSQLStringLiteral()` 函数将单引号转义为双单引号(`''`),将反斜杠转义为双反斜杠(`\\`),然后用引号包裹结果,遵循标准 SQL 字符串字面量转义规则。

### 验证

已在本地验证:
- 创建包含单引号的表名(如 `src'test`)成功
- Data Branch 创建操作对于正常表名正常工作
- `mo_snapshots` 表中的数据正确插入

### 已知限制

DATA BRANCH CREATE 语句在解析带单引号的表名时存在独立的问题(查找表时错误地将 `src'test` 转换为 `src''test`),这是一个独立的 bug,不在本 PR 的修复范围内。

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…26196)

## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#26073

## What this PR does / why we need it:

matrixorigin#26218 already fixed branch-snapshot SQL literal escaping on `main` and
added
parser-based unit coverage for quotes, backslashes, and injection-like
input.
After rebasing onto that implementation, this PR contains only the
end-to-end BVT coverage that is not present in matrixorigin#26218.

The BVT creates database and table identifiers containing backslashes,
creates
a Data Branch table, and verifies:

- the exact database-name bytes stored in `mo_catalog.mo_snapshots`;
- the exact table-name bytes stored in `mo_catalog.mo_snapshots`;
- the branch snapshot `obj_id` references the source table.

No production code or unit tests are changed.

### Validation

- `make build`
- `protect_11.sql` against rebased commit `e88eb003b`: 13/13 statements
passed
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants