A platform for community discussion. Free, open, simple.
SECURITY: correct XSS on long topic titles
Sam7d6b348d0b6c
diff --git a/app/models/topic.rb b/app/models/topic.rbindex c0bbb021630..e2e0fb449c8 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -221,8 +221,9 @@ class Topic < ActiveRecord::Baseunless skip_callbacksensure_topic_has_a_categoryend+if title_changed?- write_attribute :fancy_title, Topic.fancy_title(title)+ write_attribute(:fancy_title, Topic.fancy_title(title))endif category_id_changed? || new_record?@@ -346,10 +347,9 @@ class Topic < ActiveRecord::Baseenddef self.fancy_title(title)- escaped = ERB::Util.html_escape(title)- return unless escaped+ return unless escaped = ERB::Util.html_escape(title)fancy_title = Emoji.unicode_unescape(HtmlPrettify.render(escaped))- fancy_title.length > Topic.max_fancy_title_length ? title : fancy_title+ fancy_title.length > Topic.max_fancy_title_length ? escaped : fancy_titleenddef fancy_titlediff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex df1b61225dc..5e28d0d618a 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -296,10 +296,17 @@ describe Topic doexpect(topic_image.fancy_title).to eq("Topic with <img src=‘something’> image in its title")end+ it "always escapes title" do+ topic_script.title = topic_script.title + "x" * Topic.max_fancy_title_length+ expect(topic_script.fancy_title).to eq(ERB::Util.html_escape(topic_script.title))+ # not really needed, but just in case+ expect(topic_script.fancy_title).not_to include("<script>")+ end+endcontext 'fancy title' do- let(:topic) { Fabricate.build(:topic, title: "\"this topic\" -- has ``fancy stuff''") }+ let(:topic) { Fabricate.build(:topic, title: %{"this topic" -- has ``fancy stuff''}) }context 'title_fancy_entities disabled' dobefore do@@ -319,7 +326,6 @@ describe Topic doit "converts the title to have fancy entities and updates" doexpect(topic.fancy_title).to eq("“this topic” – has “fancy stuff”")topic.title = "this is my test hello world... yay"- topic.user.save!topic.save!topic.reloadexpect(topic.fancy_title).to eq("This is my test hello world… yay")@@ -336,7 +342,7 @@ describe Topic doendit "works with long title that results in lots of entities" do- long_title = "NEW STOCK PICK: PRCT - LAST PICK UP 233%, NNCO.................................................................................................................................................................. ofoum"+ long_title = "NEW STOCK PICK: PRCT - LAST PICK UP 233%, NNCO#{"." * 150} ofoum"topic.title = long_titleexpect { topic.save! }.to_not raise_error