Repository navigation
fix: escape SQL string literals to prevent injection in Data Branch operations - #26218
Conversation
…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 reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 3 hours 48 minutes 43 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonThe merge conditions cannot be satisfied due to failing checks Failing checks:
HintYou may have to fix your CI before adding the pull request to the queue again. Requeued — the merge queue status continues in this comment ↓. |
## 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>
…#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
…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
…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>
…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
…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>
…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
…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>
What type of PR is this?
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 语法错误或潜在的注入攻击。修复内容
createBranchProtectSnapshot中对所有字符串值使用quoteSQLStringLiteral()进行转义quoteSQLStringLiteral()辅助函数,并在GetSqlForFkReferredTo()中应用checkTableIsMaster()和checkDatabaseIsMaster()中应用转义quoteSQLStringLiteral()函数将单引号转义为双单引号(''),将反斜杠转义为双反斜杠(\\),然后用引号包裹结果,遵循标准 SQL 字符串字面量转义规则。验证
已在本地验证:
src'test)成功mo_snapshots表中的数据正确插入已知限制
DATA BRANCH CREATE 语句在解析带单引号的表名时存在独立的问题(查找表时错误地将
src'test转换为src''test),这是一个独立的 bug,不在本 PR 的修复范围内。