Post editing (#202)
[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, message) do
67 should_plug_history? =
68 if function_exported?(policy, :history_awareness, 0) do
69 policy.history_awareness()
70 else
71 :manual
72 end
73 |> Kernel.==(:auto)
74
75 if not should_plug_history? do
76 policy.filter(message)
77 else
78 main_result = policy.filter(message)
79
80 with {_, {:ok, main_message}} <- {:main, main_result},
81 {_,
82 %{
83 "formerRepresentations" => %{
84 "orderedItems" => [_ | _]
85 }
86 }} = {_, object} <- {:object, message["object"]},
87 {_, {:ok, new_history}} <-
88 {:history,
89 Pleroma.Object.Updater.for_each_history_item(
90 object["formerRepresentations"],
91 object,
92 fn item ->
93 with {:ok, filtered} <- policy.filter(Map.put(message, "object", item)) do
94 {:ok, filtered["object"]}
95 else
96 e -> e
97 end
98 end
99 )} do
100 {:ok, put_in(main_message, ["object", "formerRepresentations"], new_history)}
101 else
102 {:main, _} -> main_result
103 {:object, _} -> main_result
104 {:history, e} -> e
105 end
106 end
107 end
108
109 def filter(policies, %{} = message) do
110 policies
111 |> Enum.reduce({:ok, message}, fn
112 policy, {:ok, message} -> filter_one(policy, message)
113 _, error -> error
114 end)
115 end
116
117 def filter(%{} = object), do: get_policies() |> filter(object)
118
119 @impl true
120 def pipeline_filter(%{} = message, meta) do
121 object = meta[:object_data]
122 ap_id = message["object"]
123
124 if object && ap_id do
125 with {:ok, message} <- filter(Map.put(message, "object", object)) do
126 meta = Keyword.put(meta, :object_data, message["object"])
127 {:ok, Map.put(message, "object", ap_id), meta}
128 else
129 {err, message} -> {err, message, meta}
130 end
131 else
132 {err, message} = filter(message)
133
134 {err, message, meta}
135 end
136 end
137
138 def get_policies do
139 Pleroma.Config.get([:mrf, :policies], [])
140 |> get_policies()
141 |> Enum.concat([
142 Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
143 Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy
144 ])
145 |> Enum.uniq()
146 end
147
148 defp get_policies(policy) when is_atom(policy), do: [policy]
149 defp get_policies(policies) when is_list(policies), do: policies
150 defp get_policies(_), do: []
151
152 @spec subdomains_regex([String.t()]) :: [Regex.t()]
153 def subdomains_regex(domains) when is_list(domains) do
154 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
155 end
156
157 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
158 def subdomain_match?(domains, host) do
159 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
160 end
161
162 @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
163 def instance_list_from_tuples(list) do
164 Enum.map(list, fn {instance, _} -> instance end)
165 end
166
167 def describe(policies) do
168 {:ok, policy_configs} =
169 policies
170 |> Enum.reduce({:ok, %{}}, fn
171 policy, {:ok, data} ->
172 {:ok, policy_data} = policy.describe()
173 {:ok, Map.merge(data, policy_data)}
174
175 _, error ->
176 error
177 end)
178
179 mrf_policies =
180 get_policies()
181 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
182
183 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
184
185 base =
186 %{
187 mrf_policies: mrf_policies,
188 exclusions: length(exclusions) > 0
189 }
190 |> Map.merge(policy_configs)
191
192 {:ok, base}
193 end
194
195 def describe, do: get_policies() |> describe()
196
197 def config_descriptions do
198 Pleroma.Web.ActivityPub.MRF.Policy
199 |> Pleroma.Docs.Generator.list_behaviour_implementations()
200 |> config_descriptions()
201 end
202
203 def config_descriptions(policies) do
204 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
205 if function_exported?(policy, :config_description, 0) do
206 description =
207 @default_description
208 |> Map.merge(policy.config_description)
209 |> Map.put(:group, :pleroma)
210 |> Map.put(:tab, :mrf)
211 |> Map.put(:type, :group)
212
213 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
214 [description | acc]
215 else
216 Logger.warn(
217 "#{policy} config description doesn't have one or all required keys #{inspect(@required_description_keys)}"
218 )
219
220 acc
221 end
222 else
223 Logger.debug(
224 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."
225 )
226
227 acc
228 end
229 end)
230 end
231 end