A platform for community discussion. Free, open, simple.
SECURITY: validate oEmbed provider_name against URL host in generic Onebox (#42217)
Isaac Janzenbdacc74ab381
diff --git a/lib/onebox/engine/allowlisted_generic_onebox.rb b/lib/onebox/engine/allowlisted_generic_onebox.rbindex 622ac8d74ea..cd2ed191390 100644--- a/lib/onebox/engine/allowlisted_generic_onebox.rb+++ b/lib/onebox/engine/allowlisted_generic_onebox.rb@@ -18,7 +18,7 @@ module Onebox# include the entire page HTML. However for some providers like Flickr it allows us# to return gifv and galleries.def self.default_html_providers- %w[Flickr Meetup]+ { "Flickr" => ["flickr.com"], "Meetup" => ["meetup.com"] }enddef self.html_providers@@ -229,7 +229,8 @@ module Oneboxdef is_embedded?return false if data[:html].blank?- return true if AllowlistedGenericOnebox.html_providers.include?(data[:provider_name])+ 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"]fragment = Nokogiri::HTML5.fragment(data[:html])diff --git a/spec/lib/onebox/engine/allowlisted_generic_onebox_spec.rb b/spec/lib/onebox/engine/allowlisted_generic_onebox_spec.rbindex bb9eedcee9e..616fe08a8e9 100644--- a/spec/lib/onebox/engine/allowlisted_generic_onebox_spec.rb+++ b/spec/lib/onebox/engine/allowlisted_generic_onebox_spec.rb@@ -19,13 +19,13 @@ RSpec.describe Onebox::Engine::AllowlistedGenericOnebox doendend- it "doesn't return the HTML when not in the `html_providers`" do- Onebox::Engine::AllowlistedGenericOnebox.html_providers = []+ it "doesn't return the HTML when the provider does not match the URL host" do+ Onebox::Engine::AllowlistedGenericOnebox.html_providers = {}expect(HTMLOnebox.new("http://coolsite.com").to_html).to be_nilend- it "returns the HMTL when in the `html_providers`" do- Onebox::Engine::AllowlistedGenericOnebox.html_providers = ["CoolSite"]+ it "returns the HTML when the provider matches the URL host" do+ Onebox::Engine::AllowlistedGenericOnebox.html_providers = { "CoolSite" => ["coolsite.com"] }expect(HTMLOnebox.new("http://coolsite.com").to_html).to eq "cool html"endenddiff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex bf98345e785..7d0adff6bb5 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -2019,6 +2019,35 @@ RSpec.describe PostsController doendend+ it "does not persist HTML from an untrusted oEmbed provider" do+ Jobs.run_immediately!+ url = "https://attacker.example.com/onebox"+ malicious_html =+ '<div class="onebox-attack" style="position: fixed; inset: 0; z-index: 9999">overlay</div>'++ 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",+ provider_name: "Flickr",+ html: malicious_html,+ }.to_json,+ )++ post "/posts.json", params: { raw: url, title: "Untrusted oEmbed provider" }++ expect(response.status).to eq(200)+ cooked = Nokogiri::HTML5.fragment(Post.find(response.parsed_body["id"]).cooked)+ expect(cooked.at_css(".onebox-attack")).to be_nil+ end+it "creates the topic and post with the right attributes" dopost "/posts.json",params: {