fix(db2): escape SQL identifiers and literals in metadata/DDL paths (#1914)#2173
Open
HandSonic wants to merge 6 commits into
Open
fix(db2): escape SQL identifiers and literals in metadata/DDL paths (#1914)#2173HandSonic wants to merge 6 commits into
HandSonic wants to merge 6 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) |
…aths (OtterMind#1914) - buildCreateColumnSql fallback: validate columnType against a strict type-shape whitelist and escape the column name as a delimited identifier instead of delegating to the raw SPI buildDefaultColumn - tableDDL: reject schema/table names containing double quotes, which would otherwise break out of the db2look -td/-t option string - add regression tests for both paths plus SET SCHEMA, dropTable and copyTable escaping composition
…t validation (OtterMind#1914) - DB2SqlBuilder: override buildDropTable/buildTruncateTable so qualified table names are quoted via Db2SqlEscapes instead of the SPI identity - DB2DBManager: override truncateTable with the same quoting - DB2ColumnTypeEnum.getByType: strip size suffix so VARCHAR(10) resolves to the enum and reaches the validated fallback instead of being skipped - DB2ColumnTypeEnum: drop parens/comma from DEFAULT_VALUE_PATTERN to block column-definition breakout via crafted default values - add regression tests for all four paths
HandSonic
marked this pull request as ready for review
July 26, 2026 10:28
…ayloads (OtterMind#1914) - DEFAULT_VALUE_PATTERN: drop single-quote from the allowed class so crafted defaults cannot toggle string-literal context inside the enclosing CREATE/ALTER statement - anchor DEFAULT_VALUE_PATTERN, UNIT_PATTERN and FALLBACK_COLUMN_TYPE_PATTERN with \A/\z instead of ^/$ so a trailing newline can no longer smuggle input past validation - validateAscOrDesc needs no anchor change: it uses exact string equality, which is already absolute - add rejection tests for quote/comma/paren breakout defaults, trailing-newline bypasses, and GET_DDL_TOKEN option-string quoting
HandSonic
marked this pull request as draft
July 26, 2026 10:57
HandSonic
marked this pull request as ready for review
July 26, 2026 11:33
…er review (OtterMind#1914) - new Db2IdentifierProcessor (SPI ISQLIdentifierProcessor): quoteIdentifier with double-quote doubling, escapeString with single-quote doubling - DB2MetaData overrides getSQLIdentifierProcessor(); metadata call sites use it - builders/managers/enums use Db2IdentifierProcessor.INSTANCE - non-escapable validation moved to Db2SqlGuards (default values, length units, fallback column types, index sort directions) - Db2SqlEscapes removed; tests migrated (23 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.
Summary
Fixes the DB2 plugin portion of #1914 (34 SQL-injection findings across database plugins). All identifier/literal interpolation in the DB2 plugin's metadata, DDL-builder, and export paths now goes through a new
Db2SqlEscapeshelper (single-quote doubling for string literals, double-quote doubling + wrapping for delimited identifiers), and free-form inputs that cannot be safely escaped (column default values, length units, index sort direction, fallback column types) are validated against strict whitelists.Changed paths:
DB2MetaData:tableDDL(db2look token call),indexes,view,function,procedure,getMetaDataNameDB2DBManager:exportTable/Views/Procedures/Triggers,connectDatabase(SET SCHEMA),dropTable,copyTable,truncateTableDB2SqlBuilder:buildCreateTable/buildAlterTable/buildCreateSchemaincl. comments, plus newbuildDropTable/buildTruncateTableoverrides (SPI defaults interpolated raw names)DB2ColumnTypeEnum: column name escaping; strictDEFAULTvalue pattern; length-unit whitelist; safe fallback for sized types likeVARCHAR(10)that miss the exact enum map (previously delegated to raw SPIbuildDefaultColumn— the one exploitable path found in adversarial review)DB2IndexTypeEnum: index/constraint names and comments escaped;ASC/DESCexact-match validationTwo defense-in-depth measures beyond plain escaping:
tableDDLrejects schema/table names containing", because the names are embedded in a double-quoted db2look option string (-td "%s" -t "%s") where quote-doubling is not a valid neutralization.\A/\z(Java$matches before a trailing newline).Trade-offs
DEFAULTpattern (\A[A-Za-z0-9_+/: \t.-]+\z) rejects quoted string literals such as'abc'; string defaults remain available via the existingEMPTY_STRING/NULLkeywords. This was an explicit reviewer request to prevent literal-context toggling inside enclosing CREATE/ALTER statements.LETTERS,LETTERS(n),LETTERS(n,n); anything else throwsIllegalArgumentExceptioninstead of being interpolated.truncateTableemitsTRUNCATE TABLE "x"withoutIMMEDIATE(matches the SPI shape; DB2 LUW may requireIMMEDIATEin some contexts — functional nit, not a security issue).Residual risk
IColumnBuilder.buildDefaultColumnstill concatenates name/type raw; the DB2 plugin no longer calls it, but other plugins still do. Out of scope for this PR (plugin-level fixes only, to keep the per-plugin PRs independent); tracked separately.buildPageLimit/buildExplainconcatenate the user's own SQL by design (SQL-editor flow), unchanged.SYSPROC.DB2LK_GENERATE_DDL; JDBC-level injection is closed (single quotes doubled) and"is rejected up front, but the procedure's internal option parsing is not auditable here.Test evidence
Db2SqlEscapesTest: 23/23 pass on a clean build (mvn -pl chat2db-community-plugins/chat2db-community-db2 test), covering every changed path with real attack strings:x'; DROP TABLE t; --,bad"name,0; DROP TABLE t,OCTETS) DROP TABLE t,1), (evil INT DEFAULT (0,x' OR '1'='1, trailing-newline bypasses (0\n,VARCHAR(10)\n,OCTETS\n,ASC\n), db2look option injection (x" -t "y), and sized-type fallback (VARCHAR(10)).IllegalArgumentException.