fix(mysql): escape SQL identifiers and literals in metadata/DDL paths (#1914) - #2172
Open
HandSonic wants to merge 5 commits into
Open
fix(mysql): escape SQL identifiers and literals in metadata/DDL paths (#1914)#2172HandSonic 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) |
…processor (OtterMind#1914) - MysqlDmlValueTemplate.wrapHex: require hex digits for the 0x template, matching wrapBit's requireBitLiteral - MysqlBinaryProcessor/MysqlVarBinaryProcessor: only pass values through raw when they are well-formed 0x hex literals (isHexLiteral), closing the 0x-prefixed injection passthrough in generated DML - MysqlIdentifierProcessor.quoteIdentifier: double embedded backticks via MysqlSqlEscapes.quoteIdentifierRaw instead of wrapping verbatim - add regression tests for all three paths
This was referenced Jul 26, 2026
…tainer review (OtterMind#1914) - MysqlIdentifierProcessor (SPI ISQLIdentifierProcessor): INSTANCE, always-quote backtick quoteIdentifier with one-pair strip + doubling, escapeString with backslash-first-then-quote doubling, static escapeIdentifier for quoted templates - MysqlMetaData call sites use getSQLIdentifierProcessor(); builders/managers/enums/ value processors use MysqlIdentifierProcessor.INSTANCE - non-escapable validation moved to MysqlSqlGuards (engine/charset/collation names, numeric defaults, bit/hex literals, definers, asc/desc, enum constants, enum/set lists) - MysqlSqlEscapes removed; tests migrated to MysqlIdentifierProcessorTest (630 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 MySQL). 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:
MysqlSqlEscapeshelper:escapeSqlLiteral— doubles backslashes first, then single quotes (mirrors the existingMysqlAccountSqlBuilder.stringLiteralbehavior for MySQL string literals).quoteIdentifier/escapeIdentifier— strips one surrounding backtick pair and doubles every embedded backtick for quoted-identifier positions.ENGINE/CHARSET/COLLATEvia arequireMysqlNamewhitelist ([A-Za-z0-9_]+-style names).DEFAULTvia numeric/expression pattern validation; bit literals validated as[01]+; hex literals in generated DML (0x...template and binary/varbinary processors) validated as well-formed hex, closing a0x-prefixed injection passthrough.ENUM/SETvalues are escaped as literals.MysqlIdentifierProcessor.quoteIdentifierdoubles embedded backticks instead of wrapping verbatim.dropViewqualified name.MysqlSqlEscapesTestcovering backslash+quote literal doubling, backtick identifier doubling, malicious-name neutralization, and whitelist rejections.Affected surfaces
Verification
mvn -B -pl chat2db-community-plugins/chat2db-community-mysql -f chat2db-community-server/pom.xml -Dmaven.test.skip=false -DskipTests=false -Dsurefire.failIfNoSpecifiedTests=false -Dmaven.test.failure.ignore=false clean testMysqlSqlEscapesTestand all pre-existing tests).IllegalArgumentException.Risk and compatibility
Reviewer map
MysqlSqlEscapes.java(new helper) andMysqlSqlEscapesTest.java.requireMysqlNamewhitelist sites (ENGINE/CHARSET/COLLATE); DEFAULT and bit-literal validation inMysqlColumnTypeEnum; thedropViewdot-separator fix.Contributor declaration
AI assistance: The fix, verification, and PR description were produced with AI coding assistance.