commit: 58ec4fd1eec3d7ac59025ab7dfca2ae9475d1b10
parent 27d271b4efd452db791613b426c88f1fecee1b83
Author: tusooa <tusooa@kazv.moe>
Date: Tue, 13 Dec 2022 22:41:19 -0500
Anonymize reporter before federating
Diffstat:
3 files changed, 52 insertions(+), 2 deletions(-)
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -415,6 +415,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
with flag_data <- make_flag_data(params, additional),
{:ok, activity} <- insert(flag_data, local),
{:ok, stripped_activity} <- strip_report_status_data(activity),
+ stripped_activity <- maybe_anonymize_reporter(stripped_activity),
_ <- notify_and_stream(activity),
:ok <-
maybe_federate(stripped_activity) do
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
@@ -875,15 +875,29 @@ defmodule Pleroma.Web.ActivityPub.Utils do
{:ok, %{activity | data: new_data}}
end
- def maybe_anonymize_reporter(activity) do
+ def get_anonymized_reporter do
with true <- Pleroma.Config.get([:activitypub, :anonymize_reporter]),
nickname when is_binary(nickname) <-
Pleroma.Config.get([:activitypub, :anonymize_reporter_local_nickname]),
%User{ap_id: ap_id, local: true} <- User.get_cached_by_nickname(nickname) do
+ ap_id
+ else
+ _ -> nil
+ end
+ end
+
+ def maybe_anonymize_reporter(%Activity{data: data} = activity) do
+ %Activity{activity | data: maybe_anonymize_reporter(data)}
+ end
+
+ def maybe_anonymize_reporter(activity) do
+ ap_id = get_anonymized_reporter()
+
+ if is_binary(ap_id) do
activity
|> Map.put("actor", ap_id)
else
- _ -> activity
+ activity
end
end
diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs
@@ -1707,6 +1707,41 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
assert_called(Utils.maybe_federate(%{activity | data: new_data}))
end
+ test "anonymize reporter from Flag, before federating it, if configured so",
+ %{
+ reporter: reporter,
+ context: context,
+ target_account: target_account,
+ reported_activity: reported_activity,
+ object_ap_id: object_ap_id,
+ content: content
+ } do
+ placeholder = insert(:user)
+ clear_config([:activitypub, :anonymize_reporter], true)
+ clear_config([:activitypub, :anonymize_reporter_local_nickname], placeholder.nickname)
+ # Workaround "could not checkout connection" problem
+ # https://elixirforum.com/t/ecto-timeout-errors-when-wrapping-into-cachex-calls-in-tests/19078/11
+ %User{} = User.get_cached_by_nickname(placeholder.nickname)
+
+ with_mock Utils, [:passthrough], [] do
+ {:ok, activity} =
+ ActivityPub.flag(%{
+ actor: reporter,
+ context: context,
+ account: target_account,
+ statuses: [reported_activity],
+ content: content
+ })
+
+ new_data =
+ activity.data
+ |> put_in(["object"], [target_account.ap_id, object_ap_id])
+ |> Map.put("actor", placeholder.ap_id)
+
+ assert_called(Utils.maybe_federate(%{activity | data: new_data}))
+ end
+ end
+
test_with_mock "reverts on error",
%{
reporter: reporter,