fix(clickhouse): escape SQL identifiers and literals in metadata/DDL paths (#1914)#2174
Open
HandSonic wants to merge 5 commits into
Open
fix(clickhouse): escape SQL identifiers and literals in metadata/DDL paths (#1914)#2174HandSonic wants to merge 5 commits into
HandSonic wants to merge 5 commits into
Conversation
Contributor
Author
|
Converting to draft: final adversarial review pass is still in progress for this branch. Will mark ready for review once it completes. (process note: PR was opened prematurely by automation) |
HandSonic
force-pushed
the
fix/sqli-clickhouse
branch
from
July 26, 2026 10:13
75e522e to
8ac520b
Compare
HandSonic
force-pushed
the
fix/sqli-clickhouse
branch
from
July 26, 2026 10:19
8ac520b to
9bcd7e9
Compare
HandSonic
marked this pull request as ready for review
July 26, 2026 10:28
…ock enum dash rejection (OtterMind#1914)
…r per maintainer review (OtterMind#1914) - new ClickHouseIdentifierProcessor (SPI ISQLIdentifierProcessor): quoteIdentifier with backtick doubling, escapeString with backslash + single-quote doubling - ClickHouseMetaData overrides getSQLIdentifierProcessor(); metadata call sites use it - builders/managers/enums use ClickHouseIdentifierProcessor.INSTANCE - non-escapable validation moved to ClickHouseSqlGuards (column type expressions, engine whitelist, column default expressions) - ClickHouseSqlEscapes removed; tests migrated (24 green)
…-quote for DDL paths (OtterMind#1914) - quoteIdentifier(String) is conditional again: null/blank pass through, plain non-keyword identifiers stay unquoted, everything else is wrapped in backticks with embedded backticks doubled; versioned overload delegates - new quoteIdentifierAlways(String) carries the old unconditional semantics for DDL-generation call sites migrated from ClickHouseSqlEscapes - quoteIdentifierIgnoreCase stays the always-quote, case-preserving variant - override removeIdentifierQuote/isQuoteIdentifier so backtick-quoted identifiers round-trip through completion/matching consumers - add ClickHouse reserved keyword set used by the conditional check - tests cover conditional vs always behavior incl. null passthrough (30 green)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue
Part of the SQL-injection hardening tracked in #1914 (multi-plugin effort; this PR covers ClickHouse). Prior art: #2052 (Oracle
escapeSqlLiteral), #2053 (SQL Server identifier quoting).Summary
These are second-order injection paths: values such as table/schema/view/index names originate from the connected database's own metadata, so exploitation requires a maliciously named object in a target database. The fix is still worthwhile hardening and matches the pattern already merged for Oracle and SQL Server.
What changed:
ClickHouseSqlEscapeshelper:escapeSqlLiteral— doubles backslashes first, then single quotes (ClickHouse treats backslash as an escape character inside string literals).quoteIdentifier/escapeIdentifier— strips one surrounding backtick pair and doubles every embedded backtick for quoted-identifier positions.format()helper was fixed at the source, so all callers now get backtick doubling.ENGINEclause and DEFAULT expressions are guarded by strict whitelist/regex validation;)is forbidden inside the parenthesized argument to block paren-breakout (e.g.Memory() ORDER BY tuple() -- x).'abc') remain supported: the inner content is escaped withescapeSqlLiteralbefore re-quoting, so'a');DROP TABLE t;--'is emitted as a harmless literal.requireColumnTypeExpressionvalidates the declared type shape (letters/digits/underscore; spaces, commas, quotes and=— forEnum8('a'=1)— only inside balanced parentheses) instead of emitting it verbatim. The fallback path keepsNullable(...)wrapping andDEFAULThandling (validated) so unknown-type columns no longer lose them.getByTypeno longer strips digits (which brokeInt32/Int64resolution); it trims and cuts at(before the map lookup.exportFunctionDdlnow quotes the function name inDROP FUNCTION IF EXISTS;buildCreateDatabasequotes the database name via the helper.ClickHouseSqlEscapesTestcovering backslash+quote literal doubling, backtick identifier doubling, malicious-name neutralization, engine/default paren-breakout rejection, quoted string defaults, mixed-case/digit type resolution, and unknown-type fallback validation.Affected surfaces
Verification
mvn -B -pl chat2db-community-plugins/chat2db-community-clickhouse -f chat2db-community-server/pom.xml -Dmaven.test.skip=false -DskipTests=false -Dsurefire.failIfNoSpecifiedTests=false -Dmaven.test.failure.ignore=false clean testIllegalArgumentException.Risk and compatibility
'abc') continue to work.Reviewer map
ClickHouseSqlEscapes.java(new helper, incl.requireColumnTypeExpression) andClickHouseSqlEscapesTest.java.format()helper and its callers;validateEngine/validateDefaultExpression;ClickHouseColumnTypeEnum.getByType/buildCreateColumnSqlSafely;exportFunctionDdlinClickHouseDBManager.Contributor declaration
AI assistance: The fix, verification, and PR description were produced with AI coding assistance.