Do not pass transient undo-y activities through MRF
[akkoma] / lib / pleroma / web / activity_pub / mrf.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF do
6 require Logger
7
8 @behaviour Pleroma.Web.ActivityPub.MRF.PipelineFiltering
9
10 @mrf_config_descriptions [
11 %{
12 group: :pleroma,
13 key: :mrf,
14 tab: :mrf,
15 label: "MRF",
16 type: :group,
17 description: "General MRF settings",
18 children: [
19 %{
20 key: :policies,
21 type: [:module, {:list, :module}],
22 description:
23 "A list of MRF policies enabled. Module names are shortened (removed leading `Pleroma.Web.ActivityPub.MRF.` part), but on adding custom module you need to use full name.",
24 suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF.Policy}
25 },
26 %{
27 key: :transparency,
28 label: "MRF transparency",
29 type: :boolean,
30 description:
31 "Make the content of your Message Rewrite Facility settings public (via nodeinfo)"
32 },
33 %{
34 key: :transparency_exclusions,
35 label: "MRF transparency exclusions",
36 type: {:list, :tuple},
37 key_placeholder: "instance",
38 value_placeholder: "reason",
39 description:
40 "Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value. You can also provide a reason for excluding these instance names. The instances and reasons won't be publicly disclosed.",
41 suggestions: [
42 "exclusion.com"
43 ]
44 },
45 %{
46 key: :transparency_obfuscate_domains,
47 label: "MRF domain obfuscation",
48 type: {:list, :string},
49 description:
50 "Obfuscate domains in MRF transparency. This is useful if the domain you're blocking contains words you don't want displayed, but still want to disclose the MRF settings.",
51 suggestions: [
52 "badword.com"
53 ]
54 }
55 ]
56 }
57 ]
58
59 @default_description %{
60 label: "",
61 description: ""
62 }
63
64 @required_description_keys [:key, :related_policy]
65
66 def filter_one(policy, %{"type" => type} = message)
67 when type in ["Undo", "Block", "Delete"] and
68 policy != Pleroma.Web.ActivityPub.MRF.SimplePolicy do
69 {:ok, message}
70 end
71
72 def filter_one(policy, message) do
73 should_plug_history? =
74 if function_exported?(policy, :history_awareness, 0) do
75 policy.history_awareness()
76 else
77 :manual
78 end
79 |> Kernel.==(:auto)
80
81 if not should_plug_history? do
82 policy.filter(message)
83 else
84 main_result = policy.filter(message)
85
86 with {_, {:ok, main_message}} <- {:main, main_result},
87 {_,
88 %{
89 "formerRepresentations" => %{
90 "orderedItems" => [_ | _]
91 }
92 }} = {_, object} <- {:object, message["object"]},
93 {_, {:ok, new_history}} <-
94 {:history,
95 Pleroma.Object.Updater.for_each_history_item(
96 object["formerRepresentations"],
97 object,
98 fn item ->
99 with {:ok, filtered} <- policy.filter(Map.put(message, "object", item)) do
100 {:ok, filtered["object"]}
101 else
102 e -> e
103 end
104 end
105 )} do
106 {:ok, put_in(main_message, ["object", "formerRepresentations"], new_history)}
107 else
108 {:main, _} -> main_result
109 {:object, _} -> main_result
110 {:history, e} -> e
111 end
112 end
113 end
114
115 def filter(policies, %{} = message) do
116 policies
117 |> Enum.reduce({:ok, message}, fn
118 policy, {:ok, message} -> filter_one(policy, message)
119 _, error -> error
120 end)
121 end
122
123 def filter(%{} = object), do: get_policies() |> filter(object)
124
125 @impl true
126 def pipeline_filter(%{} = message, meta) do
127 object = meta[:object_data]
128 ap_id = message["object"]
129
130 if object && ap_id do
131 with {:ok, message} <- filter(Map.put(message, "object", object)) do
132 meta = Keyword.put(meta, :object_data, message["object"])
133 {:ok, Map.put(message, "object", ap_id), meta}
134 else
135 {err, message} -> {err, message, meta}
136 end
137 else
138 {err, message} = filter(message)
139
140 {err, message, meta}
141 end
142 end
143
144 def get_policies do
145 Pleroma.Config.get([:mrf, :policies], [])
146 |> get_policies()
147 |> Enum.concat([
148 Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
149 Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy,
150 Pleroma.Web.ActivityPub.MRF.NormalizeMarkup
151 ])
152 |> Enum.uniq()
153 end
154
155 defp get_policies(policy) when is_atom(policy), do: [policy]
156 defp get_policies(policies) when is_list(policies), do: policies
157 defp get_policies(_), do: []
158
159 # Matches the following:
160 # - https://baddomain.net
161 # - https://extra.baddomain.net/
162 # Does NOT match the following:
163 # - https://maybebaddomain.net/
164 def subdomain_regex("*." <> domain), do: subdomain_regex(domain)
165
166 def subdomain_regex(domain) do
167 ~r/^(.+\.)?#{Regex.escape(domain)}$/i
168 end
169
170 @spec subdomains_regex([String.t()]) :: [Regex.t()]
171 def subdomains_regex(domains) when is_list(domains) do
172 Enum.map(domains, &subdomain_regex/1)
173 end
174
175 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
176 def subdomain_match?(domains, host) do
177 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
178 end
179
180 @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
181 def instance_list_from_tuples(list) do
182 Enum.map(list, fn {instance, _} -> instance end)
183 end
184
185 def describe(policies) do
186 {:ok, policy_configs} =
187 policies
188 |> Enum.reduce({:ok, %{}}, fn
189 policy, {:ok, data} ->
190 {:ok, policy_data} = policy.describe()
191 {:ok, Map.merge(data, policy_data)}
192
193 _, error ->
194 error
195 end)
196
197 mrf_policies =
198 get_policies()
199 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
200
201 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
202
203 base =
204 %{
205 mrf_policies: mrf_policies,
206 exclusions: length(exclusions) > 0
207 }
208 |> Map.merge(policy_configs)
209
210 {:ok, base}
211 end
212
213 def describe, do: get_policies() |> describe()
214
215 def config_descriptions do
216 Pleroma.Web.ActivityPub.MRF.Policy
217 |> Pleroma.Docs.Generator.list_behaviour_implementations()
218 |> config_descriptions()
219 end
220
221 def config_descriptions(policies) do
222 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
223 if function_exported?(policy, :config_description, 0) do
224 description =
225 @default_description
226 |> Map.merge(policy.config_description)
227 |> Map.put(:group, :pleroma)
228 |> Map.put(:tab, :mrf)
229 |> Map.put(:type, :group)
230
231 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
232 [description | acc]
233 else
234 Logger.warn(
235 "#{policy} config description doesn't have one or all required keys #{inspect(@required_description_keys)}"
236 )
237
238 acc
239 end
240 else
241 Logger.debug(
242 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."
243 )
244
245 acc
246 end
247 end)
248 end
249 end