-
-
Notifications
You must be signed in to change notification settings - Fork 228
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
Conversation
486e145
to
ec9e5b2
Compare
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", |
There was a problem hiding this comment.
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')
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?
There was a problem hiding this comment.
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.
In #904 I've taken another approach where I provide all the data via env vars, removing the need for the pgpass file altogether. |
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 addsdouble quotesshell_escape()
(as suggested by @smortex).This Pull Request (PR) fixes the following issues