diff --git a/lib/final_destination.rb b/lib/final_destination.rb index e9e226740e7..c2a89c54ae1 100644 --- a/lib/final_destination.rb +++ b/lib/final_destination.rb @@ -20,6 +20,7 @@ class FinalDestination nil end end + @ignored = [Discourse.base_url_no_prefix] + (@opts[:ignore_redirects] || []) @limit = @opts[:max_redirects] @status = :ready @cookie = nil @@ -63,10 +64,11 @@ class FinalDestination return nil end - # Always allow current base url - if hostname_matches?(Discourse.base_url_no_prefix) - @status = :resolved - return @uri + @ignored.each do |host| + if hostname_matches?(host) + @status = :resolved + return @uri + end end return nil unless validate_uri diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index 6a190acd181..1461894256b 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -17,6 +17,10 @@ module Oneboxer end end + def self.ignore_redirects + @ignore_redirects ||= ['http://store.steampowered.com'] + end + def self.preview(url, options=nil) options ||= {} invalidate(url) if options[:invalidate_oneboxes] @@ -144,7 +148,7 @@ module Oneboxer def self.onebox_raw(url) 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 return blank_onebox if uri.blank? || SiteSetting.onebox_domains_blacklist.include?(uri.hostname) options = { diff --git a/spec/components/final_destination_spec.rb b/spec/components/final_destination_spec.rb index 826502cb258..e716dfcdf32 100644 --- a/spec/components/final_destination_spec.rb +++ b/spec/components/final_destination_spec.rb @@ -4,7 +4,10 @@ require 'final_destination' describe FinalDestination 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| case host 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 'private-host.com' then '192.168.10.1' when 'internal-ipv6.com' then '2001:abc:de:01:3:3d0:6a65:c2bf' + when 'ignore-me.com' then '53.84.143.152' else as_ip = IPAddr.new(host) rescue nil raise "couldn't lookup #{host}" if as_ip.nil? @@ -64,6 +68,13 @@ describe FinalDestination do 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 before do stub_request(:head, 'https://some_thing.example.com').to_return(doc_response)