fail send if redirected to other hostname

This commit is contained in:
Benjamin Neff 2016-10-24 00:22:15 +02:00
parent fb60f83926
commit d18e623082
2 changed files with 33 additions and 17 deletions

View file

@ -11,6 +11,7 @@ module DiasporaFederation
# @return [Hash] hydra opts # @return [Hash] hydra opts
def self.hydra_opts def self.hydra_opts
@hydra_opts ||= { @hydra_opts ||= {
followlocation: true,
maxredirs: DiasporaFederation.http_redirect_limit, maxredirs: DiasporaFederation.http_redirect_limit,
timeout: DiasporaFederation.http_timeout, timeout: DiasporaFederation.http_timeout,
method: :post, method: :post,
@ -62,9 +63,7 @@ module DiasporaFederation
# @param [Typhoeus::Request] request # @param [Typhoeus::Request] request
def prepare_request(request) def prepare_request(request)
request.on_complete do |response| request.on_complete do |response|
DiasporaFederation.callbacks.trigger(:update_pod, pod_url(response.effective_url), status(response)) success = validate_response_and_update_pod(request, response)
success = response.success?
log_line = "success=#{success} sender=#{@sender_id} obj=#{@obj_str} url=#{response.effective_url} " \ log_line = "success=#{success} sender=#{@sender_id} obj=#{@obj_str} url=#{response.effective_url} " \
"message=#{response.return_code} code=#{response.response_code} time=#{response.total_time}" "message=#{response.return_code} code=#{response.response_code} time=#{response.total_time}"
if success if success
@ -77,15 +76,20 @@ module DiasporaFederation
end end
end end
# Get the pod root-url from the send-url def validate_response_and_update_pod(request, response)
# @param [String] url url = URI.parse(request.url)
# @return [String] pod root-url effective_url = URI.parse(response.effective_url)
def pod_url(url) same_host = url.host == effective_url.host
URI.parse(url).tap {|uri| uri.path = "/" }.to_s
(response.success? && same_host).tap do |success|
pod_url = (success ? effective_url : url).tap {|uri| uri.path = "/" }.to_s
status = same_host ? status_from_response(response) : :redirected_to_other_hostname
DiasporaFederation.callbacks.trigger(:update_pod, pod_url, status)
end
end end
def status(res) def status_from_response(response)
res.return_code == :ok ? res.response_code : res.return_code response.return_code == :ok ? response.response_code : response.return_code
end end
end end
end end

View file

@ -92,6 +92,18 @@ module DiasporaFederation
hydra_wrapper.send hydra_wrapper.send
end end
it "fails if redirected to other hostname" do
expect_callback(:update_pod, "https://example.org/", 202)
expect_callback(:update_pod, "http://example.com/", :couldnt_resolve_host)
expect_callback(:update_pod, "http://example.net/", :redirected_to_other_hostname)
url3 = "http://example.net/receive/public"
Typhoeus.stub(url3).and_return(response)
hydra_wrapper.insert_job(url3, xml)
expect(hydra_wrapper.send).to eq([url2, url3])
end
end end
end end
end end