about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2020-02-29 03:00:43 +0100
committerGitHub <noreply@github.com>2020-02-29 03:00:43 +0100
commitce17cea2210f9ce9dcbaf68074c07633df61bb6f (patch)
tree707850e10f9da776a7061c4c73948da47327d3ea /lib
parent047fde18c3d20dcd74b14613a648a03b573f802d (diff)
Fix installation failing when Redis password contains special characters (#13156)
* Add support for special characters in Redis passwords

Fixes #13154

* Refactor
Diffstat (limited to 'lib')
-rw-r--r--lib/mastodon/redis_config.rb4
-rw-r--r--lib/tasks/mastodon.rake15
2 files changed, 17 insertions, 2 deletions
diff --git a/lib/mastodon/redis_config.rb b/lib/mastodon/redis_config.rb
index f11d94a45..e9db9122f 100644
--- a/lib/mastodon/redis_config.rb
+++ b/lib/mastodon/redis_config.rb
@@ -14,7 +14,9 @@ def setup_redis_env_url(prefix = nil, defaults = true)
   ENV[prefix + 'REDIS_URL'] = if [password, host, port, db].all?(&:nil?)
                                 ENV['REDIS_URL']
                               else
-                                "redis://#{password.blank? ? '' : ":#{password}@"}#{host}:#{port}/#{db}"
+                                Addressable::URI.parse("redis://#{host}:#{port}/#{db}").tap do |uri|
+                                  uri.password = password if password.present?
+                                end.normalize.to_str
                               end
 end
 
diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake
index 2e92e8ded..a873335d4 100644
--- a/lib/tasks/mastodon.rake
+++ b/lib/tasks/mastodon.rake
@@ -336,7 +336,20 @@ namespace :mastodon do
       if prompt.yes?('Save configuration?')
         cmd = TTY::Command.new(printer: :quiet)
 
-        File.write(Rails.root.join('.env.production'), "# Generated with mastodon:setup on #{Time.now.utc}\n\n" + env.each_pair.map { |key, value| "#{key}=#{value}" }.join("\n") + "\n")
+        env_contents = env.each_pair.map do |key, value|
+          if value.is_a?(String) && value =~ /[\s\#\\"]/
+            if value =~ /[']/
+              value = value.to_s.gsub(/[\\"\$]/) { |x| "\\#{x}" }
+              "#{key}=\"#{value}\""
+            else
+              "#{key}='#{value}'"
+            end
+          else
+            "#{key}=#{value}"
+          end
+        end.join("\n")
+
+        File.write(Rails.root.join('.env.production'), "# Generated with mastodon:setup on #{Time.now.utc}\n\n" + env_contents + "\n")
 
         if using_docker
           prompt.ok 'Below is your configuration, save it to an .env.production file outside Docker:'