A platform for community discussion. Free, open, simple.
FIX: allowed_theme_ids should not be persisted in GlobalSettings (#14756)
jbrwcfc62dbace34
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rbindex 95f9bbe1e12..33cfe6db3c7 100644--- a/app/controllers/admin/themes_controller.rb+++ b/app/controllers/admin/themes_controller.rb@@ -282,7 +282,7 @@ class Admin::ThemesController < Admin::AdminControllerprivatedef ban_in_allowlist_mode!- raise Discourse::InvalidAccess if !GlobalSetting.allowed_theme_ids.nil?+ raise Discourse::InvalidAccess if !Theme.allowed_remote_theme_ids.nil?enddef ban_for_remote_theme!diff --git a/app/models/global_setting.rb b/app/models/global_setting.rbindex fba25eccdc7..c83db37cfae 100644--- a/app/models/global_setting.rb+++ b/app/models/global_setting.rb@@ -238,23 +238,6 @@ class GlobalSettingendend- # test only- def self.reset_allowed_theme_ids!- @allowed_theme_ids = nil- end-- def self.allowed_theme_ids- return nil if allowed_theme_repos.blank?-- @allowed_theme_ids ||= begin- urls = allowed_theme_repos.split(",").map(&:strip)- Theme- .joins(:remote_theme)- .where('remote_themes.remote_url in (?)', urls)- .pluck(:id)- end- end-def self.add_default(name, default)unless self.respond_to? namedefine_singleton_method(name) dodiff --git a/app/models/theme.rb b/app/models/theme.rbindex e6c81bb4e92..41e4ba8bddc 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -183,6 +183,18 @@ class Theme < ActiveRecord::Baseendend+ def self.allowed_remote_theme_ids+ return nil if GlobalSetting.allowed_theme_repos.blank?++ get_set_cache "allowed_remote_theme_ids" do+ urls = GlobalSetting.allowed_theme_repos.split(",").map(&:strip)+ Theme+ .joins(:remote_theme)+ .where('remote_themes.remote_url in (?)', urls)+ .pluck(:id)+ end+ end+def self.components_for(theme_id)get_set_cache "theme_components_for_#{theme_id}" doChildTheme.where(parent_theme_id: theme_id).pluck(:child_theme_id)diff --git a/lib/guardian.rb b/lib/guardian.rbindex 693222846fe..f623f459355 100644--- a/lib/guardian.rb+++ b/lib/guardian.rb@@ -494,7 +494,7 @@ class Guardiandef allow_themes?(theme_ids, include_preview: false)return true if theme_ids.blank?- if allowed_theme_ids = GlobalSetting.allowed_theme_ids+ if allowed_theme_ids = Theme.allowed_remote_theme_idsif (theme_ids - allowed_theme_ids).present?return falseenddiff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rbindex 0185b322179..3ff0420bb2d 100644--- a/spec/components/guardian_spec.rb+++ b/spec/components/guardian_spec.rb@@ -3196,14 +3196,9 @@ describe Guardian docontext "allowlist mode" dobefore do- GlobalSetting.reset_allowed_theme_ids!global_setting :allowed_theme_repos, " https://magic.com/repo.git, https://x.com/git"end- after do- GlobalSetting.reset_allowed_theme_ids!- end-it "should respect theme allowlisting" dor = RemoteTheme.create!(remote_url: "https://magic.com/repo.git")theme.update!(remote_theme_id: r.id)diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rbindex 472bc039814..92b7020e4fd 100644--- a/spec/requests/admin/themes_controller_spec.rb+++ b/spec/requests/admin/themes_controller_spec.rb@@ -102,26 +102,23 @@ describe Admin::ThemesController docontext 'when theme allowlist mode is enabled' dobefore do- GlobalSetting.reset_allowed_theme_ids!- global_setting :allowed_theme_repos, "https://github.com/discourse/discourse-brand-header"- end-- after do- GlobalSetting.reset_allowed_theme_ids!+ global_setting :allowed_theme_repos, "https://github.com/discourse/discourse-brand-header.git"endit "allows allowlisted imports" do- RemoteTheme.stubs(:import_theme)+ expect(Theme.allowed_remote_theme_ids.length).to eq(0)+post "/admin/themes/import.json", params: {- remote: ' https://github.com/discourse/discourse-brand-header '+ remote: ' https://github.com/discourse/discourse-brand-header.git '}+ expect(Theme.allowed_remote_theme_ids.length).to eq(1)expect(response.status).to eq(201)endit "prevents adding disallowed themes" doRemoteTheme.stubs(:import_theme)- remote = ' https://bad.com/discourse/discourse-brand-header '+ remote = ' https://bad.com/discourse/discourse-brand-header.git 'post "/admin/themes/import.json", params: { remote: remote }@@ -138,7 +135,7 @@ describe Admin::ThemesController doit 'can import a theme from Git' doRemoteTheme.stubs(:import_theme)post "/admin/themes/import.json", params: {- remote: ' https://github.com/discourse/discourse-brand-header '+ remote: ' https://github.com/discourse/discourse-brand-header.git '}expect(response.status).to eq(201)@@ -311,14 +308,9 @@ describe Admin::ThemesController docontext 'when theme allowlist mode is enabled' dobefore do- GlobalSetting.reset_allowed_theme_ids!global_setting :allowed_theme_repos, " https://magic.com/repo.git, https://x.com/git"end- after do- GlobalSetting.reset_allowed_theme_ids!- end-it 'unconditionally bans theme_fields from updating' dor = RemoteTheme.create!(remote_url: "https://magic.com/repo.git")theme.update!(remote_theme_id: r.id)