Skip to content

Escape PostgreSQL options #3800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

V02460
Copy link

@V02460 V02460 commented Mar 21, 2025

Properly escape PostgreSQL options containing spaces and backslash characters as specified under https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-OPTIONS.

Fixes #2027

Is this a breaking change?

No, this change fixes behavior that I consider a minor edge case.

Yes,

anyone who's already manually escaping options will get different, likely incorrect, results after this patch

@abonander
Copy link
Collaborator

This is technically a breaking behavior change as well, because anyone who's already manually escaping options will get different, likely incorrect, results after this patch.

This should also document that the strings will be escaped:

/// Set additional startup options for the connection as a list of key-value pairs.
///
/// # Example
///
/// ```rust
/// # use sqlx_postgres::PgConnectOptions;
/// let options = PgConnectOptions::new()
/// .options([("geqo", "off"), ("statement_timeout", "5min")]);
/// ```

Alternatively, we don't do this internally but instead expose PgEscapeOption and direct people to use it. That would be backwards-compatible.

@V02460
Copy link
Author

V02460 commented Mar 23, 2025

Alternatively, we don't do this internally but instead expose PgEscapeOption and direct people to use it. That would be backwards-compatible.

This would leak implementation details and keep a foot gun around, so I’d caution against that.

@abonander
Copy link
Collaborator

abonander commented Mar 23, 2025

This would leak implementation details and keep a foot gun around, so I’d caution against that.

  1. It's a detail of the underlying protocol, which can be important to understand.
  2. We could emit a warning log if someone provides a string that isn't properly escaped.
  3. I have learned that silent changes to behavior tend to trip people up regardless, because not everyone reads the CHANGELOG or reads it thoroughly. Hyrum's Law has always held true.
  4. Users who aren't prepared to upgrade to a new major version won't be able to benefit.

If you're fine waiting for this to hit 0.9.0, then it doesn't matter.

@abonander abonander added this to the 0.9.0 milestone Apr 14, 2025
@V02460 V02460 requested a review from abonander April 15, 2025 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PgConnectOptions.options are not URL encoded
2 participants