Repository navigation
Fix SQL injection in connector sample path and escape init queries - #688
vishwasvaidya-cloudsufi wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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.
890fafd to
8e7560e
Compare
a32509a to
6fef409
Compare
6fef409 to
7614b6c
Compare
deepika-lalwani123
left a comment
There was a problem hiding this comment.
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):
- 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). - 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 namedcall. - Behaviour change with no opt-out and no docs. Existing pipelines fail at run time after upgrade, and no
*-batchsource.mdor widget file is updated. - Read-only connection. The connector sample/spec path (
loadTableSchema) still runs a plainStatementin 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 (aSELECTthat 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. CallingvalidateQueries(collector)from bothconfigurePipelineandprepareRunwould be simpler. - Several comments in
SelectQueryValidatordescribe earlier revisions ("They never occur in a sampling query", "instead of two new executors per call"), and the comment aboveWRITE_KEYWORDSsays 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?
7614b6c to
9b11c80
Compare
9b11c80 to
6143d2a
Compare
|
@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
|
deepika-lalwani123
left a comment
There was a problem hiding this comment.
Thanks for splitting the PR, adding the read-only/rollback backstop, fixing the connection leaks, and adding the init-query helpers and tests—this is much cleaner and more focused now.
The core identifier escaping across all 8 connectors and the SET LANGUAGE / SET SESSION sql_mode escaping look solid. Before merging, there are a few important issues in the new read-only transaction / RollbackOnCloseConnection handling and its tests (details inline):
RollbackOnCloseConnection.commit()leak + duplicateROLLBACK: Hadoop'sDBRecordReader.close()runsconnection.commit(); connection.close();with nofinally. Becausecommit()callsgetDelegate().rollback()unguarded, a rollbackSQLExceptionskipsconnection.close()andDriverManager.deregisterDriver(driverShim), and every healthy close issues twoROLLBACKs. ExtendingNoOpCommitConnectionfixes both.- Retries inside
autoCommit=false(AbstractDBSpecificConnector.loadTableSchema): A transient error on attempt 1 leaves PostgreSQL/Redshift-family transactions in an aborted state (25P02) unless rolled back before Failsafe retries. - Post-connect failure cleanup &
setAutoCommit(false)asymmetry (DataDrivenETLDBInputFormat.getConnection): Assigningthis.connectionbefore setup finishes leaks the connection (and caches a half-configured wrapper) if a later step throws, andsetAutoCommit(false)isn't tolerated here the wayconfigureReadOnlyTransactiontolerates it. - Test gaps in
DataDrivenETLDBInputFormatTestandAbstractDBSpecificConnectorReadOnlyTest: Reverting all changes inDataDrivenETLDBInputFormat.javaleaves the suite green, and on HSQLDB 2.2.4 the direct-UPDATEcheck inAbstractDBSpecificConnectorReadOnlyTestfails before executing theUPDATEeven without read-only/rollback.
Also one release question: since 1.12.x is affected as well, which release branch(es) will this be cherry-picked to once merged to develop?
|
Hi @deepika-lalwani123 , |
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 anUPDATE) in default autocommit mode under the connection's stored credential. The same unescaped identifier pattern existed across the other database connectors.(Note: Source query /
splitByvalidation viaSelectQueryValidatorhas 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
quoteIdentifier): Schema, database, table, andstratacolumn identifiers inAbstractDBSpecificConnectorand 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.AbstractDBSpecificConnector.loadTableSchemanow setsconnection.setReadOnly(true), disables auto-commit (when!isAutoCommitEnabled()), and rolls back infinallyon both normal completion and error, closing the opened connections ingetInputFormatProviderandgetTableSchema.getInputFormatProvider->DataDrivenETLDBInputFormat) marks the configuration read-only (ConnectionConfigAccessor.READ_ONLY) and wraps non-autocommit sampling connections inRollbackOnCloseConnectionsoDBRecordReader.close()rolls back instead of committing.SqlServerConstants.buildSetLanguageQueryandMysqlConstants.buildSetSqlModeQueryto escape interpolated string literals across all 8 SQL Server and MySQL source/sink/action/post-action configs.Testing
quoteIdentifier,getTableName, andgetStratifiedQueryacross every connector (including the exact payload from the report).AbstractDBSpecificConnectorReadOnlyTest,RollbackOnCloseConnectionTest) verifying read-only transaction setup, rollback on error/close, fullInputFormat/RecordReadersampling, and that neither a stackedUPDATEin a table path nor a directUPDATEpassed toloadTableSchemamodifies a marker table.SqlServerInitQueriesUnitTest,MysqlInitQueriesUnitTest) pinninggetInitQueries()escaping across all 8 MySQL and SQL Server configs.