Skip to content

Commit 0428248

Browse files
committed
Backslashes in a password need to be escaped
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.
1 parent 3c22469 commit 0428248

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

spec/classes/server_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,22 @@
452452
with_mode('0600').
453453
with_content(%r{admin\.auth\('admin', 'password'\)})
454454
}
455+
456+
context 'with backslash in password' do
457+
let :params do
458+
{
459+
admin_username: 'admin',
460+
admin_password: 'password_\_with_backslash',
461+
auth: true,
462+
store_creds: true
463+
}
464+
end
465+
466+
it {
467+
is_expected.to contain_file('/root/.mongoshrc.js').
468+
with_content(%r{admin\.auth\('admin', 'password_\\\\_with_backslash'\)})
469+
}
470+
end
455471
end
456472

457473
context 'false' do

templates/mongoshrc.js.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (authRequired()) {
3434
<%- end -%>
3535
try {
3636
admin = db.getSiblingDB('admin')
37-
admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>')
37+
admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive.gsub('\\','\\\\\\\\') %>')
3838
}
3939
catch (err) {
4040
// Silently ignore this error, we can't really do anything about it.

0 commit comments

Comments
 (0)