From fbc374083f8661fce8c224a69df3d780679eb15f Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 28 Feb 2023 10:40:24 -0500 Subject: [PATCH 01/11] Add emoji policy to remove emojis matching certain urls https://git.pleroma.social/pleroma/pleroma/-/issues/2775 --- config/config.exs | 6 + lib/pleroma/web/activity_pub/emoji_policy.ex | 158 ++++++++++++++++++ .../activity_pub/mrf/emoji_policy_test.exs | 123 ++++++++++++++ 3 files changed, 287 insertions(+) create mode 100644 lib/pleroma/web/activity_pub/emoji_policy.ex create mode 100644 test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs diff --git a/config/config.exs b/config/config.exs index 3430ee4d7..502b489bd 100644 --- a/config/config.exs +++ b/config/config.exs @@ -396,6 +396,12 @@ config :pleroma, :mrf_keyword, federated_timeline_removal: [], replace: [] +config :pleroma, :mrf_emoji, + remove_url: [], + remove_shortcode: [], + federated_timeline_removal_url: [], + federated_timeline_removal_shortcode: [] + config :pleroma, :mrf_hashtag, sensitive: ["nsfw"], reject: [], diff --git a/lib/pleroma/web/activity_pub/emoji_policy.ex b/lib/pleroma/web/activity_pub/emoji_policy.ex new file mode 100644 index 000000000..747e720b6 --- /dev/null +++ b/lib/pleroma/web/activity_pub/emoji_policy.ex @@ -0,0 +1,158 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do + require Pleroma.Constants + + @moduledoc "Reject or force-unlisted emojis with certain URLs or names" + + @behaviour Pleroma.Web.ActivityPub.MRF.Policy + + defp config_remove_url do + Pleroma.Config.get([:mrf_emoji, :remove_url], []) + end + + @impl Pleroma.Web.ActivityPub.MRF.Policy + def filter(%{"type" => type, "object" => %{} = object} = message) + when type in ["Create", "Update"] do + with object <- process_remove(object, :url, config_remove_url()) do + {:ok, Map.put(message, "object", object)} + end + end + + @impl Pleroma.Web.ActivityPub.MRF.Policy + def filter(%{"type" => type} = object) when type in Pleroma.Constants.actor_types() do + with object <- process_remove(object, :url, config_remove_url()) do + {:ok, object} + end + end + + @impl Pleroma.Web.ActivityPub.MRF.Policy + def filter(message) do + {:ok, message} + end + + defp match_string?(string, pattern) when is_binary(pattern) do + string == pattern + end + + defp match_string?(string, %Regex{} = pattern) do + String.match?(string, pattern) + end + + defp match_any?(string, patterns) do + Enum.any?(patterns, &match_string?(string, &1)) + end + + defp process_remove(object, :url, patterns) do + processed_tag = + Enum.filter( + object["tag"], + fn + %{"type" => "Emoji", "icon" => %{"url" => url}} when is_binary(url) -> + not match_any?(url, patterns) + + _ -> + true + end + ) + + processed_emoji = + if object["emoji"] do + object["emoji"] + |> Enum.reduce(%{}, fn {name, url}, acc -> + if not match_any?(url, patterns) do + Map.put(acc, name, url) + else + acc + end + end) + else + nil + end + + if processed_emoji do + object + |> Map.put("tag", processed_tag) + |> Map.put("emoji", processed_emoji) + else + object + |> Map.put("tag", processed_tag) + end + end + + @impl Pleroma.Web.ActivityPub.MRF.Policy + def describe do + # This horror is needed to convert regex sigils to strings + mrf_emoji = + Pleroma.Config.get(:mrf_emoji, []) + |> Enum.map(fn {key, value} -> + {key, + Enum.map(value, fn + pattern -> + if not is_binary(pattern) do + inspect(pattern) + else + pattern + end + end)} + end) + |> Enum.into(%{}) + + {:ok, %{mrf_emoji: mrf_emoji}} + end + + @impl Pleroma.Web.ActivityPub.MRF.Policy + def config_description do + %{ + key: :mrf_emoji, + related_policy: "Pleroma.Web.ActivityPub.MRF.EmojiPolicy", + label: "MRF Emoji", + description: + "Reject or force-unlisted emojis whose URLs or names match a keyword or [Regex](https://hexdocs.pm/elixir/Regex.html).", + children: [ + %{ + key: :remove_url, + type: {:list, :string}, + description: """ + A list of patterns which result in emoji whose URL matches being removed from the message. This will apply to both statuses and user profiles. + + Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. + """, + suggestions: ["foo", ~r/foo/iu] + }, + %{ + key: :remove_shortcode, + type: {:list, :string}, + description: """ + A list of patterns which result in emoji whose shortcode matches being removed from the message. This will apply to both statuses and user profiles. + + Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. + """, + suggestions: ["foo", ~r/foo/iu] + }, + %{ + key: :federated_timeline_removal_url, + type: {:list, :string}, + description: """ + A list of patterns which result in message with emojis whose URLs match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses. + + Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. + """, + suggestions: ["foo", ~r/foo/iu] + }, + %{ + key: :federated_timeline_removal_shortcode, + type: {:list, :string}, + description: """ + A list of patterns which result in message with emojis whose shortcodes match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses. + + Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. + """, + suggestions: ["foo", ~r/foo/iu] + } + ] + } + end +end diff --git a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs new file mode 100644 index 000000000..fe1fb338e --- /dev/null +++ b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs @@ -0,0 +1,123 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do + use Pleroma.DataCase + + alias Pleroma.Web.ActivityPub.MRF + alias Pleroma.Web.ActivityPub.MRF.EmojiPolicy + + setup do: clear_config(:mrf_emoji) + + setup do + clear_config([:mrf_emoji], %{ + remove_url: [], + remove_shortcode: [], + federated_timeline_removal_url: [], + federated_timeline_removal_shortcode: [] + }) + end + + @emoji_tags [ + %{ + "icon" => %{ + "type" => "Image", + "url" => "https://example.org/emoji/biribiri/mikoto_smile2.png" + }, + "id" => "https://example.org/emoji/biribiri/mikoto_smile2.png", + "name" => ":mikoto_smile2:", + "type" => "Emoji", + "updated" => "1970-01-01T00:00:00Z" + }, + %{ + "icon" => %{ + "type" => "Image", + "url" => "https://example.org/emoji/biribiri/mikoto_smile3.png" + }, + "id" => "https://example.org/emoji/biribiri/mikoto_smile3.png", + "name" => ":mikoto_smile3:", + "type" => "Emoji", + "updated" => "1970-01-01T00:00:00Z" + }, + %{ + "icon" => %{ + "type" => "Image", + "url" => "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + }, + "id" => "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png", + "name" => ":nekomimi_girl_emoji_007:", + "type" => "Emoji", + "updated" => "1970-01-01T00:00:00Z" + }, + %{ + "icon" => %{ + "type" => "Image", + "url" => "https://example.org/test.png" + }, + "id" => "https://example.org/test.png", + "name" => ":test:", + "type" => "Emoji", + "updated" => "1970-01-01T00:00:00Z" + } + ] + + @misc_tags [%{"type" => "Placeholder"}] + + @user_data %{ + "type" => "Person", + "id" => "https://example.org/placeholder", + "name" => "lol", + "tag" => @emoji_tags ++ @misc_tags + } + + @status_data %{ + "type" => "Create", + "object" => %{ + "type" => "Note", + "id" => "https://example.org/placeholder", + "content" => "lol", + "tag" => @emoji_tags ++ @misc_tags, + "emoji" => %{ + "mikoto_smile2" => "https://example.org/emoji/biribiri/mikoto_smile2.png", + "mikoto_smile3" => "https://example.org/emoji/biribiri/mikoto_smile3.png", + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png", + "test" => "https://example.org/test.png" + } + } + } + + describe "remove_url" do + setup do + clear_config([:mrf_emoji, :remove_url], [ + "https://example.org/test.png", + ~r{/biribiri/mikoto_smile[23]\.png}, + "nekomimi_girl_emoji" + ]) + + :ok + end + + test "processes user" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @user_data) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + assert %{"tag" => ^expected_tags} = filtered + end + + test "processes status" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @status_data) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + expected_emoji = %{ + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + } + + assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered + end + end +end -- 2.34.1 From 10d64955105cc3cd3f076fd743e88bc76cabfad5 Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 28 Feb 2023 10:51:56 -0500 Subject: [PATCH 02/11] EmojiPolicy: implement remove by shortcode --- lib/pleroma/web/activity_pub/emoji_policy.ex | 48 ++++++++++++++++--- .../activity_pub/mrf/emoji_policy_test.exs | 33 +++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/activity_pub/emoji_policy.ex b/lib/pleroma/web/activity_pub/emoji_policy.ex index 747e720b6..346b64699 100644 --- a/lib/pleroma/web/activity_pub/emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/emoji_policy.ex @@ -13,17 +13,23 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do Pleroma.Config.get([:mrf_emoji, :remove_url], []) end + defp config_remove_shortcode do + Pleroma.Config.get([:mrf_emoji, :remove_shortcode], []) + end + @impl Pleroma.Web.ActivityPub.MRF.Policy def filter(%{"type" => type, "object" => %{} = object} = message) when type in ["Create", "Update"] do - with object <- process_remove(object, :url, config_remove_url()) do + with object <- process_remove(object, :url, config_remove_url()), + object <- process_remove(object, :shortcode, config_remove_shortcode()) do {:ok, Map.put(message, "object", object)} end end @impl Pleroma.Web.ActivityPub.MRF.Policy def filter(%{"type" => type} = object) when type in Pleroma.Constants.actor_types() do - with object <- process_remove(object, :url, config_remove_url()) do + with object <- process_remove(object, :url, config_remove_url()), + object <- process_remove(object, :shortcode, config_remove_shortcode()) do {:ok, object} end end @@ -46,12 +52,42 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do end defp process_remove(object, :url, patterns) do + process_remove_impl( + object, + fn + %{"icon" => %{"url" => url}} -> url + _ -> nil + end, + fn {_name, url} -> url end, + patterns + ) + end + + defp process_remove(object, :shortcode, patterns) do + process_remove_impl( + object, + fn + %{"name" => name} when is_binary(name) -> String.trim(name, ":") + _ -> nil + end, + fn {name, _url} -> name end, + patterns + ) + end + + defp process_remove_impl(object, extract_from_tag, extract_from_emoji, patterns) do processed_tag = Enum.filter( object["tag"], fn - %{"type" => "Emoji", "icon" => %{"url" => url}} when is_binary(url) -> - not match_any?(url, patterns) + %{"type" => "Emoji"} = tag -> + str = extract_from_tag.(tag) + + if is_binary(str) do + not match_any?(str, patterns) + else + true + end _ -> true @@ -61,8 +97,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do processed_emoji = if object["emoji"] do object["emoji"] - |> Enum.reduce(%{}, fn {name, url}, acc -> - if not match_any?(url, patterns) do + |> Enum.reduce(%{}, fn {name, url} = emoji, acc -> + if not match_any?(extract_from_emoji.(emoji), patterns) do Map.put(acc, name, url) else acc diff --git a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs index fe1fb338e..6511f6532 100644 --- a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs @@ -120,4 +120,37 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered end end + + describe "remove_shortcode" do + setup do + clear_config([:mrf_emoji, :remove_shortcode], [ + "test", + ~r{mikoto_s}, + "nekomimi_girl_emoji" + ]) + + :ok + end + + test "processes user" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @user_data) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + assert %{"tag" => ^expected_tags} = filtered + end + + test "processes status" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @status_data) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + expected_emoji = %{ + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + } + + assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered + end + end end -- 2.34.1 From 485bd7d5a830b30414f874acd63204c1ccff685e Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 28 Feb 2023 11:47:53 -0500 Subject: [PATCH 03/11] EmojiPolicy: Implement delist --- lib/pleroma/web/activity_pub/emoji_policy.ex | 117 ++++++++++-- .../activity_pub/mrf/emoji_policy_test.exs | 180 +++++++++++++++++- 2 files changed, 274 insertions(+), 23 deletions(-) diff --git a/lib/pleroma/web/activity_pub/emoji_policy.ex b/lib/pleroma/web/activity_pub/emoji_policy.ex index 346b64699..df1566ec3 100644 --- a/lib/pleroma/web/activity_pub/emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/emoji_policy.ex @@ -5,6 +5,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do require Pleroma.Constants + alias Pleroma.Object.Updater + @moduledoc "Reject or force-unlisted emojis with certain URLs or names" @behaviour Pleroma.Web.ActivityPub.MRF.Policy @@ -17,12 +19,31 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do Pleroma.Config.get([:mrf_emoji, :remove_shortcode], []) end + defp config_unlist_url do + Pleroma.Config.get([:mrf_emoji, :federated_timeline_removal_url], []) + end + + defp config_unlist_shortcode do + Pleroma.Config.get([:mrf_emoji, :federated_timeline_removal_shortcode], []) + end + + @impl Pleroma.Web.ActivityPub.MRF.Policy + def history_awareness, do: :manual + @impl Pleroma.Web.ActivityPub.MRF.Policy def filter(%{"type" => type, "object" => %{} = object} = message) when type in ["Create", "Update"] do - with object <- process_remove(object, :url, config_remove_url()), - object <- process_remove(object, :shortcode, config_remove_shortcode()) do - {:ok, Map.put(message, "object", object)} + with {:ok, object} <- + Updater.do_with_history(object, fn object -> + {:ok, process_remove(object, :url, config_remove_url())} + end), + {:ok, object} <- + Updater.do_with_history(object, fn object -> + {:ok, process_remove(object, :shortcode, config_remove_shortcode())} + end), + activity <- Map.put(message, "object", object), + activity <- maybe_delist(activity) do + {:ok, activity} end end @@ -51,28 +72,22 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do Enum.any?(patterns, &match_string?(string, &1)) end + defp url_from_tag(%{"icon" => %{"url" => url}}), do: url + defp url_from_tag(_), do: nil + + defp url_from_emoji({_name, url}), do: url + + defp shortcode_from_tag(%{"name" => name}) when is_binary(name), do: String.trim(name, ":") + defp shortcode_from_tag(_), do: nil + + defp shortcode_from_emoji({name, _url}), do: name + defp process_remove(object, :url, patterns) do - process_remove_impl( - object, - fn - %{"icon" => %{"url" => url}} -> url - _ -> nil - end, - fn {_name, url} -> url end, - patterns - ) + process_remove_impl(object, &url_from_tag/1, &url_from_emoji/1, patterns) end defp process_remove(object, :shortcode, patterns) do - process_remove_impl( - object, - fn - %{"name" => name} when is_binary(name) -> String.trim(name, ":") - _ -> nil - end, - fn {name, _url} -> name end, - patterns - ) + process_remove_impl(object, &shortcode_from_tag/1, &shortcode_from_emoji/1, patterns) end defp process_remove_impl(object, extract_from_tag, extract_from_emoji, patterns) do @@ -118,6 +133,66 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do end end + defp maybe_delist(%{"object" => object, "to" => to, "type" => "Create"} = activity) do + check = fn object -> + if any_emoji_match?(object, &url_from_tag/1, &url_from_emoji/1, config_unlist_url()) or + any_emoji_match?( + object, + &shortcode_from_tag/1, + &shortcode_from_emoji/1, + config_unlist_shortcode() + ) do + {:should_delist, nil} + else + {:ok, %{}} + end + end + + should_delist? = fn object -> + with {:ok, _} <- Pleroma.Object.Updater.do_with_history(object, check) do + false + else + _ -> true + end + end + + if Pleroma.Constants.as_public() in to and should_delist?.(object) do + to = List.delete(to, Pleroma.Constants.as_public()) + cc = [Pleroma.Constants.as_public() | activity["cc"] || []] + + activity + |> Map.put("to", to) + |> Map.put("cc", cc) + else + activity + end + end + + defp maybe_delist(activity), do: activity + + defp any_emoji_match?(object, extract_from_tag, extract_from_emoji, patterns) do + Kernel.||( + Enum.any?( + object["tag"], + fn + %{"type" => "Emoji"} = tag -> + str = extract_from_tag.(tag) + + if is_binary(str) do + match_any?(str, patterns) + else + false + end + + _ -> + false + end + ), + object["emoji"] + |> Enum.any?(fn emoji -> match_any?(extract_from_emoji.(emoji), patterns) end) + ) + end + @impl Pleroma.Web.ActivityPub.MRF.Policy def describe do # This horror is needed to convert regex sigils to strings diff --git a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs index 6511f6532..1e1b5c578 100644 --- a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs @@ -5,6 +5,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do use Pleroma.DataCase + require Pleroma.Constants + alias Pleroma.Web.ActivityPub.MRF alias Pleroma.Web.ActivityPub.MRF.EmojiPolicy @@ -84,8 +86,27 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do "nekomimi_girl_emoji_007" => "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png", "test" => "https://example.org/test.png" - } - } + }, + "to" => ["https://example.org/self", Pleroma.Constants.as_public()], + "cc" => ["https://example.org/someone"] + }, + "to" => ["https://example.org/self", Pleroma.Constants.as_public()], + "cc" => ["https://example.org/someone"] + } + + @status_data_with_history %{ + "type" => "Create", + "object" => + @status_data["object"] + |> Map.merge(%{ + "formerRepresentations" => %{ + "type" => "OrderedCollection", + "orderedItems" => [@status_data["object"] |> Map.put("content", "older")], + "totalItems" => 1 + } + }), + "to" => ["https://example.org/self", Pleroma.Constants.as_public()], + "cc" => ["https://example.org/someone"] } describe "remove_url" do @@ -119,6 +140,49 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered end + + test "processes status with history" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @status_data_with_history) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + expected_emoji = %{ + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + } + + assert %{ + "object" => %{ + "tag" => ^expected_tags, + "emoji" => ^expected_emoji, + "formerRepresentations" => %{"orderedItems" => [item]} + } + } = filtered + + assert %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} = item + end + + test "processes updates" do + {:ok, filtered} = + MRF.filter_one(EmojiPolicy, @status_data_with_history |> Map.put("type", "Update")) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + expected_emoji = %{ + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + } + + assert %{ + "object" => %{ + "tag" => ^expected_tags, + "emoji" => ^expected_emoji, + "formerRepresentations" => %{"orderedItems" => [item]} + } + } = filtered + + assert %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} = item + end end describe "remove_shortcode" do @@ -152,5 +216,117 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered end + + test "processes status with history" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @status_data_with_history) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + expected_emoji = %{ + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + } + + assert %{ + "object" => %{ + "tag" => ^expected_tags, + "emoji" => ^expected_emoji, + "formerRepresentations" => %{"orderedItems" => [item]} + } + } = filtered + + assert %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} = item + end + + test "processes updates" do + {:ok, filtered} = + MRF.filter_one(EmojiPolicy, @status_data_with_history |> Map.put("type", "Update")) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + expected_emoji = %{ + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + } + + assert %{ + "object" => %{ + "tag" => ^expected_tags, + "emoji" => ^expected_emoji, + "formerRepresentations" => %{"orderedItems" => [item]} + } + } = filtered + + assert %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} = item + end + end + + describe "federated_timeline_removal_url" do + setup do + clear_config([:mrf_emoji, :federated_timeline_removal_url], [ + "https://example.org/test.png", + ~r{/biribiri/mikoto_smile[23]\.png}, + "nekomimi_girl_emoji" + ]) + + :ok + end + + test "processes status" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @status_data) + + expected_tags = @status_data["object"]["tag"] + expected_emoji = @status_data["object"]["emoji"] + + expected_to = ["https://example.org/self"] + expected_cc = [Pleroma.Constants.as_public(), "https://example.org/someone"] + + assert %{ + "to" => ^expected_to, + "cc" => ^expected_cc, + "object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} + } = filtered + end + + test "ignore updates" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @status_data |> Map.put("type", "Update")) + + expected_tags = @status_data["object"]["tag"] + expected_emoji = @status_data["object"]["emoji"] + + expected_to = ["https://example.org/self", Pleroma.Constants.as_public()] + expected_cc = ["https://example.org/someone"] + + assert %{ + "to" => ^expected_to, + "cc" => ^expected_cc, + "object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} + } = filtered + end + + test "processes status with history" do + status = + @status_data_with_history + |> put_in(["object", "tag"], @misc_tags) + |> put_in(["object", "emoji"], %{}) + + {:ok, filtered} = MRF.filter_one(EmojiPolicy, status) + + expected_tags = @status_data["object"]["tag"] + expected_emoji = @status_data["object"]["emoji"] + + expected_to = ["https://example.org/self"] + expected_cc = [Pleroma.Constants.as_public(), "https://example.org/someone"] + + assert %{ + "to" => ^expected_to, + "cc" => ^expected_cc, + "object" => %{ + "formerRepresentations" => %{ + "orderedItems" => [%{"tag" => ^expected_tags, "emoji" => ^expected_emoji}] + } + } + } = filtered + end end end -- 2.34.1 From b243c20ff48ed789de16d6bb607f967a1b081079 Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 28 Feb 2023 11:48:34 -0500 Subject: [PATCH 04/11] Move emoji_policy.ex to the right place --- lib/pleroma/web/activity_pub/{ => mrf}/emoji_policy.ex | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/pleroma/web/activity_pub/{ => mrf}/emoji_policy.ex (100%) diff --git a/lib/pleroma/web/activity_pub/emoji_policy.ex b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex similarity index 100% rename from lib/pleroma/web/activity_pub/emoji_policy.ex rename to lib/pleroma/web/activity_pub/mrf/emoji_policy.ex -- 2.34.1 From e25d4ad2557b41c8eb2c738c945262370cb7aeb2 Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 28 Feb 2023 12:05:54 -0500 Subject: [PATCH 05/11] Update config cheatsheet --- docs/docs/configuration/cheatsheet.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/docs/configuration/cheatsheet.md b/docs/docs/configuration/cheatsheet.md index ef340ffea..ae7103bea 100644 --- a/docs/docs/configuration/cheatsheet.md +++ b/docs/docs/configuration/cheatsheet.md @@ -222,6 +222,12 @@ Notes: - The hashtags in the configuration do not have a leading `#`. - This MRF Policy is always enabled, if you want to disable it you have to set empty lists +#### :mrf_emoji +* `remove_url`: A list of patterns which result in emoji whose URL matches being removed from the message. This will apply to both statuses and user profiles. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). +* `remove_shortcode`: A list of patterns which result in emoji whose shortcode matches being removed from the message. This will apply to both statuses and user profiles. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). +* `federated_timeline_removal_url`: A list of patterns which result in message with emojis whose URLs match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). +* `federated_timeline_removal_shortcode`: A list of patterns which result in message with emojis whose shortcodes match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). + ### :activitypub * `unfollow_blocked`: Whether blocks result in people getting unfollowed * `outgoing_blocks`: Whether to federate blocks to other instances -- 2.34.1 From 1d6aea114cbbe7aeae561599870ae2f618723147 Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 28 Feb 2023 12:14:48 -0500 Subject: [PATCH 06/11] Improve config examples for EmojiPolicy --- lib/pleroma/web/activity_pub/mrf/emoji_policy.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex index df1566ec3..9a2e8eade 100644 --- a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex @@ -231,7 +231,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. """, - suggestions: ["foo", ~r/foo/iu] + suggestions: ["https://example.org/foo.png", ~r/example.org\/foo/iu] }, %{ key: :remove_shortcode, @@ -251,7 +251,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. """, - suggestions: ["foo", ~r/foo/iu] + suggestions: ["https://example.org/foo.png", ~r/example.org\/foo/iu] }, %{ key: :federated_timeline_removal_shortcode, -- 2.34.1 From 945afa7ed4fdfb832f51b2f2715c3cb58b2942af Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 13 Jun 2023 14:53:20 -0400 Subject: [PATCH 07/11] Make EmojiPolicy aware of custom emoji reactions --- docs/docs/configuration/cheatsheet.md | 4 +- .../web/activity_pub/mrf/emoji_policy.ex | 33 +++++++++---- .../activity_pub/mrf/emoji_policy_test.exs | 46 +++++++++++++++++++ 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/docs/docs/configuration/cheatsheet.md b/docs/docs/configuration/cheatsheet.md index ae7103bea..31fd70897 100644 --- a/docs/docs/configuration/cheatsheet.md +++ b/docs/docs/configuration/cheatsheet.md @@ -223,8 +223,8 @@ Notes: - This MRF Policy is always enabled, if you want to disable it you have to set empty lists #### :mrf_emoji -* `remove_url`: A list of patterns which result in emoji whose URL matches being removed from the message. This will apply to both statuses and user profiles. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). -* `remove_shortcode`: A list of patterns which result in emoji whose shortcode matches being removed from the message. This will apply to both statuses and user profiles. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). +* `remove_url`: A list of patterns which result in emoji whose URL matches being removed from the message. This will apply to statuses, emoji reactions, and user profiles. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). +* `remove_shortcode`: A list of patterns which result in emoji whose shortcode matches being removed from the message. This will apply to statuses, emoji reactions, and user profiles. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). * `federated_timeline_removal_url`: A list of patterns which result in message with emojis whose URLs match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). * `federated_timeline_removal_shortcode`: A list of patterns which result in message with emojis whose shortcodes match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses. Each pattern can be a string or a [regular expression](https://hexdocs.pm/elixir/Regex.html). diff --git a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex index 9a2e8eade..13201ac55 100644 --- a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex @@ -55,6 +55,17 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do end end + @impl Pleroma.Web.ActivityPub.MRF.Policy + def filter(%{"type" => "EmojiReact"} = object) do + with {:ok, _} <- + matched_emoji_checker(config_remove_url(), config_remove_shortcode()).(object) do + {:ok, object} + else + _ -> + {:reject, "[EmojiPolicy] Rejected for having disallowed emoji"} + end + end + @impl Pleroma.Web.ActivityPub.MRF.Policy def filter(message) do {:ok, message} @@ -133,20 +144,24 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do end end - defp maybe_delist(%{"object" => object, "to" => to, "type" => "Create"} = activity) do - check = fn object -> - if any_emoji_match?(object, &url_from_tag/1, &url_from_emoji/1, config_unlist_url()) or + defp matched_emoji_checker(urls, shortcodes) do + fn object -> + if any_emoji_match?(object, &url_from_tag/1, &url_from_emoji/1, urls) or any_emoji_match?( object, &shortcode_from_tag/1, &shortcode_from_emoji/1, - config_unlist_shortcode() + shortcodes ) do - {:should_delist, nil} + {:matched, nil} else {:ok, %{}} end end + end + + defp maybe_delist(%{"object" => object, "to" => to, "type" => "Create"} = activity) do + check = matched_emoji_checker(config_unlist_url(), config_unlist_shortcode()) should_delist? = fn object -> with {:ok, _} <- Pleroma.Object.Updater.do_with_history(object, check) do @@ -173,7 +188,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do defp any_emoji_match?(object, extract_from_tag, extract_from_emoji, patterns) do Kernel.||( Enum.any?( - object["tag"], + object["tag"] || [], fn %{"type" => "Emoji"} = tag -> str = extract_from_tag.(tag) @@ -188,7 +203,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do false end ), - object["emoji"] + (object["emoji"] || []) |> Enum.any?(fn emoji -> match_any?(extract_from_emoji.(emoji), patterns) end) ) end @@ -227,7 +242,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do key: :remove_url, type: {:list, :string}, description: """ - A list of patterns which result in emoji whose URL matches being removed from the message. This will apply to both statuses and user profiles. + A list of patterns which result in emoji whose URL matches being removed from the message. This will apply to statuses, emoji reactions, and user profiles. Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. """, @@ -237,7 +252,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do key: :remove_shortcode, type: {:list, :string}, description: """ - A list of patterns which result in emoji whose shortcode matches being removed from the message. This will apply to both statuses and user profiles. + A list of patterns which result in emoji whose shortcode matches being removed from the message. This will apply to statuses, emoji reactions, and user profiles. Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`. """, diff --git a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs index 1e1b5c578..ae4c44f1c 100644 --- a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs @@ -109,6 +109,30 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do "cc" => ["https://example.org/someone"] } + @emoji_react_data %{ + "type" => "EmojiReact", + "tag" => [@emoji_tags |> Enum.at(3)], + "object" => "https://example.org/someobject", + "to" => ["https://example.org/self"], + "cc" => ["https://example.org/someone"] + } + + @emoji_react_data_matching_regex %{ + "type" => "EmojiReact", + "tag" => [@emoji_tags |> Enum.at(1)], + "object" => "https://example.org/someobject", + "to" => ["https://example.org/self"], + "cc" => ["https://example.org/someone"] + } + + @emoji_react_data_matching_nothing %{ + "type" => "EmojiReact", + "tag" => [@emoji_tags |> Enum.at(2)], + "object" => "https://example.org/someobject", + "to" => ["https://example.org/self"], + "cc" => ["https://example.org/someone"] + } + describe "remove_url" do setup do clear_config([:mrf_emoji, :remove_url], [ @@ -183,6 +207,17 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do assert %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} = item end + + test "processes EmojiReact" do + assert {:reject, "[EmojiPolicy] Rejected for having disallowed emoji"} == + MRF.filter_one(EmojiPolicy, @emoji_react_data) + + assert {:reject, "[EmojiPolicy] Rejected for having disallowed emoji"} == + MRF.filter_one(EmojiPolicy, @emoji_react_data_matching_regex) + + assert {:ok, @emoji_react_data_matching_nothing} == + MRF.filter_one(EmojiPolicy, @emoji_react_data_matching_nothing) + end end describe "remove_shortcode" do @@ -259,6 +294,17 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do assert %{"tag" => ^expected_tags, "emoji" => ^expected_emoji} = item end + + test "processes EmojiReact" do + assert {:reject, "[EmojiPolicy] Rejected for having disallowed emoji"} == + MRF.filter_one(EmojiPolicy, @emoji_react_data) + + assert {:reject, "[EmojiPolicy] Rejected for having disallowed emoji"} == + MRF.filter_one(EmojiPolicy, @emoji_react_data_matching_regex) + + assert {:ok, @emoji_react_data_matching_nothing} == + MRF.filter_one(EmojiPolicy, @emoji_react_data_matching_nothing) + end end describe "federated_timeline_removal_url" do -- 2.34.1 From 89207456754158376d1b674709e46331e515fbf8 Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 13 Jun 2023 14:55:27 -0400 Subject: [PATCH 08/11] Test that unicode emoji reactions are not affected --- .../web/activity_pub/mrf/emoji_policy_test.exs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs index ae4c44f1c..121ee1ea0 100644 --- a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs @@ -133,6 +133,14 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do "cc" => ["https://example.org/someone"] } + @emoji_react_data_unicode %{ + "type" => "EmojiReact", + "content" => "😍", + "object" => "https://example.org/someobject", + "to" => ["https://example.org/self"], + "cc" => ["https://example.org/someone"] + } + describe "remove_url" do setup do clear_config([:mrf_emoji, :remove_url], [ @@ -217,6 +225,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do assert {:ok, @emoji_react_data_matching_nothing} == MRF.filter_one(EmojiPolicy, @emoji_react_data_matching_nothing) + + assert {:ok, @emoji_react_data_unicode} == + MRF.filter_one(EmojiPolicy, @emoji_react_data_unicode) end end @@ -304,6 +315,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do assert {:ok, @emoji_react_data_matching_nothing} == MRF.filter_one(EmojiPolicy, @emoji_react_data_matching_nothing) + + assert {:ok, @emoji_react_data_unicode} == + MRF.filter_one(EmojiPolicy, @emoji_react_data_unicode) end end -- 2.34.1 From c9f331e7a0b418f2630dea9b0851c87e825e9d84 Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 13 Jun 2023 14:56:16 -0400 Subject: [PATCH 09/11] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee1e1765a..909ecd6ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Added a new configuration option to the MediaProxy feature that allows the blocking of specific domains from using the media proxy or being explicitly allowed by the Content-Security-Policy. - Please make sure instances you wanted to block media from are not in the MediaProxy `whitelist`, and instead use `blocklist`. - `OnlyMedia` Upload Filter to simplify restricting uploads to audio, image, and video types +- Implement `EmojiPolicy` MRF to reject or delist according to emojis ## 2023.05 -- 2.34.1 From c8ed181b17353fc52e64f24176f62d81db7e2ace Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 20 Jun 2023 10:14:01 -0400 Subject: [PATCH 10/11] Fix edge cases --- lib/pleroma/constants.ex | 12 +++ .../web/activity_pub/mrf/emoji_policy.ex | 79 ++++++++++--------- .../activity_pub/mrf/emoji_policy_test.exs | 33 ++++++++ 3 files changed, 87 insertions(+), 37 deletions(-) diff --git a/lib/pleroma/constants.ex b/lib/pleroma/constants.ex index 234dde1c9..208b30863 100644 --- a/lib/pleroma/constants.ex +++ b/lib/pleroma/constants.ex @@ -43,6 +43,18 @@ defmodule Pleroma.Constants do ] ) + const(status_object_types, + do: [ + "Note", + "Question", + "Audio", + "Video", + "Event", + "Article", + "Page" + ] + ) + const(updatable_object_types, do: [ "Note", diff --git a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex index 13201ac55..95d6a6d43 100644 --- a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex @@ -31,8 +31,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do def history_awareness, do: :manual @impl Pleroma.Web.ActivityPub.MRF.Policy - def filter(%{"type" => type, "object" => %{} = object} = message) - when type in ["Create", "Update"] do + def filter(%{"type" => type, "object" => %{"type" => objtype} = object} = message) + when type in ["Create", "Update"] and objtype in Pleroma.Constants.status_object_types() do with {:ok, object} <- Updater.do_with_history(object, fn object -> {:ok, process_remove(object, :url, config_remove_url())} @@ -102,46 +102,51 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do end defp process_remove_impl(object, extract_from_tag, extract_from_emoji, patterns) do - processed_tag = - Enum.filter( - object["tag"], - fn - %{"type" => "Emoji"} = tag -> - str = extract_from_tag.(tag) + object = + if object["tag"] do + Map.put( + object, + "tag", + Enum.filter( + object["tag"], + fn + %{"type" => "Emoji"} = tag -> + str = extract_from_tag.(tag) - if is_binary(str) do - not match_any?(str, patterns) - else - true + if is_binary(str) do + not match_any?(str, patterns) + else + true + end + + _ -> + true end - - _ -> - true - end - ) - - processed_emoji = - if object["emoji"] do - object["emoji"] - |> Enum.reduce(%{}, fn {name, url} = emoji, acc -> - if not match_any?(extract_from_emoji.(emoji), patterns) do - Map.put(acc, name, url) - else - acc - end - end) + ) + ) else - nil + object end - if processed_emoji do - object - |> Map.put("tag", processed_tag) - |> Map.put("emoji", processed_emoji) - else - object - |> Map.put("tag", processed_tag) - end + object = + if object["emoji"] do + Map.put( + object, + "emoji", + object["emoji"] + |> Enum.reduce(%{}, fn {name, url} = emoji, acc -> + if not match_any?(extract_from_emoji.(emoji), patterns) do + Map.put(acc, name, url) + else + acc + end + end) + ) + else + object + end + + object end defp matched_emoji_checker(urls, shortcodes) do diff --git a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs index 121ee1ea0..7350800f0 100644 --- a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs @@ -389,4 +389,37 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do } = filtered end end + + describe "edge cases" do + setup do + clear_config([:mrf_emoji, :remove_url], [ + "https://example.org/test.png", + ~r{/biribiri/mikoto_smile[23]\.png}, + "nekomimi_girl_emoji" + ]) + + :ok + end + + test "non-statuses" do + answer = @status_data |> put_in(["object", "type"], "Answer") + {:ok, filtered} = MRF.filter_one(EmojiPolicy, answer) + + assert filtered == answer + end + + test "without tag" do + status = @status_data |> Map.put("object", Map.drop(@status_data["object"], ["tag"])) + {:ok, filtered} = MRF.filter_one(EmojiPolicy, status) + + refute Map.has_key?(filtered["object"], "tag") + end + + test "without emoji" do + status = @status_data |> Map.put("object", Map.drop(@status_data["object"], ["emoji"])) + {:ok, filtered} = MRF.filter_one(EmojiPolicy, status) + + refute Map.has_key?(filtered["object"], "emoji") + end + end end -- 2.34.1 From 8c83c852304d0834dbfe76e91645ba7375e95437 Mon Sep 17 00:00:00 2001 From: tusooa Date: Fri, 7 Jul 2023 07:09:35 -0400 Subject: [PATCH 11/11] Make regex-to-string descriptor reusable --- .../web/activity_pub/mrf/emoji_policy.ex | 12 ++---------- .../web/activity_pub/mrf/keyword_policy.ex | 16 ++++------------ lib/pleroma/web/activity_pub/mrf/utils.ex | 15 +++++++++++++++ .../web/activity_pub/mrf/utils_test.exs | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 lib/pleroma/web/activity_pub/mrf/utils.ex create mode 100644 test/pleroma/web/activity_pub/mrf/utils_test.exs diff --git a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex index 95d6a6d43..f884962b9 100644 --- a/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/emoji_policy.ex @@ -6,6 +6,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do require Pleroma.Constants alias Pleroma.Object.Updater + alias Pleroma.Web.ActivityPub.MRF.Utils @moduledoc "Reject or force-unlisted emojis with certain URLs or names" @@ -215,19 +216,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do @impl Pleroma.Web.ActivityPub.MRF.Policy def describe do - # This horror is needed to convert regex sigils to strings mrf_emoji = Pleroma.Config.get(:mrf_emoji, []) |> Enum.map(fn {key, value} -> - {key, - Enum.map(value, fn - pattern -> - if not is_binary(pattern) do - inspect(pattern) - else - pattern - end - end)} + {key, Enum.map(value, &Utils.describe_regex_or_string/1)} end) |> Enum.into(%{}) diff --git a/lib/pleroma/web/activity_pub/mrf/keyword_policy.ex b/lib/pleroma/web/activity_pub/mrf/keyword_policy.ex index 7c921fc76..26a611427 100644 --- a/lib/pleroma/web/activity_pub/mrf/keyword_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/keyword_policy.ex @@ -5,6 +5,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do require Pleroma.Constants + alias Pleroma.Web.ActivityPub.MRF.Utils + @moduledoc "Reject or Word-Replace messages with a keyword or regex" @behaviour Pleroma.Web.ActivityPub.MRF.Policy @@ -128,7 +130,6 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do @impl true def describe do - # This horror is needed to convert regex sigils to strings mrf_keyword = Pleroma.Config.get(:mrf_keyword, []) |> Enum.map(fn {key, value} -> @@ -136,21 +137,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do Enum.map(value, fn {pattern, replacement} -> %{ - "pattern" => - if not is_binary(pattern) do - inspect(pattern) - else - pattern - end, + "pattern" => Utils.describe_regex_or_string(pattern), "replacement" => replacement } pattern -> - if not is_binary(pattern) do - inspect(pattern) - else - pattern - end + Utils.describe_regex_or_string(pattern) end)} end) |> Enum.into(%{}) diff --git a/lib/pleroma/web/activity_pub/mrf/utils.ex b/lib/pleroma/web/activity_pub/mrf/utils.ex new file mode 100644 index 000000000..f2dc9eea9 --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/utils.ex @@ -0,0 +1,15 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.Utils do + @spec describe_regex_or_string(String.t() | Regex.t()) :: String.t() + def describe_regex_or_string(pattern) do + # This horror is needed to convert regex sigils to strings + if not is_binary(pattern) do + inspect(pattern) + else + pattern + end + end +end diff --git a/test/pleroma/web/activity_pub/mrf/utils_test.exs b/test/pleroma/web/activity_pub/mrf/utils_test.exs new file mode 100644 index 000000000..3bbc2cfd3 --- /dev/null +++ b/test/pleroma/web/activity_pub/mrf/utils_test.exs @@ -0,0 +1,19 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.UtilsTest do + use Pleroma.DataCase, async: true + + alias Pleroma.Web.ActivityPub.MRF.Utils + + describe "describe_regex_or_string/1" do + test "describes regex" do + assert "~r/foo/i" == Utils.describe_regex_or_string(~r/foo/i) + end + + test "returns string as-is" do + assert "foo" == Utils.describe_regex_or_string("foo") + end + end +end -- 2.34.1