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

This commit is contained in:
Jan Cernik
2024-05-23 10:25:42 -03:00
committed by GitHub
parent ebc3af90eb
commit 1ed1a1f96a
2 changed files with 14 additions and 5 deletions

View File

@ -9,7 +9,7 @@ module Onebox
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
matches_regexp( 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 always_https
@ -26,7 +26,13 @@ module Onebox
def get_twitter_data def get_twitter_data
response = response =
begin 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 rescue StandardError
return nil return nil
end end
@ -45,7 +51,7 @@ module Onebox
end end
def match 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 end
def twitter_data def twitter_data

View File

@ -74,7 +74,8 @@ module Onebox
domain: nil, domain: nil,
headers: nil, headers: nil,
body_cacher: 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 > redirect_limit = Onebox.options.redirect_limit if redirect_limit >
Onebox.options.redirect_limit Onebox.options.redirect_limit
@ -113,6 +114,7 @@ module Onebox
size_bytes = Onebox.options.max_download_kb * 1024 size_bytes = Onebox.options.max_download_kb * 1024
http.request(request) do |response| http.request(request) do |response|
if cookie = response.get_fields("set-cookie") 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' # 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" # See test: it "does not send cookies to the wrong domain"
redir_header = { "Cookie" => cookie.join("; ") } redir_header = { "Cookie" => cookie.join("; ") }
@ -129,7 +131,8 @@ module Onebox
response["location"], response["location"],
redirect_limit: redirect_limit - 1, redirect_limit: redirect_limit - 1,
domain: "#{uri.scheme}://#{uri.host}", 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 end