A platform for community discussion. Free, open, simple.
SECURITY: HTML injection in PrettyText.format_for_email from cooked-attribute reparsing
Mark VanLandinghamb25a074b1652
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex 1e618689247..8e5153c3277 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -20,6 +20,10 @@ module PrettyTextBLOCKED_HOTLINKED_SRC_ATTR = "data-blocked-hotlinked-src"BLOCKED_HOTLINKED_SRCSET_ATTR = "data-blocked-hotlinked-srcset"+ VIMEO_PLAYER_HOST = "player.vimeo.com"+ VIMEO_PLAYER_PATH = %r{\A/video/(?<id>\d+)/?\z}+ VIMEO_UNLISTED_HASH = /\A[a-zA-Z0-9]+\z/+@mutex = Mutex.new@ctx_init = Mutex.new@@ -533,7 +537,7 @@ module PrettyTextdef self.convert_hashtag_links_to_plaintext(doc)doc.css("a.hashtag-cooked")- .each { |hashtag| hashtag.replace("##{hashtag.attributes["data-slug"]}") }+ .each { |hashtag| hashtag.replace(doc.document.create_text_node("##{hashtag["data-slug"]}")) }enddef self.strip_links(string)@@ -580,16 +584,48 @@ module PrettyTextdoc.css("iframe[src*='player.vimeo.com']").each do |iframe|- if iframe["data-original-href"].present?- vimeo_url = UrlHelper.normalized_encode(iframe["data-original-href"])- else- vimeo_id = iframe["src"].split("/").last.sub("?h=", "/")- vimeo_url = "https://vimeo.com/#{vimeo_id}"- end- iframe.replace Nokogiri::HTML5.fragment("<p><a href='#{vimeo_url}'>#{vimeo_url}</a></p>")+ vimeo_url = vimeo_url_from_iframe(iframe)+ next if vimeo_url.blank?++ paragraph = doc.document.create_element("p")+ link = doc.document.create_element("a")+ link["href"] = vimeo_url+ link.content = vimeo_url+ paragraph.add_child(link)+ iframe.replace(paragraph)endend+ def self.vimeo_url_from_iframe(iframe)+ if iframe["data-original-href"].present?+ url = UrlHelper.normalized_encode(iframe["data-original-href"])+ scheme = UrlHelper.relaxed_parse(url)&.scheme+ return %w[http https].include?(scheme) ? url : nil+ end++ uri = UrlHelper.relaxed_parse(iframe["src"].to_s)+ return nil if uri.nil? || uri.host&.downcase != VIMEO_PLAYER_HOST++ video_id = VIMEO_PLAYER_PATH.match(uri.path.to_s)&.[](:id)+ return nil if video_id.nil?++ unlisted_hash = vimeo_unlisted_hash(uri.query)+ if unlisted_hash+ "https://vimeo.com/#{video_id}/#{unlisted_hash}"+ else+ "https://vimeo.com/#{video_id}"+ end+ end++ def self.vimeo_unlisted_hash(query)+ return nil if query.blank?++ hash = URI.decode_www_form(query).to_h["h"]+ hash if hash&.match?(VIMEO_UNLISTED_HASH)+ rescue ArgumentError+ nil+ end+def self.strip_secure_uploads(doc)# images inside a lightbox or other linkdoc@@ -684,14 +720,31 @@ module PrettyTextenddef self.secure_uploads_placeholder(doc, url, onebox_type: false, width: nil, height: nil)- data_width = width ? "data-width=#{width}" : ""- data_height = height ? "data-height=#{height}" : ""- data_onebox_type = onebox_type ? "data-onebox-type='#{onebox_type}'" : ""- <<~HTML- <div class="secure-upload-notice" data-stripped-secure-upload="#{url}" #{data_onebox_type} #{data_width} #{data_height}>- #{I18n.t("emails.secure_uploads_placeholder")} <a class='stripped-secure-view-upload' href="#{url}">#{I18n.t("emails.view_redacted_media")}</a>.- </div>- HTML+ notice = doc.document.create_element("div")+ notice["class"] = "secure-upload-notice"+ notice["data-stripped-secure-upload"] = url.to_s+ notice["data-onebox-type"] = onebox_type.to_s if onebox_type++ width = numeric_dimension(width)+ height = numeric_dimension(height)+ notice["data-width"] = width if width+ notice["data-height"] = height if height++ link = doc.document.create_element("a")+ link["class"] = "stripped-secure-view-upload"+ link["href"] = url.to_s+ link.content = I18n.t("emails.view_redacted_media")++ notice.add_child(+ doc.document.create_text_node("#{I18n.t("emails.secure_uploads_placeholder")} "),+ )+ notice.add_child(link)+ notice.add_child(doc.document.create_text_node("."))+ notice+ end++ def self.numeric_dimension(value)+ value.to_s[/\A\d+\z/]enddef self.format_for_email(html, post = nil)diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rbindex e5ff6a736f3..7dcf6f46c86 100644--- a/spec/lib/pretty_text_spec.rb+++ b/spec/lib/pretty_text_spec.rb@@ -1463,6 +1463,51 @@ RSpec.describe PrettyText do<p><a href="https://vimeo.com/1">https://vimeo.com/1</a></p>HTMLend++ it "leaves a malformed Vimeo source without rendering it as markup" do+ iframe_source = "https://player.vimeo.com/video/123'></a><img src=x onerror=alert(1)>"+ html = %(<iframe src="#{iframe_source}"></iframe>)++ fragment = Nokogiri::HTML5.fragment(described_class.format_for_email(html, post))++ aggregate_failures do+ expect(fragment.css("img")).to be_empty+ expect(fragment.at_css("iframe")["src"]).to eq(iframe_source)+ end+ end++ it "leaves a non-Vimeo iframe containing the player domain unchanged" do+ iframe_source =+ "https://www.instagram.com/?x=player.vimeo.com/<img src=x onerror=alert(1)>"+ html = %(<iframe src="#{iframe_source}"></iframe>)++ fragment = Nokogiri::HTML5.fragment(described_class.format_for_email(html, post))++ aggregate_failures do+ expect(fragment.css("a")).to be_empty+ expect(fragment.at_css("iframe")["src"]).to eq(iframe_source)+ end+ end++ it "converts an unlisted Vimeo embed using its hash" do+ html = %(<iframe src="https://player.vimeo.com/video/508864124?h=fcbbcc92fa"></iframe>)++ expect(described_class.format_for_email(html, post)).to match_html(<<~HTML)+ <p><a href="https://vimeo.com/508864124/fcbbcc92fa">https://vimeo.com/508864124/fcbbcc92fa</a></p>+ HTML+ end++ it "leaves an iframe with a non-http data-original-href unchanged" do+ html =+ %(<iframe src="https://player.vimeo.com/video/1" data-original-href="javascript:alert(1)"></iframe>)++ fragment = Nokogiri::HTML5.fragment(described_class.format_for_email(html, post))++ aggregate_failures do+ expect(fragment.css("a")).to be_empty+ expect(fragment.at_css("iframe")["data-original-href"]).to eq("javascript:alert(1)")+ end+ endenddescribe "#strip_secure_uploads" do@@ -1544,6 +1589,35 @@ RSpec.describe PrettyText doexpect(md).to include("data-width=\"20\"")expect(md).to include("data-height=\"20\"")end++ it "keeps a crafted secure upload URL as attribute data" do+ url =+ %(#{Discourse.base_url}/secure-uploads/original/1X/a"><img src=x onerror=alert(1)>b.png)++ fragment =+ Nokogiri::HTML5.fragment(described_class.format_for_email(%(<img src='#{url}'>), post))+ notice = fragment.at_css("div.secure-upload-notice")++ aggregate_failures do+ expect(fragment.css("img")).to be_empty+ expect(notice["data-stripped-secure-upload"]).to eq(url)+ end+ end++ it "discards non-numeric secure upload dimensions" do+ html =+ %(<img src="/secure-uploads/original/1X/testimage.png" width="20 onmouseover=alert(1) x" height="20">)++ notice =+ Nokogiri::HTML5.fragment(described_class.format_for_email(html, post)).at_css(+ "div.secure-upload-notice",+ )++ aggregate_failures do+ expect(notice.attributes.keys).not_to include("onmouseover", "data-width")+ expect(notice["data-height"]).to eq("20")+ end+ endendenddiff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rbindex f77bb568db6..5bc029c89bc 100644--- a/spec/requests/embed_controller_spec.rb+++ b/spec/requests/embed_controller_spec.rb@@ -213,6 +213,43 @@ RSpec.describe EmbedController doexpect(response.status).to eq(200)end++ fab!(:attacker, :trust_level_1)+ fab!(:existing_post) { Fabricate(:post, topic: topic) }++ it "does not reinterpret an allowlisted non-Vimeo iframe as Vimeo content" do+ iframe_source = "https://www.instagram.com/?x=player.vimeo.com/<img src=x onerror=alert(1)>"+ sign_in(attacker)++ post "/posts.json",+ params: {+ raw: %(<iframe src="#{iframe_source}"></iframe>),+ topic_id: topic.id,+ }++ expect(response.status).to eq(200)++ created_post = Post.find(response.parsed_body["id"])+ cooked_iframe = Nokogiri::HTML5.fragment(created_post.cooked).at_css("iframe")++ aggregate_failures do+ expect(created_post).to have_attributes(hidden: false, deleted_at: nil)+ expect(cooked_iframe["src"]).to eq(iframe_source)+ end++ sign_out+ get "/embed/comments", params: { topic_id: topic.id }++ document = Nokogiri.HTML5(response.body)+ rendered_iframe = document.at_css("article#post-#{created_post.id} .cooked > iframe")++ aggregate_failures do+ expect(response.status).to eq(200)+ expect(rendered_iframe).to be_present+ expect(rendered_iframe&.[]("src")).to eq(iframe_source)+ expect(document.css("img[onerror]")).to be_empty+ end+ endenddescribe "full_app redirect" do