A platform for community discussion. Free, open, simple.
FIX: Reject unregistered topic archetypes (#45364)
Keegan George3fc5966b51fe
diff --git a/app/models/topic.rb b/app/models/topic.rbindex 1fca8296903..99b4a03d10b 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -217,6 +217,8 @@ class Topic < ActiveRecord::Base!SiteSetting.allow_uncategorized_topics && (t.archetype.nil? || t.regular?)}+ validate :ensure_archetype_is_registered, if: :will_save_change_to_archetype?+validates :featured_link, allow_nil: true, url: truevalidate if: :featured_link doif featured_link_changed? && !Guardian.new(user).can_edit_featured_link?(category_id)@@ -2097,6 +2099,10 @@ class Topic < ActiveRecord::Baseerrors.add(:featured_link, :invalid)end+ def ensure_archetype_is_registered+ errors.add(:archetype, :inclusion) if !Archetype.exists?(archetype)+ end+def self.private_message_topics_count_per_day(start_date, end_date, topic_subtype)private_messages.with_subtype(topic_subtype)diff --git a/lib/archetype.rb b/lib/archetype.rbindex e518bc76b27..9f187a25223 100644--- a/lib/archetype.rb+++ b/lib/archetype.rb@@ -31,6 +31,10 @@ class Archetype@archetypes.valuesend+ def self.exists?(name)+ @archetypes.present? && @archetypes.key?(name)+ end+def self.register(name, options = {})@archetypes ||= {}@archetypes[name] = Archetype.new(name, options)diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rbindex 1b13d996cea..f782ac40b0c 100644--- a/lib/guardian/topic_guardian.rb+++ b/lib/guardian/topic_guardian.rb@@ -305,6 +305,7 @@ module TopicGuardiandef can_change_archetype?(topic, new_archetype)return true if new_archetype == topic.archetype+ return false if !Archetype.exists?(new_archetype)if new_archetype == Archetype.banner || topic.archetype == Archetype.bannerreturn can_banner_topic?(topic)enddiff --git a/spec/lib/archetype_spec.rb b/spec/lib/archetype_spec.rbindex 2f27d4e500e..82adb1245c9 100644--- a/spec/lib/archetype_spec.rb+++ b/spec/lib/archetype_spec.rb@@ -25,6 +25,19 @@ RSpec.describe Archetype doendend+ describe ".exists?" do+ it "returns true for registered archetypes" do+ expect(Archetype.exists?(Archetype.default)).to eq(true)+ expect(Archetype.exists?(Archetype.private_message)).to eq(true)+ expect(Archetype.exists?(Archetype.banner)).to eq(true)+ end++ it "returns false for unregistered archetypes" do+ expect(Archetype.exists?("unknown")).to eq(false)+ expect(Archetype.exists?(nil)).to eq(false)+ end+ end+describe "register an archetype" doit "has one more element" do@list = Archetype.list.dupdiff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rbindex ccf74f1922c..529631b1375 100644--- a/spec/lib/guardian_spec.rb+++ b/spec/lib/guardian_spec.rb@@ -714,6 +714,19 @@ RSpec.describe Guardian doit "returns true when archetype is unchanged" doexpect(Guardian.new(user).can_change_archetype?(topic, topic.archetype)).to eq(true)end++ it "does not allow anyone to change archetype to an unregistered one" do+ expect(Guardian.new(user).can_change_archetype?(topic, "unknown")).to eq(false)+ expect(Guardian.new(admin).can_change_archetype?(topic, "unknown")).to eq(false)+ end++ it "allows changing archetype to one registered by a plugin" do+ Archetype.register("plugin_archetype")++ expect(Guardian.new(user).can_change_archetype?(topic, "plugin_archetype")).to eq(true)+ ensure+ Archetype.deregister("plugin_archetype")+ endendit "respects category group moderator settings" dodiff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 53d444f0a52..5a249cc43ff 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -251,6 +251,33 @@ RSpec.describe Topic doendend+ describe "archetype validation" do+ fab!(:topic)++ it "is invalid with an unregistered archetype" do+ topic.archetype = "unknown"++ expect(topic).not_to be_valid+ expect(topic.errors[:archetype]).to be_present+ end++ it "is valid with an archetype registered by a plugin" do+ Archetype.register("plugin_archetype")+ topic.archetype = "plugin_archetype"++ expect(topic).to be_valid+ ensure+ Archetype.deregister("plugin_archetype")+ end++ it "does not block saving a topic that already has an unregistered archetype" do+ topic.update_column(:archetype, "unknown")+ topic.title = "this is a brand new topic title"++ expect(topic).to be_valid+ end+ end+describe "slug" docontext "with encoded generator" dobefore { SiteSetting.slug_generation_method = "encoded" }diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex d737de6edad..0df8d1e502a 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -1626,6 +1626,21 @@ RSpec.describe PostsController doendend+ it "creates a regular topic when an unregistered archetype is supplied" do+ sign_in(user_trust_level_1)++ post "/posts.json",+ params: {+ raw: "this is a test topic body",+ title: "this is a test topic title",+ category: category.id,+ archetype: "unknown",+ }++ expect(response.status).to eq(200)+ expect(Topic.find(response.parsed_body["topic_id"]).archetype).to eq(Archetype.default)+ end+context "with api" doit "memoizes duplicate requests" doraw = "this is a test post 123 #{SecureRandom.hash}"diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 7a97d16ab85..7f56e21369d 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -2409,6 +2409,13 @@ RSpec.describe TopicsController doexpect(topic.archetype).to eq(Archetype.default)end+ it "does not allow changing archetype to an unregistered one" do+ put "/t/#{topic.id}.json", params: { archetype: "unknown" }++ expect(response.status).to eq(422)+ expect(topic.reload.archetype).to eq(Archetype.default)+ end+it "does not allow a regular user to convert a private message to a public topic" doprivate_message = Fabricate(:private_message_topic, user: user, recipient: user_2)Fabricate(:post, topic: private_message, user: user)