FIX: preserve github fragment URL

This commit is contained in:
Arpit Jalan 2018-12-19 11:57:07 +05:30
parent cae5ba7356
commit 1ab91f0474
3 changed files with 22 additions and 1 deletions

View File

@ -34,6 +34,7 @@ class FinalDestination
@opts = opts || {} @opts = opts || {}
@force_get_hosts = @opts[:force_get_hosts] || [] @force_get_hosts = @opts[:force_get_hosts] || []
@preserve_fragment_url_hosts = @opts[:preserve_fragment_url_hosts] || []
@opts[:max_redirects] ||= 5 @opts[:max_redirects] ||= 5
@opts[:lookup_ip] ||= lambda { |host| FinalDestination.lookup_ip(host) } @opts[:lookup_ip] ||= lambda { |host| FinalDestination.lookup_ip(host) }
@ -59,6 +60,7 @@ class FinalDestination
@limited_ips = [] @limited_ips = []
@verbose = @opts[:verbose] || false @verbose = @opts[:verbose] || false
@timeout = @opts[:timeout] || nil @timeout = @opts[:timeout] || nil
@preserve_fragment_url = @preserve_fragment_url_hosts.any? { |host| hostname_matches?(host) }
end end
def self.connection_timeout def self.connection_timeout
@ -210,6 +212,7 @@ class FinalDestination
if location if location
old_port = @uri.port old_port = @uri.port
location = "#{location}##{@uri.fragment}" if @preserve_fragment_url && @uri.fragment.present?
location = "#{@uri.scheme}://#{@uri.host}#{location}" if location[0] == "/" location = "#{@uri.scheme}://#{@uri.host}#{location}" if location[0] == "/"
@uri = uri(location) @uri = uri(location)
@limit -= 1 @limit -= 1

View File

@ -259,9 +259,13 @@ module Oneboxer
SiteSetting.onebox_domains_blacklist.split("|") SiteSetting.onebox_domains_blacklist.split("|")
end end
def self.preserve_fragment_url_hosts
['http://github.com']
end
def self.external_onebox(url) def self.external_onebox(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, ignore_redirects: ignore_redirects, ignore_hostnames: blacklisted_domains, force_get_hosts: force_get_hosts) fd = FinalDestination.new(url, ignore_redirects: ignore_redirects, ignore_hostnames: blacklisted_domains, force_get_hosts: force_get_hosts, preserve_fragment_url_hosts: preserve_fragment_url_hosts)
uri = fd.resolve uri = fd.resolve
return blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any? return blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any?

View File

@ -9,6 +9,8 @@ describe FinalDestination do
force_get_hosts: ['https://force.get.com'], force_get_hosts: ['https://force.get.com'],
preserve_fragment_url_hosts: ['https://eviltrout.com'],
# avoid IP lookups in test # avoid IP lookups in test
lookup_ip: lambda do |host| lookup_ip: lambda do |host|
case host case host
@ -266,6 +268,18 @@ describe FinalDestination do
expect(final.status).to eq(:resolved) expect(final.status).to eq(:resolved)
expect(final.cookie).to eq("bar=1; baz=2; foo=219ffwef9w0f") expect(final.cookie).to eq("bar=1; baz=2; foo=219ffwef9w0f")
end end
it "persists fragment url" do
origin_url = "https://eviltrout.com/origin/lib/code/foobar.rb"
upstream_url = "https://eviltrout.com/upstream/lib/code/foobar.rb"
redirect_response(origin_url, upstream_url)
stub_request(:head, upstream_url).to_return(doc_response)
final = FinalDestination.new("#{origin_url}#L154-L205", opts)
expect(final.resolve.to_s).to eq("#{upstream_url}#L154-L205")
expect(final.status).to eq(:resolved)
end
end end
describe '.get' do describe '.get' do