A platform for community discussion. Free, open, simple.
SECURITY: Escape embed URL for HTML attribute context in footer (#42314)
Mark VanLandingham9e3cac68a65d
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex 48de799dd34..a294a88e604 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -36,7 +36,7 @@ class TopicEmbed < ActiveRecord::Baseenddef self.imported_from_html(url)- url = UrlHelper.normalized_encode(url)+ url = ERB::Util.html_escape(UrlHelper.normalized_encode(url))I18n.with_locale(SiteSetting.default_locale) do"\n<hr>\n<small>#{I18n.t("embed.imported_from", link: "<a href='#{url}'>#{url}</a>")}</small>\n"enddiff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rbindex 7a8c64ad275..dcbe34232df 100644--- a/spec/models/topic_embed_spec.rb+++ b/spec/models/topic_embed_spec.rb@@ -832,6 +832,15 @@ RSpec.describe TopicEmbed do"\n<hr>\n<small>This is a companion discussion topic for the original entry at <a href='http://www.discourse.org/%23%3C/a%3E%3Cimg%20src=x%20onerror=alert(%22document.domain%22);%3E'>http://www.discourse.org/%23%3C/a%3E%3Cimg%20src=x%20onerror=alert(%22document.domain%22);%3E</a></small>\n"expect(html).to eq(expected_html)end++ it "escapes the URL in the raw HTML footer" do+ url = "http://eviltrout.com/'onmouseover='window.topicEmbedXssExecuted=true"+ link = Nokogiri::HTML5.fragment(TopicEmbed.imported_from_html(url)).at_css("a")++ expect(link["href"]).to eq(url)+ expect(link["onmouseover"]).to be_nil+ expect(link.text).to eq(url)+ endenddescribe ".expanded_for" dodiff --git a/spec/system/topic_embed_spec.rb b/spec/system/topic_embed_spec.rbnew file mode 100644index 00000000000..1008a86a74a--- /dev/null+++ b/spec/system/topic_embed_spec.rb@@ -0,0 +1,31 @@+# frozen_string_literal: true++RSpec.describe "Imported topic embeds" do+ fab!(:embeddable_host) { Fabricate(:embeddable_host, host: "eviltrout.com") }++ let(:embed_url) { "http://eviltrout.com/'onmouseover='window.topicEmbedXssExecuted=true" }++ before do+ SiteSetting.content_security_policy = false+ SiteSetting.import_embed_unlisted = false+ Jobs.run_immediately!+ stub_request(:get, embed_url).to_return(+ status: 200,+ body: "<html><title>Embedded article</title><body><p>Article content</p></body></html>",+ )+ end++ it "does not execute JavaScript from an imported URL" do+ visit("/embed/comments?#{{ embed_url: embed_url }.to_query}")+ expect(page).to have_content(I18n.t("embed.loading"))++ visit(TopicEmbed.last.post.topic.url)+ link = find("#post_1 .cooked a", text: embed_url)++ page.execute_script("window.topicEmbedXssExecuted = false")+ link.hover++ expect(page.evaluate_script("window.topicEmbedXssExecuted")).to eq(false)+ expect(link["onmouseover"]).to be_nil+ end+end