Repository navigation
fix: harden warehouse SQL identifier quoting - #7249
achettyiitr wants to merge 18 commits into
Conversation
🔒 Scanned for secrets using gitleaks 8.29.1
🔒 Scanned for secrets using gitleaks 8.29.1
🔒 Scanned for secrets using gitleaks 8.29.1
🔒 Scanned for secrets using gitleaks 8.29.1
🔒 Scanned for secrets using gitleaks 8.29.1
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #7249 +/- ##
==========================================
- Coverage 80.27% 79.68% -0.59%
==========================================
Files 600 601 +1
Lines 67034 67165 +131
==========================================
- Hits 53811 53521 -290
- Misses 10070 10467 +397
- Partials 3153 3177 +24 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
🔒 Scanned for secrets using gitleaks 8.29.1
725cb17 to
8cb5416
Compare
There was a problem hiding this comment.
test cases missing for bigquery
There was a problem hiding this comment.
BigQuery's CreateTable, AddColumns, and DropTable all go through the typed Go client API (Dataset().Table().Create/Update/Delete) where names are struct fields, not SQL — so those paths are structurally immune to SQL injection, and BQ's API rejects exotic identifier characters outright. The real SQL injection surface in BQ is the raw-SQL paths that interpolate attacker-controlled column names via backtick quoting: the dedup view and the users merge. Adding tests for those.
| func DoubleQuoteAndJoinByComma(elems []string) string { | ||
| var quotedSlice []string | ||
| func DoubleQuoteIdentifier(identifier string) string { | ||
| return `"` + strings.ReplaceAll(identifier, `"`, `""`) + `"` |
There was a problem hiding this comment.
Heads-up: this is a regression for ClickHouse specifically.
ClickHouse applies string-literal escaping rules to double-quoted identifiers, so \ is an escape character there. The fmt.Sprintf("%q", …) this replaces happened to match that convention exactly (" → \", \ → \\), so it was already safe on ClickHouse. Doubling handles " but passes \ through untouched:
| column name | old %q |
new |
|---|---|---|
evil\ |
"evil\\" → identifier evil\ ✅ |
"evil\" → closing quote is escaped, rest of the column list runs on 💥 |
Reachable via ColumnsWithDataTypes (clickhouse.go:355, used by CreateTable/CreateSchema) and AddColumns (clickhouse.go:987).
The split is: doubling is correct for Postgres/Redshift/Snowflake/MSSQL/Spark, backslash is correct for ClickHouse and BigQuery — so one shared escaper can't be right for all of them. BacktickQuoteIdentifier has the same mismatch for BigQuery (which escapes as \` rather than by doubling); not a regression there since master interpolated raw, but it leaves the \ breakout open.
Missing test: the cases below only cover the quote character. Worth adding a backslash case:
require.Equal(t, `"evil\\"`, DoubleQuoteIdentifier(`evil\`)) // currently `"evil\"`
require.Equal(t, "`evil\\\\`", BacktickQuoteIdentifier(`evil\`))…though the assertion depends on which fix you pick (escape \ as \\, or make the helper dialect-specific).
| } | ||
|
|
||
| func SQLStringLiteral(value string) string { | ||
| return `'` + strings.ReplaceAll(value, `'`, `''`) + `'` |
There was a problem hiding this comment.
Only ' is escaped here, but Snowflake, BigQuery, ClickHouse and Databricks/Spark also honour \ inside string literals — so the doubling can be defeated. An input containing \' becomes \'': the dialect reads \' as a literal quote and the next ' closes the literal, so everything after it is parsed as SQL.
Reachable via operator/config-supplied values at deltalake.go:578 (externalLocation), clickhouse.go:625 (loadFolder), snowflake/table_manager.go:97 (externalVolume), bigquery.go:818.
Doubling alone is correct for Postgres/Redshift/MSSQL/Synapse, so this probably wants to be dialect-specific rather than one shared escaper.
| '%[6]s', | ||
| 'CSV', | ||
| '%[7]s', | ||
| '%[6]s', |
There was a problem hiding this comment.
structure is a string literal here, not an identifier list — s3(url, key, secret, format, structure, compression).
sortedColumnNamesWithDataTypes (L588-590) is escaped for identifier position (DoubleQuoteIdentifier doubles "), then interpolated raw between single quotes. Nothing escapes ', so a column named a'b gives:
'CSV', '"a'b" String,…', 'gz'
↑ literal ends hereThe same helper feeds %[2]s at L605 where identifier quoting is correct — it's only wrong in this slot. %[4]s/%[5]s (accessKeyID/secretAccessKey, L626-627) are raw inside '…' too.
Suggested fix — nest the escaping:
warehouseutils.SQLStringLiteral(sortedColumnNamesWithDataTypes), // 6Not a regression (master built this fragment unquoted too), just the one interpolation left unhardened inside the statement this hunk hardens. Only fully closed once SQLStringLiteral handles \ as well — see the comment on that helper.
| `, | ||
| bq.namespace, | ||
| fmt.Sprintf(`%s%%`, warehouseutils.StagingTablePrefix(provider)), | ||
| warehouseutils.BacktickQuoteQualifiedIdentifier(bq.namespace, "INFORMATION_SCHEMA", "TABLES"), |
There was a problem hiding this comment.
Worth confirming against a live dataset before merge — I couldn't verify this one statically.
This now emits FROM `ns`.`INFORMATION_SCHEMA`.`TABLES` (same at L818). BigQuery documents the path as dataset.INFORMATION_SCHEMA.VIEW and its examples only backtick the region/project component. If ZetaSQL rejects the per-part quoted form, FetchSchema fails on every BigQuery sync.
Flagging it because nothing here would catch it: bigquery_test.go is untouched by this PR and TestIntegration is gated behind SLOW=1 + real credentials — BigQuery is the only destination in this PR shipping with no test coverage.
| func (ms *MSSQL) createTable(ctx context.Context, tableName string, columns model.TableSchema) (err error) { | ||
| tableIdentifier := warehouseutils.BracketQuoteQualifiedIdentifier(ms.namespace, tableName) | ||
| sqlStatement := fmt.Sprintf(`IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N%s) AND type = N'U') | ||
| CREATE TABLE %s ( %v )`, warehouseutils.SQLStringLiteral(ms.namespace+"."+tableName), tableIdentifier, ColumnsWithDataTypes(columns, "")) |
There was a problem hiding this comment.
The CREATE TABLE target is now bracket-quoted but the existence guard still uses the raw concatenated name. On master both sides used the same unquoted string so they agreed; now they diverge.
For names that actually need bracket quoting (., ], spaces), OBJECT_ID(N'ns.tbl') returns NULL → IF NOT EXISTS always passes → the CREATE fails with "There is already an object named …".
Same raw-name guard elsewhere:
AddColumnsL858 (and azure-synapse.go:865) — fails open, soALTER TABLE … ADDruns on an already-existing column- azure-synapse.go:823 — same as here
- azure-synapse.go:811
dropStagingTable— fails closed, staging table silently leaked
🔒 Scanned for secrets using gitleaks 8.29.1
4786d7d to
7e8cbae
Compare
|
Individual warehouse specific PR's will be raised later on |
Address review feedback from #7249: - escape backslash as well as backtick in quoted identifiers, closing the trailing-backslash breakout - bind INFORMATION_SCHEMA filters as query parameters instead of string literals, whose escaping differs in GoogleSQL - extract the users merge SQL into usersMergeQuery and cover it with tests - drop the unused quoteIdentifiers helper that failed lint Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
- DeleteBy quoted lowercase column names, which Snowflake treats as case-sensitive and rejects (invalid identifier); apply ToProviderCase as before - escape backslash in string literals and quote EXTERNAL_VOLUME and COPY load locations (review feedback from #7249) - keep the Iceberg BASE_LOCATION path identical to master - fix gci import grouping Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: Spark SQL treats backslash as an escape character and does not treat a doubled single quote as an escaped quote, so escape both with a backslash. Also quote the COPY INTO load folder literals. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable and AddColumns existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: - escape backslash as well as backtick in quoted identifiers, closing the trailing-backslash breakout - bind INFORMATION_SCHEMA filters as query parameters instead of string literals, whose escaping differs in GoogleSQL - extract the users merge SQL into usersMergeQuery and cover it with tests - drop the unused quoteIdentifiers helper that failed lint Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
- DeleteBy quoted lowercase column names, which Snowflake treats as case-sensitive and rejects (invalid identifier); apply ToProviderCase as before - escape backslash in string literals and quote EXTERNAL_VOLUME and COPY load locations (review feedback from #7249) - keep the Iceberg BASE_LOCATION path identical to master - fix gci import grouping Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: Spark SQL treats backslash as an escape character and does not treat a doubled single quote as an escaped quote, so escape both with a backslash. Also quote the COPY INTO load folder literals. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable and AddColumns existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: - escape backslash as well as backtick in quoted identifiers, closing the trailing-backslash breakout - bind INFORMATION_SCHEMA filters as query parameters instead of string literals, whose escaping differs in GoogleSQL - extract the users merge SQL into usersMergeQuery and cover it with tests - drop the unused quoteIdentifiers helper that failed lint Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
- DeleteBy quoted lowercase column names, which Snowflake treats as case-sensitive and rejects (invalid identifier); apply ToProviderCase as before - escape backslash in string literals and quote EXTERNAL_VOLUME and COPY load locations (review feedback from #7249) - keep the Iceberg BASE_LOCATION path identical to master - fix gci import grouping Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: Spark SQL treats backslash as an escape character and does not treat a doubled single quote as an escaped quote, so escape both with a backslash. Also quote the COPY INTO load folder literals. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable, AddColumns and dropStagingTable existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable, AddColumns and dropStagingTable existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable and AddColumns existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: - escape backslash as well as backtick in quoted identifiers, closing the trailing-backslash breakout - bind INFORMATION_SCHEMA filters as query parameters instead of string literals, whose escaping differs in GoogleSQL - extract the users merge SQL into usersMergeQuery and cover it with tests - drop the unused quoteIdentifiers helper that failed lint Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
- DeleteBy quoted lowercase column names, which Snowflake treats as case-sensitive and rejects (invalid identifier); apply ToProviderCase as before - escape backslash in string literals and quote EXTERNAL_VOLUME and COPY load locations (review feedback from #7249) - keep the Iceberg BASE_LOCATION path identical to master - fix gci import grouping Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: Spark SQL treats backslash as an escape character and does not treat a doubled single quote as an escaped quote, so escape both with a backslash. Also quote the COPY INTO load folder literals. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: Spark SQL treats backslash as an escape character and does not treat a doubled single quote as an escaped quote, so escape both with a backslash. Also quote the COPY INTO load folder literals. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
- DeleteBy quoted lowercase column names, which Snowflake treats as case-sensitive and rejects (invalid identifier); apply ToProviderCase as before - escape backslash in string literals and quote EXTERNAL_VOLUME and COPY load locations (review feedback from #7249) - keep the Iceberg BASE_LOCATION path identical to master - fix gci import grouping Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable, AddColumns and dropStagingTable existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable and AddColumns existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: - escape backslash as well as backtick in quoted identifiers, closing the trailing-backslash breakout - bind INFORMATION_SCHEMA filters as query parameters instead of string literals, whose escaping differs in GoogleSQL - extract the users merge SQL into usersMergeQuery and cover it with tests - drop the unused quoteIdentifiers helper that failed lint Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: - escape backslash as well as backtick in quoted identifiers, closing the trailing-backslash breakout - bind INFORMATION_SCHEMA filters as query parameters instead of string literals, whose escaping differs in GoogleSQL - extract the users merge SQL into usersMergeQuery and cover it with tests - drop the unused quoteIdentifiers helper that failed lint Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable, AddColumns and dropStagingTable existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: Spark SQL treats backslash as an escape character and does not treat a doubled single quote as an escaped quote, so escape both with a backslash. Also quote the COPY INTO load folder literals. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
Address review feedback from #7249: assert that the createTable and AddColumns existence guards use the same bracket-quoted name as the statement they protect. Also fix gci import grouping. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
- DeleteBy quoted lowercase column names, which Snowflake treats as case-sensitive and rejects (invalid identifier); apply ToProviderCase as before - escape backslash in string literals and quote EXTERNAL_VOLUME and COPY load locations (review feedback from #7249) - keep the Iceberg BASE_LOCATION path identical to master - fix gci import grouping Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> 🔒 Scanned for secrets using gitleaks 8.29.1
# Description ClickHouse was deliberately left out of the per-integration identifier quoting work until the v2 driver landed. Now that v1 is gone and v2 is the only implementation, this applies the same treatment. ClickHouse SQL was built by interpolating database, table and column names with Go's `%q`, and by embedding the load folder, credentials and the `s3()` structure argument in raw `'…'` literals. `%q` is Go string quoting rather than SQL quoting, and nothing escaped the values placed inside the string literals. **Helpers** (`warehouse/utils/identifier.go`): `ClickHouseQuoteIdentifier`, `ClickHouseQuoteQualifiedIdentifier` and `ClickHouseQuoteAndJoinByComma`. ClickHouse applies string-literal escape rules inside double-quoted identifiers, so the delimiter and the backslash are escaped with a backslash rather than doubled — unlike Postgres, Redshift and Snowflake. String literals reuse `SQLStringLiteralBackslash`, which escapes `\` and `'`. **Statements routed through them:** - `CreateSchema` (`CREATE DATABASE`), `ON CLUSTER`, and `CREATE TABLE` for both the ReplacingMergeTree and AggregatingMergeTree paths - column definitions (`ColumnsWithDataTypes`) and the `ORDER BY` sort key tuple - `AddColumns` (`ALTER TABLE … ADD COLUMN`), `DropTable`, and the row count query - both `INSERT` paths, including the one that previously used `DoubleQuoteAndJoinByComma`, which doubles the quote and is therefore wrong for ClickHouse - the `s3()` copy statement: the column list is quoted, and the load folder, credentials and the `structure` argument are escaped as string literals. The structure argument is a string literal holding column definitions, so its identifiers are quoted first and the whole value is then escaped — the nesting #7249 called out. The partition expression still uses the internal `received_at` constant unquoted, so the generated `PARTITION BY` is unchanged. ## Tests - `clickhouse_quoting_internal_test.go`: column definitions neutralize breakout payloads, including a trailing backslash, which a doubling escaper would get wrong; and the `s3()` copy statement quotes its column list and escapes the folder, credentials and structure argument. - `warehouse/utils/identifier_test.go`: ClickHouse identifier, qualified identifier and comma-joined list escaping, including the all-delimiters value used by the other dialects. - Integration test `TestIntegration` → "prevents SQL injection via malicious identifiers": creates a database, table and columns whose names carry `"` breakout payloads (plus a trailing-backslash column) alongside a control table, then verifies the control table survives, the names round-trip verbatim through `FetchSchema`, and `DropTable` drops only the targeted table. ## Linear Ticket Resolves INT-6791 ## Security - [x] The code changed/added as part of this pull request won't create any security issues with how the software is being used.
Description
Summary
warehouse/utils: dialect quoters (double-quote, backtick, bracket), qualified-name and comma-separated joiners, a composite-key splitter (QuoteCommaSeparatedIdentifiers), and a string-literal escaper (SQLStringLiteral). Every quoter escapes by doubling its delimiter —"→"",`→``,]→]],'→''.%qinterpolation of schema, table, column, and staging-table names.TestLoadTable, and BigQuery view/delete SQL.row_id, column_name, table_name) into individually quoted columns instead of quoting the whole string as one identifier.schemaExistswith bind parameters.Linear Ticket
Security