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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions manifests/database/postgresql.pp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
}

exec { 'update_pgpass':
command => "echo ${database_host}:5432:${database_name}:${database_user}:${database_password} >> /root/.pgpass",
command => "echo ${database_host}:5432:${database_name}:${database_user}:${shell_escape($database_password)} >> /root/.pgpass",
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.

require => File['/root/.pgpass'],
}

Expand Down