Skip to content

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.rb
index 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: true
validate if: :featured_link do
if featured_link_changed? && !Guardian.new(user).can_edit_featured_link?(category_id)
@@ -2097,6 +2099,10 @@ class Topic < ActiveRecord::Base
errors.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.rb
index e518bc76b27..9f187a25223 100644
--- a/lib/archetype.rb
+++ b/lib/archetype.rb
@@ -31,6 +31,10 @@ class Archetype
@archetypes.values
end
+ 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.rb
index 1b13d996cea..f782ac40b0c 100644
--- a/lib/guardian/topic_guardian.rb
+++ b/lib/guardian/topic_guardian.rb
@@ -305,6 +305,7 @@ module TopicGuardian
def 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.banner
return can_banner_topic?(topic)
end
diff --git a/spec/lib/archetype_spec.rb b/spec/lib/archetype_spec.rb
index 2f27d4e500e..82adb1245c9 100644
--- a/spec/lib/archetype_spec.rb
+++ b/spec/lib/archetype_spec.rb
@@ -25,6 +25,19 @@ RSpec.describe Archetype do
end
end
+ 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" do
it "has one more element" do
@list = Archetype.list.dup
diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rb
index ccf74f1922c..529631b1375 100644
--- a/spec/lib/guardian_spec.rb
+++ b/spec/lib/guardian_spec.rb
@@ -714,6 +714,19 @@ RSpec.describe Guardian do
it "returns true when archetype is unchanged" do
expect(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")
+ end
end
it "respects category group moderator settings" do
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 53d444f0a52..5a249cc43ff 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -251,6 +251,33 @@ RSpec.describe Topic do
end
end
+ 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" do
context "with encoded generator" do
before { SiteSetting.slug_generation_method = "encoded" }
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index d737de6edad..0df8d1e502a 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -1626,6 +1626,21 @@ RSpec.describe PostsController do
end
end
+ 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" do
it "memoizes duplicate requests" do
raw = "this is a test post 123 #{SecureRandom.hash}"
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 7a97d16ab85..7f56e21369d 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -2409,6 +2409,13 @@ RSpec.describe TopicsController do
expect(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" do
private_message = Fabricate(:private_message_topic, user: user, recipient: user_2)
Fabricate(:post, topic: private_message, user: user)