Skip to content

fix(mysql): quote charset/collation/time_zone in connect-time SET - #4496

Open
Nacl-HCl wants to merge 1 commit into
transact-rs:mainfrom
Nacl-HCl:fix-mysql-connect-set-quoting-4491
Open

Nacl-HCl wants to merge 1 commit into
transact-rs:mainfrom
Nacl-HCl:fix-mysql-connect-set-quoting-4491

Conversation

@Nacl-HCl

@Nacl-HCl Nacl-HCl commented Nov 1, 2026

Copy link
Copy Markdown

Does your PR solve an issue?

fixes #4491

Is this a breaking change?

No. SET NAMES 'charset' [COLLATE 'collation'] with quoted string literals is valid MySQL syntax with identical semantics to the unquoted form; the generated statement only differs in quoting.


charset, collation and time_zone come from the connection URL (or the builder methods) and were interpolated into the connect-time SET statement without quoting (charset/collation not quoted at all, time_zone wrapped in single quotes but not escaped). Since the connection enables CLIENT_MULTI_STATEMENTS, a crafted value like time_zone='; <arbitrary SQL>; -- could inject a second statement.

This quotes all three as MySQL string literals via a new quote_string_literal() helper (single quotes are doubled, backslashes escaped), and extracts statement building into MySqlConnectOptions::connect_init_statement() so it can be unit-tested without a live connection.

How verified:

  • New regression tests fail on the old interpolation and pass with the fix (injection payloads for all three parameters are neutralized inside the literal).
  • cargo test -p sqlx-mysql --lib options::: 12 passed, 0 failed.
  • cargo fmt --check clean; cargo clippy --all-targets clean for the touched file.
  • Integration tests requiring a live MySQL server were not run locally (no DB in this environment); statement-level correctness is covered by the unit tests.

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.

sqlx-mysql: charset/collation/timezone are interpolated unquoted into the connect-time SET (multi-statements on)

1 participant