Skip to content

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

FIX: Respect `blocked_onebox_domains` for Twitter/X and handle empty oneboxes (#38868)

Régis Hanol4886c597d276
diff --git a/lib/final_destination.rb b/lib/final_destination.rb
index f2342eff0c3..924b7ae5b34 100644
--- a/lib/final_destination.rb
+++ b/lib/final_destination.rb
@@ -230,6 +230,11 @@ class FinalDestination
return
end
+ if @stop_at_blocked_pages && blocked_domain?(@uri)
+ @status = :blocked_page
+ return
+ end
+
@ignored.each do |ignored|
if @uri&.hostname == ignored[:hostname] &&
@uri.path.start_with?(ignored[:path].presence || "/")
diff --git a/lib/onebox/engine/twitter_status_onebox.rb b/lib/onebox/engine/twitter_status_onebox.rb
index 77de26e6db6..f34d2ec084c 100644
--- a/lib/onebox/engine/twitter_status_onebox.rb
+++ b/lib/onebox/engine/twitter_status_onebox.rb
@@ -15,8 +15,29 @@ module Onebox
path.match?(%r{^/.+?/status(es)?/\d+(/(video|photo)/\d?)?(/?\?.*)?/?$})
end
- def to_html
- raw.present? ? super : ""
+ def to_html(ignore_errors = false)
+ return "" if raw.blank?
+
+ unless ignore_errors
+ verified_data
+ return "" if errors.present?
+ end
+
+ super()
+ end
+
+ def verified_data
+ @verified_data ||=
+ begin
+ result = data
+ %i[title tweet].each do |tag|
+ if result[tag].blank?
+ errors[tag] ||= []
+ errors[tag] << "is blank"
+ end
+ end
+ result
+ end
end
private
diff --git a/spec/lib/onebox/engine/twitter_status_onebox_spec.rb b/spec/lib/onebox/engine/twitter_status_onebox_spec.rb
index d345410c528..a33bf3577aa 100644
--- a/spec/lib/onebox/engine/twitter_status_onebox_spec.rb
+++ b/spec/lib/onebox/engine/twitter_status_onebox_spec.rb
@@ -171,16 +171,11 @@ RSpec.describe Onebox::Engine::TwitterStatusOnebox do
end
include_context "with engines"
- let(:tweet_content) { "I&#39;ve never played Minecraft" }
- let(:full_name) { "Marques Brownlee" }
- let(:screen_name) { "MKBHD" }
- let(:avatar) { "" }
- let(:timestamp) { "" }
- let(:favorite_count) { "" }
- let(:retweets_count) { "" }
-
it_behaves_like "an engine"
- it_behaves_like "#to_html"
+
+ it "returns empty html when tweet data is missing" do
+ expect(described_class.new(@link).to_html).to eq("")
+ end
end
context "with twitter client" do
diff --git a/spec/lib/oneboxer_spec.rb b/spec/lib/oneboxer_spec.rb
index 11bef8dda2a..3aaef4314ff 100644
--- a/spec/lib/oneboxer_spec.rb
+++ b/spec/lib/oneboxer_spec.rb
@@ -441,6 +441,14 @@ RSpec.describe Oneboxer do
expect(result[:preview]).to be_empty
end
+ it "does not return onebox for domains in ignore_redirects when blocked" do
+ SiteSetting.blocked_onebox_domains = "x.com"
+
+ result = Oneboxer.external_onebox("https://x.com/someone/status/123")
+ expect(result[:onebox]).to be_empty
+ expect(result[:preview]).to be_empty
+ end
+
it "does not return onebox if the Discourse-No-Onebox header == 1" do
stub_request(:get, "https://website.com/discourse-no-onebox").to_return(
status: 200,