Skip to content
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

Escape string inside update_pgpass command #831

Closed
wants to merge 1 commit into from

Conversation

src386
Copy link

@src386 src386 commented Aug 3, 2022

Pull Request (PR) description

The update_pgpass resource may fail when using strong ${database_password} that contain special characters such as & because the echoed string is not escaped. This Pull request adds double quotes shell_escape() (as suggested by @smortex).

This Pull Request (PR) fixes the following issues

@src386 src386 mentioned this pull request Aug 3, 2022
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => "grep \"${database_host}:5432:${database_name}:${database_user}:${database_password}\" /root/.pgpass",
unless => "grep ${database_host}:5432:${database_name}:${database_user}:${shell_escape($database_password)} /root/.pgpass",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can be even more creative 😈 :

romain@desktop-fln40kq ~ % sudo -u postgres createdb 'foo bar'
romain@desktop-fln40kq ~ % sudo -u postgres createdb 'foo:bar'
romain@desktop-fln40kq ~ % sudo -u postgres createuser 'foo bar baz'
romain@desktop-fln40kq ~ % sudo -u postgres createuser 'foo:bar:baz'

The spaces will break the command, and according to https://www.postgresql.org/docs/current/libpq-pgpass.html:

If an entry needs to contain : or \, escape this character with \.

So I think we should also escape the database_name, database_user and database_password accordingly (I do not think these chars are valid for a hostname), and then shell-escape the whole pattern:

$escaped_database_name     = $database_name.regsubst('[\\\\:]', '\\\\\\0', 'G')
$escaped_database_user     = $database_user.regsubst('[\\\\:]', '\\\\\\0', 'G')
$escaped_database_password = $database_password.regsubst('[\\\\:]', '\\\\\\0', 'G')
Suggested change
unless => "grep ${database_host}:5432:${database_name}:${database_user}:${shell_escape($database_password)} /root/.pgpass",
unless => "grep ${"${database_host}:5432:${escaped_database_name}:${escaped_database_user}:${escaped_database_password}".shell_escape} /root/.pgpass",

I only tested the pattern substitution (which required much escaping than i though), so we probably need to test this for real with users with annoying chars in their name to be sure it is fine.

But all this is probably overkill, maybe we can just detect "forbidden" chars and raise an error because nobody wants to do this kind of things?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion postgresql admins should know that they must avoid special characters in databases names and usernames. This is a bit different for passwords than can be sometimes randomly generated or have strong complexity requirements. In my case I used pwgen to get a strong password.

They should also know that escape characters in passwords will break everything and must be avoided. So I think this is reasonable to keep the fix as simple as possible.

@ekohl
Copy link
Member

ekohl commented Nov 7, 2023

In #904 I've taken another approach where I provide all the data via env vars, removing the need for the pgpass file altogether.

@ekohl ekohl closed this Nov 7, 2023
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.

Exec[update_pgpass] failed
4 participants