Repository navigation
Fix SQL injection in connector sample path and escape init queries - #688
vishwasvaidya-cloudsufi wants to merge 1 commit 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?
| 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("'", "''"))); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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
|
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.