Skip to content

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

FIX: Add onebox support for x.com (#27140)

Jan Cernik1ed1a1f96a98
diff --git a/lib/onebox/engine/twitter_status_onebox.rb b/lib/onebox/engine/twitter_status_onebox.rb
index d881ad7d913..1ecced5be4c 100644
--- a/lib/onebox/engine/twitter_status_onebox.rb
+++ b/lib/onebox/engine/twitter_status_onebox.rb
@@ -9,7 +9,7 @@ module Onebox
include ActionView::Helpers::NumberHelper
matches_regexp(
- %r{^https?://(mobile\.|www\.)?twitter\.com/.+?/status(es)?/\d+(/(video|photo)/\d?+)?+(/?\?.*)?/?$},
+ %r{^https?://(mobile\.|www\.)?(twitter\.com|x\.com)/.+?/status(es)?/\d+(/(video|photo)/\d?+)?+(/?\?.*)?/?$},
)
always_https
@@ -26,7 +26,13 @@ module Onebox
def get_twitter_data
response =
begin
- Onebox::Helpers.fetch_response(url, headers: http_params)
+ # We need to allow cross domain cookies to prevent an
+ # infinite redirect loop between twitter.com and x.com
+ Onebox::Helpers.fetch_response(
+ url,
+ headers: http_params,
+ allow_cross_domain_cookies: true,
+ )
rescue StandardError
return nil
end
@@ -45,7 +51,7 @@ module Onebox
end
def match
- @match ||= @url.match(%r{twitter\.com/.+?/status(es)?/(?<id>\d+)})
+ @match ||= @url.match(%r{(twitter\.com|x\.com)/.+?/status(es)?/(?<id>\d+)})
end
def twitter_data
diff --git a/lib/onebox/helpers.rb b/lib/onebox/helpers.rb
index ab807a875c3..f6f71eb705a 100644
--- a/lib/onebox/helpers.rb
+++ b/lib/onebox/helpers.rb
@@ -74,7 +74,8 @@ module Onebox
domain: nil,
headers: nil,
body_cacher: nil,
- raise_error_when_response_too_large: true
+ raise_error_when_response_too_large: true,
+ allow_cross_domain_cookies: false
)
redirect_limit = Onebox.options.redirect_limit if redirect_limit >
Onebox.options.redirect_limit
@@ -113,6 +114,7 @@ module Onebox
size_bytes = Onebox.options.max_download_kb * 1024
http.request(request) do |response|
if cookie = response.get_fields("set-cookie")
+ headers["Cookie"] = cookie.join("; ") if allow_cross_domain_cookies
# HACK: If this breaks again in the future, use HTTP::CookieJar from gem 'http-cookie'
# See test: it "does not send cookies to the wrong domain"
redir_header = { "Cookie" => cookie.join("; ") }
@@ -129,7 +131,8 @@ module Onebox
response["location"],
redirect_limit: redirect_limit - 1,
domain: "#{uri.scheme}://#{uri.host}",
- headers: redir_header,
+ headers: allow_cross_domain_cookies ? headers : redir_header,
+ allow_cross_domain_cookies: allow_cross_domain_cookies,
)
)
end