FIX: Support for skipping redirects on certain domains (like steam)

This commit is contained in:
Robin Ward 2017-06-26 15:38:23 -04:00
parent 8967d50dc2
commit db485ae0da
3 changed files with 23 additions and 6 deletions

View File

@ -20,6 +20,7 @@ class FinalDestination
nil nil
end end
end end
@ignored = [Discourse.base_url_no_prefix] + (@opts[:ignore_redirects] || [])
@limit = @opts[:max_redirects] @limit = @opts[:max_redirects]
@status = :ready @status = :ready
@cookie = nil @cookie = nil
@ -63,11 +64,12 @@ class FinalDestination
return nil return nil
end end
# Always allow current base url @ignored.each do |host|
if hostname_matches?(Discourse.base_url_no_prefix) if hostname_matches?(host)
@status = :resolved @status = :resolved
return @uri return @uri
end end
end
return nil unless validate_uri return nil unless validate_uri
headers = request_headers headers = request_headers

View File

@ -17,6 +17,10 @@ module Oneboxer
end end
end end
def self.ignore_redirects
@ignore_redirects ||= ['http://store.steampowered.com']
end
def self.preview(url, options=nil) def self.preview(url, options=nil)
options ||= {} options ||= {}
invalidate(url) if options[:invalidate_oneboxes] invalidate(url) if options[:invalidate_oneboxes]
@ -144,7 +148,7 @@ module Oneboxer
def self.onebox_raw(url) def self.onebox_raw(url)
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
fd = FinalDestination.new(url) fd = FinalDestination.new(url, ignore_redirects: ignore_redirects)
uri = fd.resolve uri = fd.resolve
return blank_onebox if uri.blank? || SiteSetting.onebox_domains_blacklist.include?(uri.hostname) return blank_onebox if uri.blank? || SiteSetting.onebox_domains_blacklist.include?(uri.hostname)
options = { options = {

View File

@ -4,7 +4,10 @@ require 'final_destination'
describe FinalDestination do describe FinalDestination do
let(:opts) do let(:opts) do
{ # avoid IP lookups in test {
ignore_redirects: ['https://ignore-me.com'],
# avoid IP lookups in test
lookup_ip: lambda do |host| lookup_ip: lambda do |host|
case host case host
when 'eviltrout.com' then '52.84.143.152' when 'eviltrout.com' then '52.84.143.152'
@ -13,6 +16,7 @@ describe FinalDestination do
when 'some_thing.example.com' then '104.25.152.10' when 'some_thing.example.com' then '104.25.152.10'
when 'private-host.com' then '192.168.10.1' when 'private-host.com' then '192.168.10.1'
when 'internal-ipv6.com' then '2001:abc:de:01:3:3d0:6a65:c2bf' when 'internal-ipv6.com' then '2001:abc:de:01:3:3d0:6a65:c2bf'
when 'ignore-me.com' then '53.84.143.152'
else else
as_ip = IPAddr.new(host) rescue nil as_ip = IPAddr.new(host) rescue nil
raise "couldn't lookup #{host}" if as_ip.nil? raise "couldn't lookup #{host}" if as_ip.nil?
@ -64,6 +68,13 @@ describe FinalDestination do
end end
end end
it "ignores redirects" do
final = FinalDestination.new('https://ignore-me.com/some-url', opts)
expect(final.resolve.to_s).to eq('https://ignore-me.com/some-url')
expect(final.redirected?).to eq(false)
expect(final.status).to eq(:resolved)
end
context "underscores in URLs" do context "underscores in URLs" do
before do before do
stub_request(:head, 'https://some_thing.example.com').to_return(doc_response) stub_request(:head, 'https://some_thing.example.com').to_return(doc_response)