Skip to content

Commit

Permalink
Merge pull request #128 from SuperTux88/dont-follow-redirects-for-fed…
Browse files Browse the repository at this point in the history
…eration

Don't follow redirects when federating messages
  • Loading branch information
SuperTux88 committed Jun 11, 2023
2 parents 62d7657 + 525c43e commit c8c2523
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 52 deletions.
21 changes: 4 additions & 17 deletions lib/diaspora_federation/federation/sender/hydra_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class HydraWrapper
# @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,
Expand Down Expand Up @@ -90,10 +89,10 @@ def hydra
# @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)
Expand All @@ -103,18 +102,6 @@ def prepare_request(request)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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
)
}

Expand All @@ -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

0 comments on commit c8c2523

Please sign in to comment.