FIX: Empty backup names with unicode site titles

FIX: Empty backup names with unicode site titles

If a site title contains unicode it may end up with an empty backup filename because of the rails parameterize method we are calling.

This fix ensures that the backup filenames default to “discourse” if the parameterized site title is empty.

Bug reported here.

diff --git a/lib/backup_restore/backuper.rb b/lib/backup_restore/backuper.rb
index 5ee1f01..4eb0c98 100644
--- a/lib/backup_restore/backuper.rb
+++ b/lib/backup_restore/backuper.rb
@@ -83,6 +83,10 @@ module BackupRestore
       raise Discourse::InvalidParameters.new(:user_id) unless @user
     end
 
+    def get_parameterized_title
+      SiteSetting.title.parameterize.empty? ? "discourse" : SiteSetting.title.parameterize
+    end
+
     def initialize_state
       @success = false
       @store = BackupRestore::BackupStore.create
@@ -91,7 +95,7 @@ module BackupRestore
       @tmp_directory = File.join(Rails.root, "tmp", "backups", @current_db, @timestamp)
       @dump_filename = File.join(@tmp_directory, BackupRestore::DUMP_FILE)
       @archive_directory = BackupRestore::LocalBackupStore.base_directory(db: @current_db)
-      filename = @filename_override || "#{SiteSetting.title.parameterize}-#{@timestamp}"
+      filename = @filename_override || "#{get_parameterized_title}-#{@timestamp}"
       @archive_basename = File.join(@archive_directory, "#{filename}-#{BackupRestore::VERSION_PREFIX}#{BackupRestore.current_version}")
 
       @backup_filename =
diff --git a/spec/lib/backup_restore/backuper_spec.rb b/spec/lib/backup_restore/backuper_spec.rb
new file mode 100644
index 0000000..1c96892
--- /dev/null
+++ b/spec/lib/backup_restore/backuper_spec.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe BackupRestore::Backuper do
+  it 'returns a non-empty parameterized title when site title contains unicode' do
+    SiteSetting.title = 'Ɣ'
+    backuper = BackupRestore::Backuper.new(-1)
+
+    expect(backuper.send(:get_parameterized_title)).to eq("discourse")
+  end
+
+  it 'returns a valid parameterized site title' do
+    SiteSetting.title = "Coding Horror"
+    backuper = BackupRestore::Backuper.new(-1)
+
+    expect(backuper.send(:get_parameterized_title)).to eq("coding-horror")
+  end
+end

GitHub sha: c8661674

1 Like

Rails has a .presence method that is handy for code like this

def get_parameterized_title
  SiteSetting.title.parameterize.presence || "discourse"
end
1 Like

REFACTOR: Clean up parameterized title