From db485ae0da9391b7e4cac573d992f4d0ea2ab7e4 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 26 Jun 2017 15:38:23 -0400 Subject: [PATCH] FIX: Support for skipping redirects on certain domains (like steam) --- lib/final_destination.rb | 10 ++++++---- lib/oneboxer.rb | 6 +++++- spec/components/final_destination_spec.rb | 13 ++++++++++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/final_destination.rb b/lib/final_destination.rb index e9e226740e7d0b..c2a89c54ae1dab 100644 --- a/lib/final_destination.rb +++ b/lib/final_destination.rb @@ -20,6 +20,7 @@ def initialize(url, opts=nil) nil end end + @ignored = [Discourse.base_url_no_prefix] + (@opts[:ignore_redirects] || []) @limit = @opts[:max_redirects] @status = :ready @cookie = nil @@ -63,10 +64,11 @@ def resolve 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 6a190acd181122..1461894256bdb7 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -17,6 +17,10 @@ def changed? 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 @@ def self.onebox_cache_key(url) 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 826502cb2584ba..e716dfcdf329f5 100644 --- a/spec/components/final_destination_spec.rb +++ b/spec/components/final_destination_spec.rb @@ -4,7 +4,10 @@ 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 @@ 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 @@ def fd(url) 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)