Skip to content

Fix SQL injection in connector sample path and escape init queries - #688

Open
vishwasvaidya-cloudsufi wants to merge 1 commit into
developfrom
sql-identifier-escaping
Open

vishwasvaidya-cloudsufi wants to merge 1 commit into
developfrom
sql-identifier-escaping

Conversation

@vishwasvaidya-cloudsufi

@vishwasvaidya-cloudsufi vishwasvaidya-cloudsufi commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What's this about

A VRP report found a SQL injection in the Cloud SQL PostgreSQL connector's sample/spec path. A table path containing a " could break out of the quoted identifier and execute stacked statements (such as an UPDATE) in default autocommit mode under the connection's stored credential. The same unescaped identifier pattern existed across the other database connectors.

(Note: Source query / splitBy validation via SelectQueryValidator has been split out to a follow-up PR per review feedback so this fix can merge without blocking on source-query validation design, compatibility flags, and docs.)

What changed

  • Connector identifier escaping (quoteIdentifier): Schema, database, table, and strata column identifiers in AbstractDBSpecificConnector and all connector overrides (CloudSQLPostgreSQL, PostgreSQL, Redshift, Oracle, SqlServer, MySQL, CloudSQLMySQL, Databricks) now double embedded quote characters (" or `) so user-supplied path/sample parameters cannot break out of the quoted identifier.
  • Read-only connection & rollback defense-in-depth:
    • AbstractDBSpecificConnector.loadTableSchema now sets connection.setReadOnly(true), disables auto-commit (when !isAutoCommitEnabled()), and rolls back in finally on both normal completion and error, closing the opened connections in getInputFormatProvider and getTableSchema.
    • Connector sampling (getInputFormatProvider -> DataDrivenETLDBInputFormat) marks the configuration read-only (ConnectionConfigAccessor.READ_ONLY) and wraps non-autocommit sampling connections in RollbackOnCloseConnection so DBRecordReader.close() rolls back instead of committing.
  • Init queries: Extracted SqlServerConstants.buildSetLanguageQuery and MysqlConstants.buildSetSqlModeQuery to escape interpolated string literals across all 8 SQL Server and MySQL source/sink/action/post-action configs.

Testing

  • Unit tests for quoteIdentifier, getTableName, and getStratifiedQuery across every connector (including the exact payload from the report).
  • Unit + embedded-HSQLDB end-to-end tests (AbstractDBSpecificConnectorReadOnlyTest, RollbackOnCloseConnectionTest) verifying read-only transaction setup, rollback on error/close, full InputFormat/RecordReader sampling, and that neither a stacked UPDATE in a table path nor a direct UPDATE passed to loadTableSchema modifies a marker table.
  • Unit tests (SqlServerInitQueriesUnitTest, MysqlInitQueriesUnitTest) pinning getInitQueries() escaping across all 8 MySQL and SQL Server configs.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces SQL identifier quoting to prevent SQL injection vulnerabilities across various database connectors, and adds query validation using jsqlparser to ensure that import and bounding queries are read-only SELECT statements. Feedback on these changes highlights several potential NullPointerException risks when database or schema parameters are null during identifier quoting, as well as a recommendation to use Locale.ROOT with toLowerCase() to avoid locale-sensitive string comparison issues.

@deepika-lalwani123 deepika-lalwani123 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough work here. The identifier escaping (quoteIdentifier in every getTableName / getStratifiedQuery) looks correct to me and the per-connector tests pin it well.

My concerns are with SelectQueryValidator. I compiled the class from this PR's head with JSqlParser 4.9 and ran a set of queries through it. I did not run anything against a real database, so where I say what a server does with a query, that is from the dialect's documented behaviour, not from a test.

Summary (details inline):

  1. It accepts some queries that are not a single read-only SELECT (-- without a following space on MySQL, a line comment ended by a carriage return, lowercase keywords under a Turkish default locale, non-ASCII whitespace, statements and locking clauses that are not on the deny-list).
  2. It rejects ordinary read-only queries well beyond the three constructs listed in the description, for example WHERE grade = 'Q', LIKE '%q', WHERE code = '$A$', LIKE '%\_%' ESCAPE '\', and an unquoted column named call.
  3. Behaviour change with no opt-out and no docs. Existing pipelines fail at run time after upgrade, and no *-batchsource.md or widget file is updated.
  4. Read-only connection. The connector sample/spec path (loadTableSchema) still runs a plain Statement in autocommit mode. Setting the connection read-only with autocommit off and rolling back would be a database-enforced backstop for both the connector path and the source schema fetch, and it covers cases a lexical scan cannot (a SELECT that calls a writing function).

Suggestion: could this be split in two? The identifier and init-query escaping could merge on its own, and the validator could follow in a second PR once the points above are addressed. That would keep the escaping fix from waiting on the validator.

A few smaller things that are not inline:

  • The import query is validated three times at configure time (configurePipeline, getSchema, loadSchemaFromDB), while a non-macro bounding query is not validated until run time. Calling validateQueries(collector) from both configurePipeline and prepareRun would be simpler.
  • Several comments in SelectQueryValidator describe earlier revisions ("They never occur in a sampling query", "instead of two new executors per call"), and the comment above WRITE_KEYWORDS says any keyword followed by ( is exempt, which is not what the code does.
  • Quoting the strata column makes it case-sensitive on databases that fold unquoted names. Does the caller always send the exact column name?

if (!Strings.isNullOrEmpty(currentLanguage)) {
return Collections.singletonList(String.format(SqlServerConstants.SET_LANGUAGE_QUERY_FORMAT, currentLanguage));
return Collections.singletonList(
String.format(SqlServerConstants.SET_LANGUAGE_QUERY_FORMAT, currentLanguage.replace("'", "''")));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the eight getInitQueries changes (four here, four in the MySQL plugin) has a test; removing the .replace(...) at any site leaves the suite green. A small test per config that asserts the exact query for a value containing a quote (and, for MySQL, a backslash) would pin it.

Since the same expression is pasted eight times, a helper next to SET_LANGUAGE_QUERY_FORMAT / SET_SQL_MODE_QUERY_FORMAT would also give one place for a comment on why MySQL doubles the backslash and SQL Server does not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — extracted SqlServerConstants.buildSetLanguageQuery and MysqlConstants.buildSetSqlModeQuery (with Javadoc explaining why MySQL escapes backslashes before single quotes whereas T-SQL only doubles single quotes) and added SqlServerInitQueriesUnitTest and MysqlInitQueriesUnitTest covering all eight configs.

@vishwasvaidya-cloudsufi vishwasvaidya-cloudsufi changed the title Fix SQL injection in connector sample path and validate source queries Fix SQL injection in connector sample path and escape init queries Nov 21, 2026
@vishwasvaidya-cloudsufi

Copy link
Copy Markdown
Contributor Author

@deepika-lalwani123 Thanks for the thorough review! Following your suggestion, I've split this into two PRs so the connector and init-query fix can merge independently while I address all the SelectQueryValidator / AbstractDBSource points in a follow-up PR:

  1. Split: Removed SelectQueryValidator, jsqlparser, and all AbstractDBSource changes from this PR onto a separate branch where I'll address all of your lexer/parser findings, false positives, validation call sites (validateQueries(collector) in configurePipeline and prepareRun), connection-level compatibility opt-out, stale comments, and *-batchsource.md docs.
  2. Read-only connection backstop (loadTableSchema & connector sampling): Added on the connector path in this PR — AbstractDBSpecificConnector.loadTableSchema now sets connection.setReadOnly(true), disables auto-commit (when !isAutoCommitEnabled()), and rolls back in finally (and closes the connections opened in getInputFormatProvider and getTableSchema). Connector row sampling via DataDrivenETLDBInputFormat also sets setReadOnly(true) and wraps non-autocommit connections in RollbackOnCloseConnection so DBRecordReader.close() rolls back instead of committing. Added unit + embedded-HSQLDB tests in AbstractDBSpecificConnectorReadOnlyTest and RollbackOnCloseConnectionTest.
  3. getInitQueries helper & tests: Extracted SqlServerConstants.buildSetLanguageQuery and MysqlConstants.buildSetSqlModeQuery with comments explaining why MySQL escapes backslashes while SQL Server only doubles single quotes, and added SqlServerInitQueriesUnitTest and MysqlInitQueriesUnitTest covering all 8 configs.
  4. Strata column case-sensitivity: In the Studio/Wrangler UI flow, the caller populates strata from the connector schema (CommonSchemaReader.getSchemaFields), which uses ResultSetMetaData.getColumnName(i) — i.e., the exact stored column name case (e.g., ID on Oracle, id on PostgreSQL). Quoting also fixes mixed-case column names that previously failed when unquoted, and matches how schema and table in the connector path are already quoted in getTableName.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants