Skip to content

Commit

Permalink
Backslashes in a password need to be escaped
Browse files Browse the repository at this point in the history
This will replace a single backslash with a double backslash in the
`/root/.mongoshrc.js` file. when a password with a backslash is used, it
is correctly passed on to the provider for setting the user's password,
but things break when attempting to use said password for the admin
user.

A small explanation on the amount of backslashes: The first argument is
a regular expression, so we need to escape the backslash. The second
argument allows for references to capture groups or the entire match
using backslashes, for example `\0` contains the entire match.  This
would make us end up with 4 backslashes, but apparantly the template
rendering also has backslash escaping, this we need to double the amount
of backslashes. So 8 in total.
  • Loading branch information
stevenpost committed May 3, 2024
1 parent 3c22469 commit 0428248
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions spec/classes/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,22 @@
with_mode('0600').
with_content(%r{admin\.auth\('admin', 'password'\)})
}

context 'with backslash in password' do
let :params do
{
admin_username: 'admin',
admin_password: 'password_\_with_backslash',
auth: true,
store_creds: true
}
end

it {
is_expected.to contain_file('/root/.mongoshrc.js').
with_content(%r{admin\.auth\('admin', 'password_\\\\_with_backslash'\)})
}
end
end

context 'false' do
Expand Down
2 changes: 1 addition & 1 deletion templates/mongoshrc.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (authRequired()) {
<%- end -%>
try {
admin = db.getSiblingDB('admin')
admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>')
admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive.gsub('\\','\\\\\\\\') %>')
}
catch (err) {
// Silently ignore this error, we can't really do anything about it.
Expand Down

0 comments on commit 0428248

Please sign in to comment.