Skip to content

Libpq connection string escaping #1285

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

Merged
merged 3 commits into from
May 15, 2017

Conversation

sehrope
Copy link
Contributor

@sehrope sehrope commented May 10, 2017

While silly, the following are perfectly legal identifiers for a username and database:

-- Username contains a single quote
postgres=# CREATE USER "dumb'user" WITH PASSWORD 'abcd1234';
CREATE ROLE

-- Database name contains a backslash
postgres=# CREATE DATABASE "dumb\db" WITH OWNER "dumb'user";
CREATE DATABASE

The existing libpq config code blindly surrounds the passed in parameters using single quotes (ex: foo => 'foo'). That breaks down when there are backslashes or single quotes in the values.

This PR fixes handling of single quotes and backslashes in connection config values used to generate libpq connection strings by escaping any occurrences of those characters.

For most property names there is no change in the valid case (i.e. no funky chars) as they were already quoted. This just corrects handling when they have funky chars.

The two exceptions are host, and hostaddr which previously weren't escaped at all. There's no valid value for either of those that could contain a single quote or even whitespace so it was fine for the valid case. But that also means that you have a config injection if you pass an invalid value like somehost foo=bar. It's not really possible right now as there's also a DNS lookup which presumably would fail for such a host, but if it were ever removed (which may be a good idea as hostaddr is an optional field anyway and having it unspecified has some value as well) it'd be broken.

Either way, the new code also escapes/quotes them as well and I've updated a couple tests to take that into account.

sehrope added 3 commits May 10, 2017 07:18
Fix handlings of libpq connection properties to properly escape single
quotes and backslashes. Previously the values were surrounded in single
quotes which handled whitespace within the property value, but internal
single quotes and backslashes would cause invalid connection strings to
be generated.
Update the expect host output in the connection parameter test
to expect it to be surrounded by single quotes.
@brianc
Copy link
Owner

brianc commented May 12, 2017

This is fantastic @sehrope - thank you! Would you consider this to be a breaking change or a bug fix? If I'm understanding you correctly the old, broken way would never even work so it's unlikely anyone was relying on that behavior.

@sehrope
Copy link
Contributor Author

sehrope commented May 12, 2017

I'd consider it a bug fix. I can't imagine anyone relying on the old broken functionality.

@brianc
Copy link
Owner

brianc commented May 15, 2017

Okay awesome! thank you @sehrope - will merge it in shortly!

@brianc brianc merged commit ee81936 into brianc:master May 15, 2017
@brianc
Copy link
Owner

brianc commented May 15, 2017

published pg@6.2.2 👏 Thanks for your work on this @sehrope and the really wonderful description of the initial behavior and problem. A pleasure to work with.

@sehrope
Copy link
Contributor Author

sehrope commented May 15, 2017

@brianc Anytime! And thanks more generally for the awesomeness that is this driver. Been using it happily for years and it's always a pleasure to contribute to it.

@sehrope sehrope deleted the libpq-connection-string-escaping branch May 16, 2017 10:57
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.

3 participants