From 5a3b81d92ef532769a11d4374424665f6d97a08f Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Mon, 27 Nov 2023 17:55:16 +0400 Subject: [PATCH 1/3] ActivityPub.UtilsTest: Add failing test for strip_report_status_data --- test/pleroma/web/activity_pub/utils_test.exs | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/pleroma/web/activity_pub/utils_test.exs b/test/pleroma/web/activity_pub/utils_test.exs index 3f93c872b..05bff1e1c 100644 --- a/test/pleroma/web/activity_pub/utils_test.exs +++ b/test/pleroma/web/activity_pub/utils_test.exs @@ -16,6 +16,37 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do require Pleroma.Constants + describe "strip_report_status_data/1" do + test "does not break on issues with the reported activites" do + reporter = insert(:user) + target_account = insert(:user) + {:ok, activity} = CommonAPI.post(target_account, %{status: "foobar"}) + context = Utils.generate_context_id() + content = "foobar" + + res = + Utils.make_flag_data( + %{ + actor: reporter, + context: context, + account: target_account, + statuses: [%{"id" => activity.data["id"]}], + content: content + }, + %{} + ) + + res = + res + |> Map.put("object", res["object"] ++ [nil, 1, 5, "123"]) + + {:ok, activity} = Pleroma.Web.ActivityPub.ActivityPub.insert(res) + + Utils.strip_report_status_data(activity) + |> IO.inspect() + end + end + describe "fetch the latest Follow" do test "fetches the latest Follow activity" do %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity) From 27df2c0ce6c214f36db742af702fc239f80764a7 Mon Sep 17 00:00:00 2001 From: NEETzsche Date: Mon, 27 Nov 2023 03:34:31 -0700 Subject: [PATCH 2/3] Fix #strip_report_status_data --- changelog.d/anonymous-exception-else.fix | 1 + lib/pleroma/web/activity_pub/utils.ex | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog.d/anonymous-exception-else.fix diff --git a/changelog.d/anonymous-exception-else.fix b/changelog.d/anonymous-exception-else.fix new file mode 100644 index 000000000..38d5d1be5 --- /dev/null +++ b/changelog.d/anonymous-exception-else.fix @@ -0,0 +1 @@ +Fix #strip_report_status_data diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 437220077..073ccd615 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -855,6 +855,7 @@ def strip_report_status_data(activity) do Enum.map(reported_activities, fn act when is_map(act) -> act["id"] act when is_binary(act) -> act + _other -> nil end) new_data = put_in(activity.data, ["object"], [actor | stripped_activities]) From 4ef56c5b65a4d1e7e90a87f1a1a507df523a4b4b Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Mon, 27 Nov 2023 18:44:11 +0400 Subject: [PATCH 3/3] ActivityPub.Utils: Only treat object ids as valid while stripping --- lib/pleroma/web/activity_pub/utils.ex | 10 ++++++---- test/pleroma/web/activity_pub/utils_test.exs | 10 +++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 073ccd615..b32f19740 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do alias Ecto.UUID alias Pleroma.Activity alias Pleroma.Config + alias Pleroma.EctoType.ActivityPub.ObjectValidators.ObjectID alias Pleroma.Maps alias Pleroma.Notification alias Pleroma.Object @@ -852,10 +853,11 @@ def strip_report_status_data(activity) do [actor | reported_activities] = activity.data["object"] stripped_activities = - Enum.map(reported_activities, fn - act when is_map(act) -> act["id"] - act when is_binary(act) -> act - _other -> nil + Enum.reduce(reported_activities, [], fn act, acc -> + case ObjectID.cast(act) do + {:ok, act} -> [act | acc] + _ -> acc + end end) new_data = put_in(activity.data, ["object"], [actor | stripped_activities]) diff --git a/test/pleroma/web/activity_pub/utils_test.exs b/test/pleroma/web/activity_pub/utils_test.exs index 05bff1e1c..9ca21f5d9 100644 --- a/test/pleroma/web/activity_pub/utils_test.exs +++ b/test/pleroma/web/activity_pub/utils_test.exs @@ -23,6 +23,7 @@ test "does not break on issues with the reported activites" do {:ok, activity} = CommonAPI.post(target_account, %{status: "foobar"}) context = Utils.generate_context_id() content = "foobar" + post_id = activity.data["id"] res = Utils.make_flag_data( @@ -30,7 +31,7 @@ test "does not break on issues with the reported activites" do actor: reporter, context: context, account: target_account, - statuses: [%{"id" => activity.data["id"]}], + statuses: [%{"id" => post_id}], content: content }, %{} @@ -42,8 +43,11 @@ test "does not break on issues with the reported activites" do {:ok, activity} = Pleroma.Web.ActivityPub.ActivityPub.insert(res) - Utils.strip_report_status_data(activity) - |> IO.inspect() + [user_id, object | _] = activity.data["object"] + + {:ok, stripped} = Utils.strip_report_status_data(activity) + + assert stripped.data["object"] == [user_id, object["id"]] end end