Skip to content

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

FEATURE: Add onebox support for restream.io (#42487)

Kris6f989cb385be
diff --git a/app/assets/stylesheets/common/base/onebox.scss b/app/assets/stylesheets/common/base/onebox.scss
index d23eeae8ad7..7860f8c0075 100644
--- a/app/assets/stylesheets/common/base/onebox.scss
+++ b/app/assets/stylesheets/common/base/onebox.scss
@@ -1172,7 +1172,8 @@ aside.onebox.mixcloud-preview {
}
iframe.vimeo-onebox,
-iframe.loom-onebox {
+iframe.loom-onebox,
+iframe.restream-onebox {
width: 100%;
height: auto;
aspect-ratio: 16/9;
diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb
index da5e13cb1ac..733f10a3f54 100644
--- a/lib/onebox/engine.rb
+++ b/lib/onebox/engine.rb
@@ -253,6 +253,7 @@ require_relative "engine/gitlab_blob_onebox"
require_relative "engine/google_photos_onebox"
require_relative "engine/kaltura_onebox"
require_relative "engine/reddit_media_onebox"
+require_relative "engine/restream_onebox"
require_relative "engine/google_drive_onebox"
require_relative "engine/facebook_media_onebox"
require_relative "engine/hackernews_onebox"
diff --git a/lib/onebox/engine/restream_onebox.rb b/lib/onebox/engine/restream_onebox.rb
new file mode 100644
index 00000000000..f5b4d4794a2
--- /dev/null
+++ b/lib/onebox/engine/restream_onebox.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+module Onebox
+ module Engine
+ class RestreamOnebox
+ include Engine
+ include StandardEmbed
+
+ matches_domain("player.restream.io")
+ always_https
+ requires_iframe_origins "https://player.restream.io"
+
+ def self.matches_path(path)
+ path.nil? || path.empty? || path == "/"
+ end
+
+ def placeholder_html
+ ::Onebox::Helpers.video_placeholder_html
+ end
+
+ def to_html
+ width = @options[:max_width] || 695
+ height = (width * 9.0 / 16).round
+
+ <<~HTML
+ <iframe
+ class="restream-onebox"
+ src="#{url}"
+ width="#{width}"
+ height="#{height}"
+ frameborder="0"
+ allowfullscreen
+ ></iframe>
+ HTML
+ end
+ end
+ end
+end
diff --git a/spec/lib/onebox/engine/restream_onebox_spec.rb b/spec/lib/onebox/engine/restream_onebox_spec.rb
new file mode 100644
index 00000000000..2b49fd4c419
--- /dev/null
+++ b/spec/lib/onebox/engine/restream_onebox_spec.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+RSpec.describe Onebox::Engine::RestreamOnebox do
+ describe ".===" do
+ it "matches a tokenized player URL" do
+ url = URI("https://player.restream.io/?token=624a83cddea44b6788cc52b38ce78748")
+ expect(described_class === url).to eq(true)
+ end
+
+ it "matches the bare player URL" do
+ url = URI("https://player.restream.io/")
+ expect(described_class === url).to eq(true)
+ end
+
+ it "does not match a path on the player host" do
+ url = URI("https://player.restream.io/some/path")
+ expect(described_class === url).to eq(false)
+ end
+
+ it "does not match an unrelated host" do
+ url = URI("https://restream.io/")
+ expect(described_class === url).to eq(false)
+ end
+
+ it "does not treat a lookalike host as the player" do
+ url = URI("https://player.restream.io.evil.example/?token=x")
+ expect(described_class === url).to eq(false)
+ end
+ end
+
+ describe "iframe origin" do
+ it "allowlists the player origin so Discourse keeps the iframe" do
+ expect(Onebox::Engine.all_iframe_origins).to include("https://player.restream.io")
+ end
+ end
+
+ describe "#to_html" do
+ it "is registered so the matcher selects it for a player URL" do
+ options = {
+ allowed_iframe_regexes:
+ Onebox::Engine.origins_to_regexes(Onebox::Engine.all_iframe_origins),
+ }
+
+ expect(Onebox::Matcher.new("https://player.restream.io/?token=abc", options).oneboxed).to eq(
+ described_class,
+ )
+ end
+
+ it "renders the player iframe in the sanitized onebox output" do
+ html = Onebox.preview("https://player.restream.io/?token=abc").to_s
+
+ expect(html).to include('class="restream-onebox"')
+ expect(html).to include('src="https://player.restream.io/?token=abc"')
+ expect(html).to include("allowfullscreen")
+ # The core onebox sanitizer does not permit the `allow` attribute, so
+ # autoplay delegation is intentionally not emitted
+ expect(html).not_to include("autoplay")
+ end
+ end
+end