A platform for community discussion. Free, open, simple.
FEATURE: whitelist theme repo mode (experimental)
Sam Saffron57a3d4e0d2c8
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rbindex d51b2439791..a08bc95495b 100644--- a/app/controllers/admin/themes_controller.rb+++ b/app/controllers/admin/themes_controller.rb@@ -14,6 +14,9 @@ class Admin::ThemesController < Admin::AdminControllerenddef upload_asset++ ban_in_whitelist_mode!+path = params[:file].pathhijack do@@ -49,6 +52,9 @@ class Admin::ThemesController < Admin::AdminControllerdef import@theme = nilif params[:theme] && params[:theme].content_type == "application/json"++ ban_in_whitelist_mode!+# .dcstyle.json import. Deprecated, but still available to allow conversionjson = JSON::parse(params[:theme].read)theme = json['theme']@@ -85,15 +91,21 @@ class Admin::ThemesController < Admin::AdminControllerelserender json: @theme.errors, status: :unprocessable_entityend- elsif params[:remote]+ elsif remote = params[:remote]++ guardian.ensure_allowed_theme_repo_import!(remote.strip)+beginbranch = params[:branch] ? params[:branch] : nil- @theme = RemoteTheme.import_theme(params[:remote], theme_user, private_key: params[:private_key], branch: branch)+ @theme = RemoteTheme.import_theme(remote, theme_user, private_key: params[:private_key], branch: branch)render json: @theme, status: :createdrescue RemoteTheme::ImportError => erender_json_error e.messageendelsif params[:bundle] || (params[:theme] && THEME_CONTENT_TYPES.include?(params[:theme].content_type))++ ban_in_whitelist_mode!+# params[:bundle] used by theme CLI. params[:theme] used by admin UIbundle = params[:bundle] || params[:theme]theme_id = params[:theme_id]@@ -139,6 +151,9 @@ class Admin::ThemesController < Admin::AdminControllerenddef create++ ban_in_whitelist_mode!+@theme = Theme.new(name: theme_params[:name],user_id: theme_user.id,user_selectable: theme_params[:user_selectable] || false,@@ -282,6 +297,10 @@ class Admin::ThemesController < Admin::AdminControllerprivate+ def ban_in_whitelist_mode!+ raise Discourse::InvalidAccess if !GlobalSetting.whitelisted_theme_ids.nil?+ end+def add_relative_themes!(kind, ids)expected = ids.map(&:to_i)@@ -339,6 +358,8 @@ class Admin::ThemesController < Admin::AdminControllerdef set_fieldsreturn unless fields = theme_params[:theme_fields]+ ban_in_whitelist_mode!+fields.each do |field|@theme.set_field(target: field[:target],diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex e7f8c9750b0..2ce9f875ffd 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -414,7 +414,9 @@ class ApplicationController < ActionController::Baseendif theme_ids.blank? && SiteSetting.default_theme_id != -1- theme_ids << SiteSetting.default_theme_id+ if guardian.allow_themes?([SiteSetting.default_theme_id])+ theme_ids << SiteSetting.default_theme_id+ endend@theme_ids = request.env[:resolved_theme_ids] = theme_idsdiff --git a/app/models/global_setting.rb b/app/models/global_setting.rbindex cd80df66818..54dc310e618 100644--- a/app/models/global_setting.rb+++ b/app/models/global_setting.rb@@ -220,6 +220,23 @@ class GlobalSettingendend+ # test only+ def self.reset_whitelisted_theme_ids!+ @whitelisted_theme_ids = nil+ end++ def self.whitelisted_theme_ids+ return nil if whitelisted_theme_repos.blank?++ @whitelisted_theme_ids ||= begin+ urls = whitelisted_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/config/discourse_defaults.conf b/config/discourse_defaults.confindex 39df104204d..b2b2aff03ed 100644--- a/config/discourse_defaults.conf+++ b/config/discourse_defaults.conf@@ -285,3 +285,12 @@ compress_anon_cache = false# This ensures there are no pathological cases where we keep storing data in anonymous cache# never to use it, set to 1 to store immediately, set to 0 to disable anon cacheanon_cache_store_threshold = 2++# EXPERIMENTAL - not yet supported in production+# by default admins can install and amend any theme+# you may restrict it so only specific themes are approved+# in whitelist mode all theme updates must happen via git repos+# themes missing from the list are automatically disallowed+# list is a comma seperated list of git repos eg:+# https://github.com/discourse/discourse-custom-header-links.git,https://github.com/discourse/discourse-simple-theme.git+whitelisted_theme_repos =diff --git a/lib/guardian.rb b/lib/guardian.rbindex 456461fead6..da6d1bf2b79 100644--- a/lib/guardian.rb+++ b/lib/guardian.rb@@ -471,9 +471,27 @@ class Guardian@user.staff? || @user.trust_level >= TrustLevel.levels[:member]end+ def allowed_theme_repo_import?(repo)+ return false if [email protected]?++ whitelisted_repos = GlobalSetting.whitelisted_theme_repos+ if !whitelisted_repos.blank?+ urls = whitelisted_repos.split(",").map(&:strip)+ return urls.include?(repo)+ end++ true+ end+def allow_themes?(theme_ids, include_preview: false)return true if theme_ids.blank?+ if whitelisted_theme_ids = GlobalSetting.whitelisted_theme_ids+ if (theme_ids - whitelisted_theme_ids).present?+ return false+ end+ end+if include_preview && is_staff? && (theme_ids - Theme.theme_ids).blank?return trueenddiff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rakeindex 8a90196faf9..8a8cde8f68d 100644--- a/lib/tasks/themes.rake+++ b/lib/tasks/themes.rake@@ -50,3 +50,27 @@ task "themes:install" => :environment do |task, args|exit 1endend++desc "List all the installed themes on the site"+task "themes:audit" => :environment do+ components = Set.new+ puts "Selectable themes"+ puts "-----------------"++ Theme.where("(enabled OR user_selectable) AND NOT component").each do |theme|+ puts theme.remote_theme&.remote_url || theme.name+ theme.child_themes.each do |child|+ if child.enabled+ repo = child.remote_theme&.remote_url || child.name+ components << repo+ end+ end+ end++ puts+ puts "Selectable components"+ puts "---------------------"+ components.each do |repo|+ puts repo+ end+enddiff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rbindex 97a1b0c2e06..53b1b14c1c3 100644--- a/spec/components/guardian_spec.rb+++ b/spec/components/guardian_spec.rb@@ -2869,6 +2869,32 @@ describe Guardian dolet!(:theme) { Fabricate(:theme) }let!(:theme2) { Fabricate(:theme) }+ context "whitelist mode" do+ before do+ GlobalSetting.reset_whitelisted_theme_ids!+ global_setting :whitelisted_theme_repos, " https://magic.com/repo.git, https://x.com/git"+ end++ after do+ GlobalSetting.reset_whitelisted_theme_ids!+ end++ it "should respect theme whitelisting" do+ r = RemoteTheme.create!(remote_url: "https://magic.com/repo.git")+ theme.update!(remote_theme_id: r.id)++ guardian = Guardian.new(admin)++ expect(guardian.allow_themes?([theme.id, theme2.id], include_preview: true)).to eq(false)++ expect(guardian.allow_themes?([theme.id], include_preview: true)).to eq(true)++ expect(guardian.allowed_theme_repo_import?('https://x.com/git')).to eq(true)+ expect(guardian.allowed_theme_repo_import?('https:/evil.com/git')).to eq(false)++ end+ end+it "allows staff to use any themes" doexpect(Guardian.new(moderator).allow_themes?([theme.id, theme2.id])).to eq(false)expect(Guardian.new(admin).allow_themes?([theme.id, theme2.id])).to eq(false)diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rbindex 39e24e89401..a7d42ce4f9b 100644--- a/spec/requests/admin/themes_controller_spec.rb+++ b/spec/requests/admin/themes_controller_spec.rb@@ -87,6 +87,7 @@ describe Admin::ThemesController doenddescribe '#import' do+let(:theme_json_file) doRack::Test::UploadedFile.new(file_from_fixtures("sam-s-simple-theme.dcstyle.json", "json"), "application/json")end@@ -99,6 +100,40 @@ describe Admin::ThemesController dofile_from_fixtures("logo.png")end+ context 'when theme whitelist mode is enabled' do+ before do+ GlobalSetting.reset_whitelisted_theme_ids!+ global_setting :whitelisted_theme_repos, "https://github.com/discourse/discourse-brand-header"+ end++ after do+ GlobalSetting.reset_whitelisted_theme_ids!+ end++ it "allows whitelisted imports" do+ RemoteTheme.stubs(:import_theme)+ post "/admin/themes/import.json", params: {+ remote: ' https://github.com/discourse/discourse-brand-header '+ }++ expect(response.status).to eq(201)+ end++ it "bans non whtielisted imports" do+ RemoteTheme.stubs(:import_theme)+ post "/admin/themes/import.json", params: {+ remote: ' https://bad.com/discourse/discourse-brand-header '+ }++ expect(response.status).to eq(403)+ end++ it "bans json file import" do+ post "/admin/themes/import.json", params: { theme: theme_json_file }+ expect(response.status).to eq(403)+ end+ end+it 'can import a theme from Git' doRemoteTheme.stubs(:import_theme)post "/admin/themes/import.json", params: {@@ -120,7 +155,7 @@ describe Admin::ThemesController doendit 'imports a theme from an archive' do- existing_theme = Fabricate(:theme, name: "Header Icons")+ _existing_theme = Fabricate(:theme, name: "Header Icons")expect dopost "/admin/themes/import.json", params: { theme: theme_archive }@@ -135,7 +170,7 @@ describe Admin::ThemesController doit 'updates an existing theme from an archive by name' do# Old theme CLI method, remove Jan 2020- existing_theme = Fabricate(:theme, name: "Header Icons")+ _existing_theme = Fabricate(:theme, name: "Header Icons")expect dopost "/admin/themes/import.json", params: { bundle: theme_archive }@@ -150,7 +185,7 @@ describe Admin::ThemesController doit 'updates an existing theme from an archive by id' do# Used by theme CLI- existing_theme = Fabricate(:theme, name: "Header Icons")+ _existing_theme = Fabricate(:theme, name: "Header Icons")other_existing_theme = Fabricate(:theme, name: "Some other name")messages = MessageBus.track_publish do@@ -273,6 +308,34 @@ describe Admin::ThemesController doexpect(SiteSetting.default_theme_id).to eq(-1)end+ context 'when theme whitelist mode is enabled' do+ before do+ GlobalSetting.reset_whitelisted_theme_ids!+ global_setting :whitelisted_theme_repos, " https://magic.com/repo.git, https://x.com/git"+ end++ after do+ GlobalSetting.reset_whitelisted_theme_ids!+ end++ it 'unconditionally bans theme_fields from updating' do+ r = RemoteTheme.create!(remote_url: "https://magic.com/repo.git")+ theme.update!(remote_theme_id: r.id)++ put "/admin/themes/#{theme.id}.json", params: {+ theme: {+ name: 'my test name',+ theme_fields: [+ { name: 'scss', target: 'common', value: '' },+ { name: 'scss', target: 'desktop', value: 'body{color: blue;}' },+ ]+ }+ }++ expect(response.status).to eq(403)+ end+ end+it 'updates a theme' dotheme.set_field(target: :common, name: :scss, value: '.body{color: black;}')theme.save