[#161] Refactoring, documentation.
authorIvan Tashkinov <ivant.business@gmail.com>
Sun, 30 Jun 2019 12:58:50 +0000 (15:58 +0300)
committerIvan Tashkinov <ivant.business@gmail.com>
Sun, 30 Jun 2019 12:58:50 +0000 (15:58 +0300)
CHANGELOG.md
config/config.exs
docs/config.md
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/federator/federator.ex
lib/pleroma/web/ostatus/handlers/note_handler.ex
test/web/activity_pub/transmogrifier_test.exs
test/web/ostatus/ostatus_test.exs

index a6ec8674d669e9fd31da9a2508980fb562910db0..85d077e3f7bca075de6f3a5788604285aecf725f 100644 (file)
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 ## [Unreleased]
 ### Added
 - MRF: Support for priming the mediaproxy cache (`Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy`)
+- Federation: Support for restricting max. reply-to depth on fetching 
 
 ## [1.0.0] - 2019-06-29
 ### Security
index e337f00aaa02a65d46d73b21741db7bfb6f3bfdd..65f239e318e4033afaf0673869eef3f6084da6c8 100644 (file)
@@ -218,6 +218,7 @@ config :pleroma, :instance,
   },
   registrations_open: true,
   federating: true,
+  federation_incoming_replies_max_depth: 100,
   federation_reachability_timeout_days: 7,
   federation_publisher_modules: [
     Pleroma.Web.ActivityPub.Publisher,
index feef43ba9c75564988dccc88930c41a73f695cba..b401474817aabcaf6dabc6689b5ed528278809f3 100644 (file)
@@ -87,6 +87,7 @@ config :pleroma, Pleroma.Emails.Mailer,
 * `invites_enabled`: Enable user invitations for admins (depends on `registrations_open: false`).
 * `account_activation_required`: Require users to confirm their emails before signing in.
 * `federating`: Enable federation with other instances
+* `federation_incoming_replies_max_depth`: Max. depth of reply-to activities fetching on incoming federation (to prevent memory leakage on extremely nested incoming threads). If set to `nil`, threads of any depth will be fetched.
 * `federation_reachability_timeout_days`: Timeout (in days) of each external federation target being unreachable prior to pausing federating to it.
 * `allow_relay`: Enable Pleroma’s Relay, which makes it possible to follow a whole instance
 * `rewrite_policy`: Message Rewrite Policy, either one or a list. Here are the ones available by default:
index d5ced2d1dc81e64dd82d70151f249a1e873cda4e..543d4bb7d1dd89c7e03ec0df027bb4a78ba769fd 100644 (file)
@@ -187,7 +187,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
 
     object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
 
-    if (options[:depth] || 1) <= Federator.max_replies_depth() do
+    if Federator.allowed_incoming_reply_depth?(options[:depth]) do
       case get_obj_helper(in_reply_to_id, options) do
         {:ok, replied_object} ->
           with %Activity{} = _activity <-
@@ -349,10 +349,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
 
   def fix_type(%{"inReplyTo" => reply_id} = object, options) when is_binary(reply_id) do
     reply =
-      if (options[:depth] || 1) <= Federator.max_replies_depth() do
+      if Federator.allowed_incoming_reply_depth?(options[:depth]) do
         Object.normalize(reply_id, true)
-      else
-        nil
       end
 
     if reply && (reply.data["type"] == "Question" and object["name"]) do
index 7c13ff32314b259c81e80121e64955cb09bbdd4b..f4f9e83e06ab55f71b715301631865bc46b11c00 100644 (file)
@@ -22,11 +22,17 @@ defmodule Pleroma.Web.Federator do
     refresh_subscriptions()
   end
 
-  @max_replies_depth 100
-
   @doc "Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161)"
   # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
-  def max_replies_depth, do: @max_replies_depth
+  def allowed_incoming_reply_depth?(depth) do
+    max_replies_depth = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
+
+    if max_replies_depth do
+      (depth || 1) <= max_replies_depth
+    else
+      true
+    end
+  end
 
   # Client API
 
index 6f8f3ddcbba0015e22bcde1b503df713848e79ff..8e0adad91f38b7c87008f37b932e426934eb0dd8 100644 (file)
@@ -94,7 +94,7 @@ defmodule Pleroma.Web.OStatus.NoteHandler do
       activity
     else
       _e ->
-        with true <- (options[:depth] || 1) <= Federator.max_replies_depth(),
+        with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
              in_reply_to_href when not is_nil(in_reply_to_href) <-
                XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
              {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(in_reply_to_href, options) do
index fc8703ca9c6000430951d98259404ce3f9b0e858..a914d3c4c77065462384820c36d7bf01003cb2c4 100644 (file)
@@ -72,7 +72,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       data = Map.put(data, "object", object)
 
       with_mock Pleroma.Web.Federator,
-        max_replies_depth: fn -> 0 end do
+        allowed_incoming_reply_depth?: fn _ -> false end do
         {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
 
         returned_object = Object.normalize(returned_activity.data["object"], false)
index 12627356c92b4e131ae554a8eb36571941750a22..acce330080dc394b05f81fe5785d49105ebaf672 100644 (file)
@@ -298,7 +298,7 @@ defmodule Pleroma.Web.OStatusTest do
     incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
 
     with_mock Pleroma.Web.Federator,
-      max_replies_depth: fn -> 0 end do
+      allowed_incoming_reply_depth?: fn _ -> false end do
       {:ok, [activity]} = OStatus.handle_incoming(incoming)
       object = Object.normalize(activity.data["object"], false)