[#534] Made federation push sender be determined basing on content instead of `refere...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 29 Jan 2019 10:12:28 +0000 (13:12 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 29 Jan 2019 10:12:28 +0000 (13:12 +0300)
15 files changed:
lib/pleroma/plugs/set_requester_reachable_plug.ex [deleted file]
lib/pleroma/reverse_proxy.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/activity_pub_controller.ex
lib/pleroma/web/ostatus/ostatus.ex
lib/pleroma/web/ostatus/ostatus_controller.ex
lib/pleroma/web/salmon/salmon.ex
lib/pleroma/web/websub/websub.ex
lib/pleroma/web/websub/websub_controller.ex
test/web/activity_pub/activity_pub_controller_test.exs
test/web/instances/instances_test.exs
test/web/ostatus/incoming_documents/delete_handling_test.exs
test/web/ostatus/ostatus_controller_test.exs
test/web/ostatus/ostatus_test.exs
test/web/websub/websub_controller_test.exs

diff --git a/lib/pleroma/plugs/set_requester_reachable_plug.ex b/lib/pleroma/plugs/set_requester_reachable_plug.ex
deleted file mode 100644 (file)
index 88551be..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.Web.Plugs.SetRequesterReachablePlug do
-  import Plug.Conn
-
-  def init(_), do: []
-
-  def call(%Plug.Conn{} = conn, _) do
-    with [referer] <- get_req_header(conn, "referer"),
-         do: Pleroma.Instances.set_reachable(referer)
-
-    conn
-  end
-end
index d8b17212bf8eaa7d69b4d89534fe012c583a4072..a25b5ea4e16f0af9c804d65a53a4fd4a7afd3fd4 100644 (file)
@@ -3,8 +3,7 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.ReverseProxy do
-  @keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since if-unmodified-since) ++
-                      ~w(if-none-match if-range range referer)
+  @keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since if-unmodified-since if-none-match if-range range)
   @resp_cache_headers ~w(etag date last-modified cache-control)
   @keep_resp_headers @resp_cache_headers ++
                        ~w(content-type content-disposition content-encoding content-range accept-ranges vary)
index 22c7824fa23c3e8862b5cf23b2e6a7ee62650f90..4016808e8e72b5706731173550f48279bb06dd51 100644 (file)
@@ -784,8 +784,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
                [
                  {"Content-Type", "application/activity+json"},
                  {"signature", signature},
-                 {"digest", digest},
-                 {"referer", Pleroma.Web.Endpoint.url()}
+                 {"digest", digest}
                ]
              ) do
       Instances.set_reachable(inbox)
index fadb038a21a9f2e486123715b118130be59d6062..4dea6ab83ee21ac87ee92f750151ef9751d8a76e 100644 (file)
@@ -18,7 +18,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
   action_fallback(:errors)
 
   plug(Pleroma.Web.FederatingPlug when action in [:inbox, :relay])
-  plug(Pleroma.Web.Plugs.SetRequesterReachablePlug when action in [:inbox])
+  plug(:set_requester_reachable when action in [:inbox])
   plug(:relay_active? when action in [:relay])
 
   def relay_active?(conn, _) do
@@ -291,4 +291,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
     |> put_status(500)
     |> json("error")
   end
+
+  defp set_requester_reachable(%Plug.Conn{} = conn, _) do
+    with actor <- conn.params["actor"],
+         true <- is_binary(actor) do
+      Pleroma.Instances.set_reachable(actor)
+    end
+
+    conn
+  end
 end
index a3155b79dd07db2ca586f7800e1f7aa75f8a57f7..a20ca17bbe7ce8498bdb3cf9faa3e3d158fd920d 100644 (file)
@@ -48,6 +48,9 @@ defmodule Pleroma.Web.OStatus do
 
   def handle_incoming(xml_string) do
     with doc when doc != :error <- parse_document(xml_string) do
+      with {:ok, actor_user} <- find_make_or_update_user(doc),
+           do: Pleroma.Instances.set_reachable(actor_user.ap_id)
+
       entries = :xmerl_xpath.string('//entry', doc)
 
       activities =
index 9392a97f0e6ca32c997ec694669010ad5fdc9eab..302ff38a457716b06e557892c2315ce7aa1d6e0d 100644 (file)
@@ -14,7 +14,6 @@ defmodule Pleroma.Web.OStatus.OStatusController do
   alias Pleroma.Web.ActivityPub.ActivityPub
 
   plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
-  plug(Pleroma.Web.Plugs.SetRequesterReachablePlug when action in [:salmon_incoming])
 
   action_fallback(:errors)
 
index e96455423a7abac7549e727386bf0ec50882a476..07ca42a5f0aef095c94909b3ad9c4f3b6792c709 100644 (file)
@@ -172,10 +172,7 @@ defmodule Pleroma.Web.Salmon do
            poster.(
              url,
              feed,
-             [
-               {"Content-Type", "application/magic-envelope+xml"},
-               {"referer", Pleroma.Web.Endpoint.url()}
-             ]
+             [{"Content-Type", "application/magic-envelope+xml"}]
            ) do
       Instances.set_reachable(url)
       Logger.debug(fn -> "Pushed to #{url}, code #{code}" end)
index abe1482702fac01a1c75d42a6e179ffc312bf952..8f7d53b03f598da82d15e12d8cbd2b5da4ebf546 100644 (file)
@@ -278,8 +278,7 @@ defmodule Pleroma.Web.Websub do
              xml,
              [
                {"Content-Type", "application/atom+xml"},
-               {"X-Hub-Signature", "sha1=#{signature}"},
-               {"referer", Pleroma.Web.Endpoint.url()}
+               {"X-Hub-Signature", "sha1=#{signature}"}
              ]
            ) do
       Instances.set_reachable(callback)
index 9da7e70a1d6ebad14f0275ff89b10e73c67c9789..a92dfe87b6cd0191bc412d10e1f960f9a05adc6d 100644 (file)
@@ -20,8 +20,6 @@ defmodule Pleroma.Web.Websub.WebsubController do
          ]
   )
 
-  plug(Pleroma.Web.Plugs.SetRequesterReachablePlug when action in [:websub_incoming])
-
   def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
     user = User.get_cached_by_nickname(nickname)
 
index eca5c134dc52a072abefeb3ef78591f54ee244d3..d3dd160dd92be987ce92988e16863ad5ec52e8b9 100644 (file)
@@ -145,17 +145,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
     end
 
     test "it clears `unreachable` federation status of the sender", %{conn: conn} do
-      sender_url = "https://pleroma.soykaf.com"
+      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
+
+      sender_url = data["actor"]
       Instances.set_consistently_unreachable(sender_url)
       refute Instances.reachable?(sender_url)
 
-      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
-
       conn =
         conn
         |> assign(:valid_signature, true)
         |> put_req_header("content-type", "application/activity+json")
-        |> put_req_header("referer", sender_url)
         |> post("/inbox", data)
 
       assert "ok" == json_response(conn, 200)
@@ -210,10 +209,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
     end
 
     test "it clears `unreachable` federation status of the sender", %{conn: conn} do
-      sender_host = "pleroma.soykaf.com"
-      Instances.set_consistently_unreachable(sender_host)
-      refute Instances.reachable?(sender_host)
-
       user = insert(:user)
 
       data =
@@ -221,11 +216,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
         |> Poison.decode!()
         |> Map.put("bcc", [user.ap_id])
 
+      sender_host = URI.parse(data["actor"]).host
+      Instances.set_consistently_unreachable(sender_host)
+      refute Instances.reachable?(sender_host)
+
       conn =
         conn
         |> assign(:valid_signature, true)
         |> put_req_header("content-type", "application/activity+json")
-        |> put_req_header("referer", "https://#{sender_host}")
         |> post("/users/#{user.nickname}/inbox", data)
 
       assert "ok" == json_response(conn, 200)
index a2fdf101940340df47e8c037ef9687872080f906..adb8560a7823a49c69663b53da78282d155da1d1 100644 (file)
@@ -39,6 +39,11 @@ defmodule Pleroma.InstancesTest do
       assert Instances.reachable?(url)
       assert Instances.reachable?(URI.parse(url).host)
     end
+
+    test "returns true on non-binary input" do
+      assert Instances.reachable?(nil)
+      assert Instances.reachable?(1)
+    end
   end
 
   describe "filter_reachable/1" do
@@ -71,6 +76,19 @@ defmodule Pleroma.InstancesTest do
       Instances.set_reachable(url)
       assert Instances.reachable?(url)
     end
+
+    test "returns error status on non-binary input" do
+      assert {:error, _} = Instances.set_reachable(nil)
+      assert {:error, _} = Instances.set_reachable(1)
+    end
+  end
+
+  # Note: implementation-specific (e.g. Instance) details of set_unreachable/1 should be tested in implementation-specific tests
+  describe "set_unreachable/1" do
+    test "returns error status on non-binary input" do
+      assert {:error, _} = Instances.set_unreachable(nil)
+      assert {:error, _} = Instances.set_unreachable(1)
+    end
   end
 
   describe "set_consistently_unreachable/1" do
index c8fbff6cc7741e3298185ca551d1dbee0eac1cec..d97cd79f4df10773e071bb6635887378ccc9c636 100644 (file)
@@ -2,9 +2,16 @@ defmodule Pleroma.Web.OStatus.DeleteHandlingTest do
   use Pleroma.DataCase
 
   import Pleroma.Factory
+  import Tesla.Mock
+
   alias Pleroma.{Repo, Activity, Object}
   alias Pleroma.Web.OStatus
 
+  setup do
+    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
+    :ok
+  end
+
   describe "deletions" do
     test "it removes the mentioned activity" do
       note = insert(:note_activity)
index cba12b3f755194047f9b92a694db548cbeb3ac24..3145ca9a1ab7a010f3364f84e67f717962ed91e8 100644 (file)
@@ -5,7 +5,7 @@
 defmodule Pleroma.Web.OStatus.OStatusControllerTest do
   use Pleroma.Web.ConnCase
   import Pleroma.Factory
-  alias Pleroma.{User, Repo, Object, Instances}
+  alias Pleroma.{User, Repo, Object}
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.OStatus.ActivityRepresenter
 
@@ -59,24 +59,6 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
 
       assert response(conn, 200)
     end
-
-    test "it clears `unreachable` federation status of the sender", %{conn: conn} do
-      sender_url = "https://pleroma.soykaf.com"
-      Instances.set_consistently_unreachable(sender_url)
-      refute Instances.reachable?(sender_url)
-
-      user = insert(:user)
-      salmon = File.read!("test/fixtures/salmon.xml")
-
-      conn =
-        conn
-        |> put_req_header("content-type", "application/atom+xml")
-        |> put_req_header("referer", sender_url)
-        |> post("/users/#{user.nickname}/salmon", salmon)
-
-      assert response(conn, 200)
-      assert Instances.reachable?(sender_url)
-    end
   end
 
   test "gets a feed", %{conn: conn} do
index 403cc7095ddb077839fc74bca266cbfab04bdce3..0c63dd84dae8a84e311fa8ce2c903cac224affb7 100644 (file)
@@ -6,7 +6,7 @@ defmodule Pleroma.Web.OStatusTest do
   use Pleroma.DataCase
   alias Pleroma.Web.OStatus
   alias Pleroma.Web.XML
-  alias Pleroma.{Object, Repo, User, Activity}
+  alias Pleroma.{Object, Repo, User, Activity, Instances}
   import Pleroma.Factory
   import ExUnit.CaptureLog
 
@@ -311,6 +311,22 @@ defmodule Pleroma.Web.OStatusTest do
     refute User.following?(follower, followed)
   end
 
+  test "it clears `unreachable` federation status of the sender" do
+    incoming_reaction_xml = File.read!("test/fixtures/share-gs.xml")
+    doc = XML.parse_document(incoming_reaction_xml)
+    actor_uri = XML.string_from_xpath("//author/uri[1]", doc)
+    reacted_to_author_uri = XML.string_from_xpath("//author/uri[2]", doc)
+
+    Instances.set_consistently_unreachable(actor_uri)
+    Instances.set_consistently_unreachable(reacted_to_author_uri)
+    refute Instances.reachable?(actor_uri)
+    refute Instances.reachable?(reacted_to_author_uri)
+
+    {:ok, _} = OStatus.handle_incoming(incoming_reaction_xml)
+    assert Instances.reachable?(actor_uri)
+    refute Instances.reachable?(reacted_to_author_uri)
+  end
+
   describe "new remote user creation" do
     test "returns local users" do
       local_user = insert(:user)
index cb19d6fe60e87f73ae2bd3f0532f712f743ca510..6492df2a0b5c0167fbf3229c41c16e9e02bdebb3 100644 (file)
@@ -6,7 +6,7 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do
   use Pleroma.Web.ConnCase
   import Pleroma.Factory
   alias Pleroma.Web.Websub.WebsubClientSubscription
-  alias Pleroma.{Repo, Activity, Instances}
+  alias Pleroma.{Repo, Activity}
   alias Pleroma.Web.Websub
 
   test "websub subscription request", %{conn: conn} do
@@ -82,25 +82,5 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do
 
       assert length(Repo.all(Activity)) == 0
     end
-
-    test "it clears `unreachable` federation status of the sender", %{conn: conn} do
-      sender_url = "https://pleroma.soykaf.com"
-      Instances.set_consistently_unreachable(sender_url)
-      refute Instances.reachable?(sender_url)
-
-      websub = insert(:websub_client_subscription)
-      doc = "some stuff"
-      signature = Websub.sign(websub.secret, doc)
-
-      conn =
-        conn
-        |> put_req_header("x-hub-signature", "sha1=" <> signature)
-        |> put_req_header("content-type", "application/atom+xml")
-        |> put_req_header("referer", sender_url)
-        |> post("/push/subscriptions/#{websub.id}", doc)
-
-      assert response(conn, 200) == "OK"
-      assert Instances.reachable?(sender_url)
-    end
   end
 end