Merge pull request #128 from SuperTux88/dont-follow-redirects-for-federation

Don't follow redirects when federating messages
This commit is contained in:
Benjamin Neff 2023-06-11 15:59:55 +02:00
commit c8c252393e
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
2 changed files with 26 additions and 52 deletions

View file

@ -15,8 +15,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, followlocation: false,
maxredirs: DiasporaFederation.http_redirect_limit,
timeout: DiasporaFederation.http_timeout, timeout: DiasporaFederation.http_timeout,
method: :post, method: :post,
verbose: DiasporaFederation.http_verbose, verbose: DiasporaFederation.http_verbose,
@ -90,10 +89,10 @@ 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|
success = validate_response_and_update_pod(request, response) DiasporaFederation.callbacks.trigger(:update_pod, request.url, status_from_response(response))
log_line = "success=#{success} sender=#{@sender_id} obj=#{@obj_str} url=#{response.effective_url} " \ log_line = "success=#{response.success?} sender=#{@sender_id} obj=#{@obj_str} url=#{request.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 response.success?
logger.info(log_line) logger.info(log_line)
else else
logger.warn(log_line) logger.warn(log_line)
@ -103,18 +102,6 @@ module DiasporaFederation
end end
end end
def validate_response_and_update_pod(request, response)
url = URI.parse(request.url)
effective_url = URI.parse(response.effective_url)
same_host = url.host == effective_url.host
(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
def status_from_response(response) def status_from_response(response)
response.return_code == :ok ? response.response_code : response.return_code response.return_code == :ok ? response.response_code : response.return_code
end end

View file

@ -6,8 +6,8 @@ module DiasporaFederation
let(:obj_str) { "status_message@guid" } let(:obj_str) { "status_message@guid" }
let(:xml) { "<xml>post</xml>" } let(:xml) { "<xml>post</xml>" }
let(:json) { "{\"aes_key\": \"...\", \"encrypted_magic_envelope\": \"...\"}" } let(:json) { "{\"aes_key\": \"...\", \"encrypted_magic_envelope\": \"...\"}" }
let(:url) { "http://example.org/receive/public" } let(:url) { "https://example.org/receive/public" }
let(:url2) { "http://example.com/receive/public" } let(:url2) { "https://example.com/receive/public" }
let(:hydra) { Typhoeus::Hydra.new } let(:hydra) { Typhoeus::Hydra.new }
let(:hydra_wrapper) { Federation::Sender::HydraWrapper.new(sender_id, obj_str) } let(:hydra_wrapper) { Federation::Sender::HydraWrapper.new(sender_id, obj_str) }
@ -61,20 +61,18 @@ module DiasporaFederation
describe "#send" do describe "#send" do
let(:response) { let(:response) {
Typhoeus::Response.new( Typhoeus::Response.new(
code: 202, code: 202,
body: "", body: "",
time: 0.2, time: 0.2,
effective_url: url.sub("http://", "https://"), return_code: :ok
return_code: :ok
) )
} }
let(:error_response) { let(:error_response) {
Typhoeus::Response.new( Typhoeus::Response.new(
code: 0, code: 0,
body: "", body: "",
time: 0.2, time: 0.2,
effective_url: url2, return_code: :couldnt_resolve_host
return_code: :couldnt_resolve_host
) )
} }
@ -96,41 +94,30 @@ module DiasporaFederation
end end
it "calls the update_pod callback for all responses with effective_url and status" do it "calls the update_pod callback for all responses with effective_url and status" do
expect_callback(:update_pod, "https://example.org/", 202) expect_callback(:update_pod, url, 202)
expect_callback(:update_pod, "http://example.com/", :couldnt_resolve_host) expect_callback(:update_pod, url2, :couldnt_resolve_host)
hydra_wrapper.send hydra_wrapper.send
end end
it "calls the update_pod callback with http status code when there was no error" do it "calls the update_pod callback with http status code when there was no error" do
expect_callback(:update_pod, "https://example.org/", 202) not_found_url = "https://example.net/receive/not_found"
expect_callback(:update_pod, "http://example.net/", 404)
expect_callback(:update_pod, url, 202)
expect_callback(:update_pod, not_found_url, 404)
allow(DiasporaFederation.callbacks).to receive(:trigger) allow(DiasporaFederation.callbacks).to receive(:trigger)
not_found = Typhoeus::Response.new( not_found = Typhoeus::Response.new(
code: 404, code: 404,
body: "", body: "",
time: 0.2, time: 0.2,
effective_url: "http://example.net/", return_code: :ok
return_code: :ok
) )
Typhoeus.stub("http://example.net/receive/not_found").and_return(not_found) Typhoeus.stub(not_found_url).and_return(not_found)
hydra_wrapper.insert_magic_env_request("http://example.net/receive/not_found", xml) hydra_wrapper.insert_magic_env_request(not_found_url, xml)
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_magic_env_request(url3, xml)
expect(hydra_wrapper.send).to eq([url2, url3])
end
end end
end end
end end