A platform for community discussion. Free, open, simple.
SECURITY: Restrict untrusted oEmbed HTML to allowlisted iframes only
Joffrey JAFFEUXdde8bc4fb620
diff --git a/lib/onebox/engine/allowlisted_generic_onebox.rb b/lib/onebox/engine/allowlisted_generic_onebox.rbindex cd2ed191390..b5766ff5b02 100644--- a/lib/onebox/engine/allowlisted_generic_onebox.rb+++ b/lib/onebox/engine/allowlisted_generic_onebox.rb@@ -229,13 +229,18 @@ module Oneboxdef is_embedded?return false if data[:html].blank?- provider_hosts = AllowlistedGenericOnebox.html_providers[data[:provider_name]]- return true if provider_hosts && AllowlistedGenericOnebox.host_matches(uri, provider_hosts)- return false unless data[:html]["iframe"]+ return true if trusted_html_provider?- fragment = Nokogiri::HTML5.fragment(data[:html])- src = fragment.at_css("iframe")&.[]("src")- options[:allowed_iframe_regexes]&.any? { |r| src =~ r }+ iframes = Nokogiri::HTML5.fragment(data[:html]).css("iframe")+ iframes.present? &&+ iframes.all? do |iframe|+ options[:allowed_iframe_regexes]&.any? { |regex| iframe["src"] =~ regex }+ end+ end++ def trusted_html_provider?+ provider_hosts = AllowlistedGenericOnebox.html_providers[data[:provider_name]]+ !!provider_hosts && AllowlistedGenericOnebox.host_matches(uri, provider_hosts)enddef force_article_html?@@ -297,15 +302,20 @@ module Oneboxdef embedded_htmlfragment = Nokogiri::HTML5.fragment(data[:html])- fragment.css("img").each { |img| img["class"] = "thumbnail" }- if iframe = fragment.at_css("iframe")+ if trusted_html_provider?+ fragment.css("img").each { |img| img["class"] = "thumbnail" }+ return fragment.to_html+ end++ iframes = fragment.css("iframe")+ iframes.each do |iframe|iframe.remove_attribute("style")iframe["width"] = data[:width] || "100%"iframe["height"] = data[:height] if data[:height].present?iframe["scrolling"] = "no"iframe["frameborder"] = "0"end- fragment.to_html+ iframes.to_htmlendendenddiff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex 1546a83357f..844fcc14c49 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -2331,6 +2331,38 @@ RSpec.describe PostsController doexpect(cooked.at_css(".onebox-attack")).to be_nilend+ it "does not persist sibling HTML from oEmbed with an allowed iframe" do+ Jobs.run_immediately!+ url = "https://attacker.example.com/onebox"+ iframe_source = "https://www.youtube.com/embed/dQw4w9WgXcQ"++ stub_request(:head, url).to_return(status: 200)+ stub_request(:get, url).to_return(+ status: 200,+ body:+ '<html><head><link type="application/json+oembed" href="https://attacker.example.com/oembed"></head></html>',+ )+ stub_request(:get, "https://attacker.example.com/oembed").to_return(+ status: 200,+ body: {+ title: "Attacker onebox",+ type: "rich",+ html:+ "<iframe src=\"#{iframe_source}\"></iframe><style>.onebox-attack { position: fixed; inset: 0; z-index: 9999; }</style><div class=\"onebox-attack\">overlay</div>",+ }.to_json,+ )++ post "/posts.json", params: { raw: url, title: "Allowed iframe oEmbed" }++ expect(response.status).to eq(200)+ expect(response.body).to include(%("id":#{response.parsed_body["id"]}))++ cooked = Nokogiri::HTML5.fragment(Post.find(response.parsed_body["id"]).cooked)+ expect(cooked.at_css("iframe")["src"]).to eq(iframe_source)+ expect(cooked.at_css("style")).to be_nil+ expect(cooked.at_css(".onebox-attack")).to be_nil+ end+it "does not persist a backslash-bypassed wildcard iframe origin from oEmbed" doJobs.run_immediately!url = "https://attacker.example.com/onebox"