Skip to content

fix(spanner-jdbc): escape generated-key column names - #15319

Open
olavloite wants to merge 2 commits into
mainfrom
spanner-jdbc-escape-generated-key-cols
Open

olavloite wants to merge 2 commits into
mainfrom
spanner-jdbc-escape-generated-key-cols

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

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.

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.
@olavloite
olavloite requested review from a team as code owners December 1, 2026 12:49

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

Copy link
Copy Markdown
Contributor

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 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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant