MRF Policies: Return a {:reject, reason} instead of {:reject, nil}
[akkoma] / lib / pleroma / web / activity_pub / mrf / keyword_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
6 require Pleroma.Constants
7
8 @moduledoc "Reject or Word-Replace messages with a keyword or regex"
9
10 @behaviour Pleroma.Web.ActivityPub.MRF
11 defp string_matches?(string, _) when not is_binary(string) do
12 false
13 end
14
15 defp string_matches?(string, pattern) when is_binary(pattern) do
16 String.contains?(string, pattern)
17 end
18
19 defp string_matches?(string, pattern) do
20 String.match?(string, pattern)
21 end
22
23 defp check_reject(%{"object" => %{"content" => content, "summary" => summary}} = message) do
24 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :reject]), fn pattern ->
25 string_matches?(content, pattern) or string_matches?(summary, pattern)
26 end) do
27 {:reject, "[KeywordPolicy] Matches with rejected keyword"}
28 else
29 {:ok, message}
30 end
31 end
32
33 defp check_ftl_removal(
34 %{"to" => to, "object" => %{"content" => content, "summary" => summary}} = message
35 ) do
36 if Pleroma.Constants.as_public() in to and
37 Enum.any?(Pleroma.Config.get([:mrf_keyword, :federated_timeline_removal]), fn pattern ->
38 string_matches?(content, pattern) or string_matches?(summary, pattern)
39 end) do
40 to = List.delete(to, Pleroma.Constants.as_public())
41 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
42
43 message =
44 message
45 |> Map.put("to", to)
46 |> Map.put("cc", cc)
47
48 {:ok, message}
49 else
50 {:ok, message}
51 end
52 end
53
54 defp check_replace(%{"object" => %{"content" => content, "summary" => summary}} = message) do
55 content =
56 if is_binary(content) do
57 content
58 else
59 ""
60 end
61
62 summary =
63 if is_binary(summary) do
64 summary
65 else
66 ""
67 end
68
69 {content, summary} =
70 Enum.reduce(
71 Pleroma.Config.get([:mrf_keyword, :replace]),
72 {content, summary},
73 fn {pattern, replacement}, {content_acc, summary_acc} ->
74 {String.replace(content_acc, pattern, replacement),
75 String.replace(summary_acc, pattern, replacement)}
76 end
77 )
78
79 {:ok,
80 message
81 |> put_in(["object", "content"], content)
82 |> put_in(["object", "summary"], summary)}
83 end
84
85 @impl true
86 def filter(%{"type" => "Create", "object" => %{"content" => _content}} = message) do
87 with {:ok, message} <- check_reject(message),
88 {:ok, message} <- check_ftl_removal(message),
89 {:ok, message} <- check_replace(message) do
90 {:ok, message}
91 else
92 {:reject, nil} -> {:reject, "[KeywordPolicy] "}
93 {:reject, _} = e -> e
94 _e -> {:reject, "[KeywordPolicy] "}
95 end
96 end
97
98 @impl true
99 def filter(message), do: {:ok, message}
100
101 @impl true
102 def describe do
103 # This horror is needed to convert regex sigils to strings
104 mrf_keyword =
105 Pleroma.Config.get(:mrf_keyword, [])
106 |> Enum.map(fn {key, value} ->
107 {key,
108 Enum.map(value, fn
109 {pattern, replacement} ->
110 %{
111 "pattern" =>
112 if not is_binary(pattern) do
113 inspect(pattern)
114 else
115 pattern
116 end,
117 "replacement" => replacement
118 }
119
120 pattern ->
121 if not is_binary(pattern) do
122 inspect(pattern)
123 else
124 pattern
125 end
126 end)}
127 end)
128 |> Enum.into(%{})
129
130 {:ok, %{mrf_keyword: mrf_keyword}}
131 end
132 end