Don't follow redirects when federating messages

Federating uses POST requests, which don't work for redirects (unless
used with 307/308, but almost nobody uses these), so this was basically
broken anyway. The idea behind this was to follow http -> https
redirects, but as all pods nowadays have https already anyway, and
webfinger already enforces https, there is no need to follow redirects
anymore.
This commit is contained in:
Benjamin Neff 2022-12-05 04:52:14 +01:00
parent 62d7657dba
commit 525c43e67a
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
def self.hydra_opts
@hydra_opts ||= {
followlocation: true,
maxredirs: DiasporaFederation.http_redirect_limit,
followlocation: false,
timeout: DiasporaFederation.http_timeout,
method: :post,
verbose: DiasporaFederation.http_verbose,
@ -90,10 +89,10 @@ module DiasporaFederation
# @param [Typhoeus::Request] request
def prepare_request(request)
request.on_complete do |response|
success = validate_response_and_update_pod(request, response)
log_line = "success=#{success} sender=#{@sender_id} obj=#{@obj_str} url=#{response.effective_url} " \
DiasporaFederation.callbacks.trigger(:update_pod, request.url, status_from_response(response))
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}"
if success
if response.success?
logger.info(log_line)
else
logger.warn(log_line)
@ -103,18 +102,6 @@ module DiasporaFederation
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)
response.return_code == :ok ? response.response_code : response.return_code
end

View file

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