Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: Onebox iframe origin allowlist enforces URL authority boundary

Isaac Janzen92eec47e8b47
diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb
index 9065311442b..da5e13cb1ac 100644
--- a/lib/onebox/engine.rb
+++ b/lib/onebox/engine.rb
@@ -27,10 +27,21 @@ module Onebox
origins.map do |origin|
escaped_origin = Regexp.escape(origin)
if origin.start_with?("*.", "https://*.", "http://*.")
- escaped_origin = escaped_origin.sub("\\*", '\S*')
+ escaped_origin = escaped_origin.sub("\\*", "[^/?#]*")
end
- Regexp.new("\\A#{escaped_origin}", "i")
+ origin_boundary =
+ if origin.match?(%r{\Ahttps?://[^/?#]+\z}i)
+ if origin.match?(/:\d+\z/)
+ "(?:[/?#]|\\z)"
+ else
+ "(?::\\d+(?:[/?#]|\\z)|[/?#]|\\z)"
+ end
+ else
+ ""
+ end
+
+ Regexp.new("\\A#{escaped_origin}#{origin_boundary}", "i")
end
end
diff --git a/spec/lib/onebox/engine_spec.rb b/spec/lib/onebox/engine_spec.rb
index 361814e5feb..f9f4ecb6579 100644
--- a/spec/lib/onebox/engine_spec.rb
+++ b/spec/lib/onebox/engine_spec.rb
@@ -47,9 +47,46 @@ RSpec.describe Onebox::Engine do
end
describe "origins_to_regexes" do
- it "converts URLs to regexes" do
+ it "converts host-only URLs to regexes with URL boundaries" do
result = Onebox::Engine.origins_to_regexes(%w[https://example.com https://example2.com])
- expect(result).to eq([%r{\Ahttps://example\.com}i, %r{\Ahttps://example2\.com}i])
+ expect(result).to eq(
+ [
+ %r{\Ahttps://example\.com(?::\d+(?:[/?#]|\z)|[/?#]|\z)}i,
+ %r{\Ahttps://example2\.com(?::\d+(?:[/?#]|\z)|[/?#]|\z)}i,
+ ],
+ )
+ end
+
+ it "matches host-only URLs only at URL boundaries" do
+ regex = Onebox::Engine.origins_to_regexes(["https://example.com"]).first
+
+ %w[
+ https://example.com
+ https://example.com/embed
+ https://example.com?foo=bar
+ https://example.com#fragment
+ https://example.com:3000/embed
+ ].each { |url| expect(url).to match(regex) }
+
+ %w[
+ https://example.com.attacker.example/embed
+ https://example.com:3000.attacker.example/embed
+ ].each { |url| expect(url).not_to match(regex) }
+ end
+
+ it "limits wildcard origins to a single URL authority" do
+ regex = Onebox::Engine.origins_to_regexes(["https://*.example.com"]).first
+
+ %w[https://embed.example.com/player https://deep.embed.example.com/player].each do |url|
+ expect(url).to match(regex)
+ end
+
+ %w[
+ https://attacker.example/path.example.com/player
+ https://attacker.example?.example.com/
+ https://attacker.example#.example.com/
+ https://embed.example.com.attacker.example/player
+ ].each { |url| expect(url).not_to match(regex) }
end
it "treats '*' as a catch-all" do
diff --git a/spec/lib/oneboxer_spec.rb b/spec/lib/oneboxer_spec.rb
index 9ccba824763..645b8322e58 100644
--- a/spec/lib/oneboxer_spec.rb
+++ b/spec/lib/oneboxer_spec.rb
@@ -1109,6 +1109,77 @@ RSpec.describe Oneboxer do
)
end
+ it "blocks oEmbed iframes whose source only prefix-matches an allowed onebox origin" do
+ page_url = "https://attacker.example/page"
+ oembed_url = "https://attacker.example/oembed.json"
+ malicious_iframe_src = "https://www.youtube.com.attacker.example/payload"
+
+ page_body = <<~HTML
+ <html>
+ <head>
+ <meta property="og:type" content="website">
+ <link rel="alternate" type="application/json+oembed" href="#{oembed_url}">
+ </head>
+ </html>
+ HTML
+
+ oembed_body = {
+ type: "video",
+ version: "1.0",
+ title: "Interesting content",
+ provider_name: "ContentHost",
+ provider_url: "https://attacker.example",
+ width: 560,
+ height: 315,
+ html: "<iframe src='#{malicious_iframe_src}' width='560' height='315'></iframe>",
+ }
+
+ stub_request(:any, page_url).to_return(status: 200, body: page_body)
+ stub_request(:any, oembed_url).to_return(status: 200, body: oembed_body.to_json)
+
+ onebox = Oneboxer.onebox(page_url, invalidate_oneboxes: true)
+
+ expect(onebox).not_to include("<iframe")
+ expect(onebox).not_to include(malicious_iframe_src)
+ end
+
+ it "blocks oEmbed iframes whose wildcard allowlist suffix is only in a query or fragment" do
+ page_url = "https://attacker.example/wildcard-page"
+ oembed_url = "https://attacker.example/wildcard-oembed.json"
+ malicious_iframe_sources = %w[
+ https://attacker.example?.kaltura.com/
+ https://attacker.example#.kaltura.com/
+ ]
+
+ page_body = <<~HTML
+ <html>
+ <head>
+ <meta property="og:type" content="website">
+ <link rel="alternate" type="application/json+oembed" href="#{oembed_url}">
+ </head>
+ </html>
+ HTML
+
+ oembed_body = {
+ type: "video",
+ version: "1.0",
+ title: "Interesting content",
+ provider_name: "ContentHost",
+ provider_url: "https://attacker.example",
+ width: 560,
+ height: 315,
+ html: malicious_iframe_sources.map { |source| "<iframe src='#{source}'></iframe>" }.join,
+ }
+
+ stub_request(:any, page_url).to_return(status: 200, body: page_body)
+ stub_request(:any, oembed_url).to_return(status: 200, body: oembed_body.to_json)
+
+ onebox = Oneboxer.onebox(page_url, invalidate_oneboxes: true)
+
+ expect(onebox).not_to include("<iframe")
+ malicious_iframe_sources.each { |source| expect(onebox).not_to include(source) }
+ end
+
describe "missing attributes" do
before { stub_request(:head, url) }