Repository navigation
Conversation
Previously, column names passed to JDBC generated-keys methods were wrapped in quotes without escaping embedded quotes, backslashes, or control characters. This allowed specially crafted column names to break out of the identifier and inject arbitrary SQL into the generated `THEN RETURN` (GoogleSQL) or `RETURNING` (PostgreSQL) clause. In addition, a user-provided column literally named `*` was treated as a wildcard (`RETURN_GENERATED_KEYS`), and `null`, empty, or NUL-containing column names could trigger an uncaught `NullPointerException` or invalid SQL. This change: - Adds dialect-aware identifier quoting and escaping in the statement parsers (escaping backticks, backslashes, and control characters for GoogleSQL, and doubling double quotes for PostgreSQL). - Quotes and escapes all explicit generated-key column names in the JDBC driver rather than interpolating raw strings. - Distinguishes `Statement.RETURN_GENERATED_KEYS` from explicit column lists with a dedicated representation so a literal `*` column name is quoted rather than expanded into a wildcard. - Eagerly rejects `null`, empty, and NUL-containing column names with an `INVALID_ARGUMENT` SQL exception (after checking that the connection or statement is not closed). - Adds unit, mock-server, and integration tests covering identifier escaping, injection payloads, literal `*`, and invalid inputs across both GoogleSQL and PostgreSQL dialects.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces the GeneratedKeysColumns class to manage and validate generated-key columns for JDBC statement executions. It refactors JdbcConnection, JdbcPreparedStatement, and JdbcStatement to utilize this new class. Additionally, it implements dialect-specific SQL identifier quoting and escaping within the statement parsers (AbstractStatementParser, PostgreSQLStatementParser, and SpannerStatementParser) to safely handle special characters and prevent SQL injection. Comprehensive unit and integration tests have been added to verify these changes. There are no review comments to address, and the implementation looks solid.
This branch has not been deployed
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.
Previously, column names passed to JDBC generated-keys methods were wrapped in quotes without escaping embedded quotes, backslashes, or control characters. This allowed specially crafted column names to break out of the identifier and inject arbitrary SQL into the generated
THEN RETURN(GoogleSQL) orRETURNING(PostgreSQL) clause.In addition, a user-provided column literally named
*was treated as a wildcard (RETURN_GENERATED_KEYS), andnull, empty, or NUL-containing column names could trigger an uncaughtNullPointerExceptionor invalid SQL.This change:
Statement.RETURN_GENERATED_KEYSfrom explicit column lists with a dedicated representation so a literal*column name is quoted rather than expanded into a wildcard.null, empty, and NUL-containing column names with anINVALID_ARGUMENTSQL exception (after checking that the connection or statement is not closed).*, and invalid inputs across both GoogleSQL and PostgreSQL dialects.