Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: block cross-site backup traversal in multisite local storage

Roman Rizzib348c0d0ce85
diff --git a/app/controllers/admin/backups_controller.rb b/app/controllers/admin/backups_controller.rb
index c81e3bf308a..bb6c5904994 100644
--- a/app/controllers/admin/backups_controller.rb
+++ b/app/controllers/admin/backups_controller.rb
@@ -7,6 +7,7 @@ class Admin::BackupsController < Admin::AdminController
include ExternalUploadHelpers
before_action :ensure_backups_enabled
+ before_action :ensure_valid_backup_id, only: %i[show email destroy restore]
skip_before_action :check_xhr, only: %i[index show logs check_backup_chunk upload_backup_chunk]
skip_before_action :ensure_backups_enabled, only: %i[show status index email]
@@ -271,6 +272,11 @@ class Admin::BackupsController < Admin::AdminController
!!(/\A[a-zA-Z0-9\._\-]+\z/ =~ filename)
end
+ def ensure_valid_backup_id
+ backup_id = params.fetch(:id)
+ raise Discourse::NotFound unless valid_filename?(backup_id) && valid_extension?(backup_id)
+ end
+
def render_error(message_key)
render json: failed_json.merge(message: I18n.t(message_key))
end
diff --git a/lib/backup_restore/local_backup_store.rb b/lib/backup_restore/local_backup_store.rb
index 38cd61b275d..3958ca1a7ed 100644
--- a/lib/backup_restore/local_backup_store.rb
+++ b/lib/backup_restore/local_backup_store.rb
@@ -55,7 +55,14 @@ module BackupRestore
end
def path_from_filename(filename)
- File.join(@base_directory, filename)
+ path = File.expand_path(File.join(@base_directory, filename))
+ base_directory = File.expand_path(@base_directory)
+
+ if path.start_with?("#{base_directory}#{File::SEPARATOR}")
+ path
+ else
+ raise Discourse::InvalidParameters.new(:filename)
+ end
end
def create_file_from_path(path, include_download_source = false)
diff --git a/lib/route_format.rb b/lib/route_format.rb
index 23fb554701b..07af6a90368 100644
--- a/lib/route_format.rb
+++ b/lib/route_format.rb
@@ -6,6 +6,6 @@ module RouteFormat
end
def self.backup
- /.+\.(sql\.gz|tar\.gz|tgz)/i
+ /[a-zA-Z0-9._-]+\.(sql\.gz|tar\.gz|tgz)/i
end
end
diff --git a/spec/lib/backup_restore/local_backup_store_spec.rb b/spec/lib/backup_restore/local_backup_store_spec.rb
index 4252507b6c9..2b2c578d41a 100644
--- a/spec/lib/backup_restore/local_backup_store_spec.rb
+++ b/spec/lib/backup_restore/local_backup_store_spec.rb
@@ -19,6 +19,40 @@ RSpec.describe BackupRestore::LocalBackupStore do
expect(store.remote?).to eq(false)
end
+ describe "path traversal protection" do
+ let(:filename) { "a.tgz" }
+
+ before do
+ create_local_backup_file(
+ root_directory: @root_directory,
+ db_name: "default",
+ filename: filename,
+ last_modified: "2018-02-11T09:27:00Z",
+ size_in_bytes: 29,
+ )
+ end
+
+ it "raises an error when reading a backup outside the current site's directory" do
+ expect { store.file("../second/multi-1.tar.gz") }.to raise_error(Discourse::InvalidParameters)
+ end
+
+ it "raises an error when deleting a backup outside the current site's directory" do
+ expect { store.delete_file("../second/multi-1.tar.gz") }.to raise_error(
+ Discourse::InvalidParameters,
+ )
+ end
+
+ it "raises an error when downloading a backup outside the current site's directory" do
+ Dir.mktmpdir do |path|
+ destination_path = File.join(path, filename)
+
+ expect { store.download_file("../second/multi-1.tar.gz", destination_path) }.to raise_error(
+ Discourse::InvalidParameters,
+ )
+ end
+ end
+ end
+
def create_backups
create_local_backup_file(
root_directory: @root_directory,
diff --git a/spec/lib/route_format_spec.rb b/spec/lib/route_format_spec.rb
new file mode 100644
index 00000000000..9d142a07939
--- /dev/null
+++ b/spec/lib/route_format_spec.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+RSpec.describe RouteFormat do
+ describe ".backup" do
+ def full_backup_match?(filename)
+ /\A#{described_class.backup}\z/i.match?(filename)
+ end
+
+ it "matches valid backup filenames" do
+ expect(full_backup_match?("backup-2026-05-12.tar.gz")).to eq(true)
+ expect(full_backup_match?("backup-2026-05-12.tgz")).to eq(true)
+ expect(full_backup_match?("backup-2026-05-12.sql.gz")).to eq(true)
+ end
+
+ it "does not match path traversal attempts" do
+ expect(full_backup_match?("../second/backup.tar.gz")).to eq(false)
+ expect(full_backup_match?("..%2Fsecond%2Fbackup.tar.gz")).to eq(false)
+ expect(full_backup_match?("nested/path/backup.tar.gz")).to eq(false)
+ end
+ end
+end
diff --git a/spec/requests/admin/backups_controller_spec.rb b/spec/requests/admin/backups_controller_spec.rb
index dd2756170ed..ea8f1365c66 100644
--- a/spec/requests/admin/backups_controller_spec.rb
+++ b/spec/requests/admin/backups_controller_spec.rb
@@ -237,6 +237,13 @@ RSpec.describe Admin::BackupsController do
expect(response.status).to eq(404)
end
+
+ it "returns 404 for invalid backup ids" do
+ token = EmailBackupToken.set(admin.id)
+ get "/admin/backups/..%2Fsecond%2F#{backup_filename}.json", params: { token: token }
+
+ expect(response.status).to eq(404)
+ end
end
shared_examples "backup inaccessible" do
@@ -295,6 +302,11 @@ RSpec.describe Admin::BackupsController do
expect(response.status).to eq(404)
end
+ it "returns 404 for invalid backup ids" do
+ delete "/admin/backups/..%2Fsecond%2F#{backup_filename}.json"
+ expect(response.status).to eq(404)
+ end
+
context "when readonly mode is enabled" do
before { Discourse.enable_readonly_mode }
@@ -399,6 +411,15 @@ RSpec.describe Admin::BackupsController do
expect(response.status).to eq(200)
end
end
+
+ it "returns 404 for invalid backup ids" do
+ post "/admin/backups/..%2Fsecond%2F#{backup_filename}/restore.json",
+ params: {
+ client_id: "foo",
+ }
+
+ expect(response.status).to eq(404)
+ end
end
shared_examples "backup restoration not allowed" do
@@ -939,6 +960,12 @@ RSpec.describe Admin::BackupsController do
expect(response).to be_not_found
end
+
+ it "returns 404 for invalid backup ids" do
+ put "/admin/backups/..%2Fsecond%2F#{backup_filename}.json"
+
+ expect(response).to be_not_found
+ end
end
shared_examples "backup emails not allowed" do