diff --git a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex index 161177727..22cb242fc 100644 --- a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_accept(%{host: actor_host} = _actor_info, object) do accepts = - Config.get([:mrf_simple, :accept]) + instance_list(:accept) |> MRF.subdomains_regex() cond do @@ -28,7 +28,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_reject(%{host: actor_host} = _actor_info, object) do rejects = - Config.get([:mrf_simple, :reject]) + instance_list(:reject) |> MRF.subdomains_regex() if MRF.subdomain_match?(rejects, actor_host) do @@ -44,7 +44,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do ) when length(child_attachment) > 0 do media_removal = - Config.get([:mrf_simple, :media_removal]) + instance_list(:media_removal) |> MRF.subdomains_regex() object = @@ -69,7 +69,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do ) when is_map(child_object) do media_nsfw = - Config.get([:mrf_simple, :media_nsfw]) + instance_list(:media_nsfw) |> MRF.subdomains_regex() object = @@ -89,7 +89,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do timeline_removal = - Config.get([:mrf_simple, :federated_timeline_removal]) + instance_list(:federated_timeline_removal) |> MRF.subdomains_regex() object = @@ -116,7 +116,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_followers_only(%{host: actor_host} = _actor_info, object) do followers_only = - Config.get([:mrf_simple, :followers_only]) + instance_list(:followers_only) |> MRF.subdomains_regex() object = @@ -141,7 +141,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do report_removal = - Config.get([:mrf_simple, :report_removal]) + instance_list(:report_removal) |> MRF.subdomains_regex() if MRF.subdomain_match?(report_removal, actor_host) do @@ -155,7 +155,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do avatar_removal = - Config.get([:mrf_simple, :avatar_removal]) + instance_list(:avatar_removal) |> MRF.subdomains_regex() if MRF.subdomain_match?(avatar_removal, actor_host) do @@ -169,7 +169,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do banner_removal = - Config.get([:mrf_simple, :banner_removal]) + instance_list(:banner_removal) |> MRF.subdomains_regex() if MRF.subdomain_match?(banner_removal, actor_host) do @@ -181,12 +181,19 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do defp check_banner_removal(_actor_info, object), do: {:ok, object} + defp instance_list(config_key) do + Config.get([:mrf_simple, config_key]) + |> Enum.map(fn + {instance, _} -> instance + end) + end + @impl true def filter(%{"type" => "Delete", "actor" => actor} = object) do %{host: actor_host} = URI.parse(actor) reject_deletes = - Config.get([:mrf_simple, :reject_deletes]) + instance_list(:reject_deletes) |> MRF.subdomains_regex() if MRF.subdomain_match?(reject_deletes, actor_host) do @@ -239,7 +246,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do mrf_simple = Config.get(:mrf_simple) - |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn v -> v in exclusions end)} end) + |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn {v, _} -> v in exclusions end)} end) |> Enum.into(%{}) {:ok, %{mrf_simple: mrf_simple}} diff --git a/test/user_test.exs b/test/user_test.exs index d506f7047..0bfa87972 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -462,7 +462,7 @@ defmodule Pleroma.UserTest do ) test "it sends a welcome chat message when Simple policy applied to local instance" do - Pleroma.Config.put([:mrf_simple, :media_nsfw], ["localhost"]) + Pleroma.Config.put([:mrf_simple, :media_nsfw], [{"localhost", ""}]) welcome_user = insert(:user) Pleroma.Config.put([:welcome, :chat_message, :enabled], true) diff --git a/test/web/activity_pub/mrf/simple_policy_test.exs b/test/web/activity_pub/mrf/simple_policy_test.exs index d7dde62c4..3e9da13b5 100644 --- a/test/web/activity_pub/mrf/simple_policy_test.exs +++ b/test/web/activity_pub/mrf/simple_policy_test.exs @@ -34,7 +34,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "has a matching host" do - Config.put([:mrf_simple, :media_removal], ["remote.instance"]) + Config.put([:mrf_simple, :media_removal], [{"remote.instance", "Some reason"}]) media_message = build_media_message() local_message = build_local_message() @@ -47,7 +47,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "match with wildcard domain" do - Config.put([:mrf_simple, :media_removal], ["*.remote.instance"]) + Config.put([:mrf_simple, :media_removal], [{"*.remote.instance", "Whatever reason"}]) media_message = build_media_message() local_message = build_local_message() @@ -71,7 +71,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "has a matching host" do - Config.put([:mrf_simple, :media_nsfw], ["remote.instance"]) + Config.put([:mrf_simple, :media_nsfw], [{"remote.instance", "Whetever"}]) media_message = build_media_message() local_message = build_local_message() @@ -85,7 +85,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "match with wildcard domain" do - Config.put([:mrf_simple, :media_nsfw], ["*.remote.instance"]) + Config.put([:mrf_simple, :media_nsfw], [{"*.remote.instance", "yeah yeah"}]) media_message = build_media_message() local_message = build_local_message() @@ -122,7 +122,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "has a matching host" do - Config.put([:mrf_simple, :report_removal], ["remote.instance"]) + Config.put([:mrf_simple, :report_removal], [{"remote.instance", "muh"}]) report_message = build_report_message() local_message = build_local_message() @@ -131,7 +131,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "match with wildcard domain" do - Config.put([:mrf_simple, :report_removal], ["*.remote.instance"]) + Config.put([:mrf_simple, :report_removal], [{"*.remote.instance", "suya"}]) report_message = build_report_message() local_message = build_local_message() @@ -166,7 +166,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do |> URI.parse() |> Map.fetch!(:host) - Config.put([:mrf_simple, :federated_timeline_removal], [ftl_message_actor_host]) + Config.put([:mrf_simple, :federated_timeline_removal], [{ftl_message_actor_host, "uwu"}]) local_message = build_local_message() assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message) @@ -187,7 +187,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do |> URI.parse() |> Map.fetch!(:host) - Config.put([:mrf_simple, :federated_timeline_removal], ["*." <> ftl_message_actor_host]) + Config.put([:mrf_simple, :federated_timeline_removal], [ + {"*." <> ftl_message_actor_host, "owo"} + ]) + local_message = build_local_message() assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message) @@ -210,7 +213,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do ftl_message = Map.put(ftl_message, "cc", []) - Config.put([:mrf_simple, :federated_timeline_removal], [ftl_message_actor_host]) + Config.put([:mrf_simple, :federated_timeline_removal], [ + {ftl_message_actor_host, "spiderwaifu goes 88w88"} + ]) assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message) refute "https://www.w3.org/ns/activitystreams#Public" in ftl_message["to"] @@ -239,7 +244,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "activity has a matching host" do - Config.put([:mrf_simple, :reject], ["remote.instance"]) + Config.put([:mrf_simple, :reject], [{"remote.instance", ""}]) remote_message = build_remote_message() @@ -247,7 +252,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "activity matches with wildcard domain" do - Config.put([:mrf_simple, :reject], ["*.remote.instance"]) + Config.put([:mrf_simple, :reject], [{"*.remote.instance", ""}]) remote_message = build_remote_message() @@ -255,7 +260,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "actor has a matching host" do - Config.put([:mrf_simple, :reject], ["remote.instance"]) + Config.put([:mrf_simple, :reject], [{"remote.instance", ""}]) remote_user = build_remote_user() @@ -305,7 +310,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do |> URI.parse() |> Map.fetch!(:host) - Config.put([:mrf_simple, :followers_only], [actor_domain]) + Config.put([:mrf_simple, :followers_only], [{actor_domain, ""}]) assert {:ok, new_activity} = SimplePolicy.filter(activity) assert actor.follower_address in new_activity["cc"] @@ -333,7 +338,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "is not empty but activity doesn't have a matching host" do - Config.put([:mrf_simple, :accept], ["non.matching.remote"]) + Config.put([:mrf_simple, :accept], [{"non.matching.remote", ""}]) local_message = build_local_message() remote_message = build_remote_message() @@ -343,7 +348,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "activity has a matching host" do - Config.put([:mrf_simple, :accept], ["remote.instance"]) + Config.put([:mrf_simple, :accept], [{"remote.instance", ""}]) local_message = build_local_message() remote_message = build_remote_message() @@ -353,7 +358,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "activity matches with wildcard domain" do - Config.put([:mrf_simple, :accept], ["*.remote.instance"]) + Config.put([:mrf_simple, :accept], [{"*.remote.instance", ""}]) local_message = build_local_message() remote_message = build_remote_message() @@ -363,7 +368,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "actor has a matching host" do - Config.put([:mrf_simple, :accept], ["remote.instance"]) + Config.put([:mrf_simple, :accept], [{"remote.instance", ""}]) remote_user = build_remote_user() @@ -381,7 +386,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "is not empty but it doesn't have a matching host" do - Config.put([:mrf_simple, :avatar_removal], ["non.matching.remote"]) + Config.put([:mrf_simple, :avatar_removal], [{"non.matching.remote", ""}]) remote_user = build_remote_user() @@ -389,7 +394,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "has a matching host" do - Config.put([:mrf_simple, :avatar_removal], ["remote.instance"]) + Config.put([:mrf_simple, :avatar_removal], [{"remote.instance", ""}]) remote_user = build_remote_user() {:ok, filtered} = SimplePolicy.filter(remote_user) @@ -398,7 +403,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "match with wildcard domain" do - Config.put([:mrf_simple, :avatar_removal], ["*.remote.instance"]) + Config.put([:mrf_simple, :avatar_removal], [{"*.remote.instance", ""}]) remote_user = build_remote_user() {:ok, filtered} = SimplePolicy.filter(remote_user) @@ -417,7 +422,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "is not empty but it doesn't have a matching host" do - Config.put([:mrf_simple, :banner_removal], ["non.matching.remote"]) + Config.put([:mrf_simple, :banner_removal], [{"non.matching.remote", ""}]) remote_user = build_remote_user() @@ -425,7 +430,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "has a matching host" do - Config.put([:mrf_simple, :banner_removal], ["remote.instance"]) + Config.put([:mrf_simple, :banner_removal], [{"remote.instance", ""}]) remote_user = build_remote_user() {:ok, filtered} = SimplePolicy.filter(remote_user) @@ -434,7 +439,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "match with wildcard domain" do - Config.put([:mrf_simple, :banner_removal], ["*.remote.instance"]) + Config.put([:mrf_simple, :banner_removal], [{"*.remote.instance", ""}]) remote_user = build_remote_user() {:ok, filtered} = SimplePolicy.filter(remote_user) @@ -447,7 +452,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do setup do: Config.put([:mrf_simple, :reject_deletes], []) test "it accepts deletions even from rejected servers" do - Config.put([:mrf_simple, :reject], ["remote.instance"]) + Config.put([:mrf_simple, :reject], [{"remote.instance", ""}]) deletion_message = build_remote_deletion_message() @@ -455,7 +460,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "it accepts deletions even from non-whitelisted servers" do - Config.put([:mrf_simple, :accept], ["non.matching.remote"]) + Config.put([:mrf_simple, :accept], [{"non.matching.remote", ""}]) deletion_message = build_remote_deletion_message() @@ -464,10 +469,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end describe "when :reject_deletes is not empty but it doesn't have a matching host" do - setup do: Config.put([:mrf_simple, :reject_deletes], ["non.matching.remote"]) + setup do: Config.put([:mrf_simple, :reject_deletes], [{"non.matching.remote", ""}]) test "it accepts deletions even from rejected servers" do - Config.put([:mrf_simple, :reject], ["remote.instance"]) + Config.put([:mrf_simple, :reject], [{"remote.instance", ""}]) deletion_message = build_remote_deletion_message() @@ -475,7 +480,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end test "it accepts deletions even from non-whitelisted servers" do - Config.put([:mrf_simple, :accept], ["non.matching.remote"]) + Config.put([:mrf_simple, :accept], [{"non.matching.remote", ""}]) deletion_message = build_remote_deletion_message() @@ -484,7 +489,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end describe "when :reject_deletes has a matching host" do - setup do: Config.put([:mrf_simple, :reject_deletes], ["remote.instance"]) + setup do: Config.put([:mrf_simple, :reject_deletes], [{"remote.instance", ""}]) test "it rejects the deletion" do deletion_message = build_remote_deletion_message() @@ -494,7 +499,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do end describe "when :reject_deletes match with wildcard domain" do - setup do: Config.put([:mrf_simple, :reject_deletes], ["*.remote.instance"]) + setup do: Config.put([:mrf_simple, :reject_deletes], [{"*.remote.instance", ""}]) test "it rejects the deletion" do deletion_message = build_remote_deletion_message() diff --git a/test/web/node_info_test.exs b/test/web/node_info_test.exs index 06b33607f..077852445 100644 --- a/test/web/node_info_test.exs +++ b/test/web/node_info_test.exs @@ -156,15 +156,17 @@ defmodule Pleroma.Web.NodeInfoTest do clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.SimplePolicy]) clear_config([:mrf, :transparency], true) - simple_config = %{"reject" => ["example.com"]} + simple_config = %{"reject" => [{"example.com", ""}]} clear_config(:mrf_simple, simple_config) + expected_config = %{"reject" => [["example.com", ""]]} + response = conn |> get("/nodeinfo/2.1.json") |> json_response(:ok) - assert response["metadata"]["federation"]["mrf_simple"] == simple_config + assert response["metadata"]["federation"]["mrf_simple"] == expected_config end test "it performs exclusions from MRF transparency data if configured", %{conn: conn} do @@ -172,10 +174,10 @@ defmodule Pleroma.Web.NodeInfoTest do clear_config([:mrf, :transparency], true) clear_config([:mrf, :transparency_exclusions], ["other.site"]) - simple_config = %{"reject" => ["example.com", "other.site"]} + simple_config = %{"reject" => [{"example.com", ""}, {"other.site", ""}]} clear_config(:mrf_simple, simple_config) - expected_config = %{"reject" => ["example.com"]} + expected_config = %{"reject" => [["example.com", ""]]} response = conn