6cd91826db178ba72f9cc0859fe7eac5c7397eca
[akkoma] / lib / pleroma / web / activity_pub / mrf / simple_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.SimplePolicy do
6 @moduledoc "Filter activities depending on their origin instance"
7 @behaviour Pleroma.Web.ActivityPub.MRF
8
9 alias Pleroma.Config
10 alias Pleroma.FollowingRelationship
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.MRF
13
14 require Pleroma.Constants
15
16 defp check_accept(%{host: actor_host} = _actor_info, object) do
17 accepts =
18 Config.get([:mrf_simple, :accept])
19 |> MRF.subdomains_regex()
20
21 cond do
22 accepts == [] -> {:ok, object}
23 actor_host == Config.get([Pleroma.Web.Endpoint, :url, :host]) -> {:ok, object}
24 MRF.subdomain_match?(accepts, actor_host) -> {:ok, object}
25 true -> {:reject, "[SimplePolicy] host not in accept list"}
26 end
27 end
28
29 defp check_reject(%{host: actor_host} = _actor_info, object) do
30 rejects =
31 Config.get([:mrf_simple, :reject])
32 |> MRF.subdomains_regex()
33
34 if MRF.subdomain_match?(rejects, actor_host) do
35 {:reject, "[SimplePolicy] host in reject list"}
36 else
37 {:ok, object}
38 end
39 end
40
41 defp check_media_removal(
42 %{host: actor_host} = _actor_info,
43 %{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object
44 )
45 when length(child_attachment) > 0 do
46 media_removal =
47 Config.get([:mrf_simple, :media_removal])
48 |> MRF.subdomains_regex()
49
50 object =
51 if MRF.subdomain_match?(media_removal, actor_host) do
52 child_object = Map.delete(object["object"], "attachment")
53 Map.put(object, "object", child_object)
54 else
55 object
56 end
57
58 {:ok, object}
59 end
60
61 defp check_media_removal(_actor_info, object), do: {:ok, object}
62
63 defp check_media_nsfw(
64 %{host: actor_host} = _actor_info,
65 %{
66 "type" => "Create",
67 "object" => child_object
68 } = object
69 )
70 when is_map(child_object) do
71 media_nsfw =
72 Config.get([:mrf_simple, :media_nsfw])
73 |> MRF.subdomains_regex()
74
75 object =
76 if MRF.subdomain_match?(media_nsfw, actor_host) do
77 tags = (child_object["tag"] || []) ++ ["nsfw"]
78 child_object = Map.put(child_object, "tag", tags)
79 child_object = Map.put(child_object, "sensitive", true)
80 Map.put(object, "object", child_object)
81 else
82 object
83 end
84
85 {:ok, object}
86 end
87
88 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
89
90 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
91 timeline_removal =
92 Config.get([:mrf_simple, :federated_timeline_removal])
93 |> MRF.subdomains_regex()
94
95 object =
96 with true <- MRF.subdomain_match?(timeline_removal, actor_host),
97 user <- User.get_cached_by_ap_id(object["actor"]),
98 true <- Pleroma.Constants.as_public() in object["to"] do
99 to = List.delete(object["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
100
101 cc = List.delete(object["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
102
103 object
104 |> Map.put("to", to)
105 |> Map.put("cc", cc)
106 else
107 _ -> object
108 end
109
110 {:ok, object}
111 end
112
113 defp intersection(list1, list2) do
114 list1 -- list1 -- list2
115 end
116
117 defp check_followers_only(%{host: actor_host} = _actor_info, object) do
118 followers_only =
119 Config.get([:mrf_simple, :followers_only])
120 |> MRF.subdomains_regex()
121
122 object =
123 with true <- MRF.subdomain_match?(followers_only, actor_host),
124 user <- User.get_cached_by_ap_id(object["actor"]) do
125 # Don't use Map.get/3 intentionally, these must not be nil
126 fixed_to = object["to"] || []
127 fixed_cc = object["cc"] || []
128
129 to = FollowingRelationship.followers_ap_ids(user, fixed_to)
130 cc = FollowingRelationship.followers_ap_ids(user, fixed_cc)
131
132 object
133 |> Map.put("to", intersection([user.follower_address | to], fixed_to))
134 |> Map.put("cc", intersection([user.follower_address | cc], fixed_cc))
135 else
136 _ -> object
137 end
138
139 {:ok, object}
140 end
141
142 defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
143 report_removal =
144 Config.get([:mrf_simple, :report_removal])
145 |> MRF.subdomains_regex()
146
147 if MRF.subdomain_match?(report_removal, actor_host) do
148 {:reject, "[SimplePolicy] host in report_removal list"}
149 else
150 {:ok, object}
151 end
152 end
153
154 defp check_report_removal(_actor_info, object), do: {:ok, object}
155
156 defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do
157 avatar_removal =
158 Config.get([:mrf_simple, :avatar_removal])
159 |> MRF.subdomains_regex()
160
161 if MRF.subdomain_match?(avatar_removal, actor_host) do
162 {:ok, Map.delete(object, "icon")}
163 else
164 {:ok, object}
165 end
166 end
167
168 defp check_avatar_removal(_actor_info, object), do: {:ok, object}
169
170 defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do
171 banner_removal =
172 Config.get([:mrf_simple, :banner_removal])
173 |> MRF.subdomains_regex()
174
175 if MRF.subdomain_match?(banner_removal, actor_host) do
176 {:ok, Map.delete(object, "image")}
177 else
178 {:ok, object}
179 end
180 end
181
182 defp check_banner_removal(_actor_info, object), do: {:ok, object}
183
184 @impl true
185 def filter(%{"type" => "Delete", "actor" => actor} = object) do
186 %{host: actor_host} = URI.parse(actor)
187
188 reject_deletes =
189 Config.get([:mrf_simple, :reject_deletes])
190 |> MRF.subdomains_regex()
191
192 if MRF.subdomain_match?(reject_deletes, actor_host) do
193 {:reject, "[SimplePolicy] host in reject_deletes list"}
194 else
195 {:ok, object}
196 end
197 end
198
199 @impl true
200 def filter(%{"actor" => actor} = object) do
201 actor_info = URI.parse(actor)
202
203 with {:ok, object} <- check_accept(actor_info, object),
204 {:ok, object} <- check_reject(actor_info, object),
205 {:ok, object} <- check_media_removal(actor_info, object),
206 {:ok, object} <- check_media_nsfw(actor_info, object),
207 {:ok, object} <- check_ftl_removal(actor_info, object),
208 {:ok, object} <- check_followers_only(actor_info, object),
209 {:ok, object} <- check_report_removal(actor_info, object) do
210 {:ok, object}
211 else
212 {:reject, nil} -> {:reject, "[SimplePolicy]"}
213 {:reject, _} = e -> e
214 _ -> {:reject, "[SimplePolicy]"}
215 end
216 end
217
218 def filter(%{"id" => actor, "type" => obj_type} = object)
219 when obj_type in ["Application", "Group", "Organization", "Person", "Service"] do
220 actor_info = URI.parse(actor)
221
222 with {:ok, object} <- check_accept(actor_info, object),
223 {:ok, object} <- check_reject(actor_info, object),
224 {:ok, object} <- check_avatar_removal(actor_info, object),
225 {:ok, object} <- check_banner_removal(actor_info, object) do
226 {:ok, object}
227 else
228 {:reject, nil} -> {:reject, "[SimplePolicy]"}
229 {:reject, _} = e -> e
230 _ -> {:reject, "[SimplePolicy]"}
231 end
232 end
233
234 def filter(object), do: {:ok, object}
235
236 @impl true
237 def describe do
238 exclusions = Config.get([:mrf, :transparency_exclusions])
239
240 mrf_simple =
241 Config.get(:mrf_simple)
242 |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn v -> v in exclusions end)} end)
243 |> Enum.into(%{})
244
245 {:ok, %{mrf_simple: mrf_simple}}
246 end
247
248 @impl true
249 def config_description do
250 %{
251 key: :mrf_simple,
252 related_policy: "Pleroma.Web.ActivityPub.MRF.SimplePolicy",
253 label: "MRF Simple",
254 description: "Simple ingress policies",
255 children: [
256 %{
257 key: :media_removal,
258 type: {:list, :string},
259 description: "List of instances to strip media attachments from",
260 suggestions: ["example.com", "*.example.com"]
261 },
262 %{
263 key: :media_nsfw,
264 label: "Media NSFW",
265 type: {:list, :string},
266 description: "List of instances to tag all media as NSFW (sensitive) from",
267 suggestions: ["example.com", "*.example.com"]
268 },
269 %{
270 key: :federated_timeline_removal,
271 type: {:list, :string},
272 description:
273 "List of instances to remove from the Federated (aka The Whole Known Network) Timeline",
274 suggestions: ["example.com", "*.example.com"]
275 },
276 %{
277 key: :reject,
278 type: {:list, :string},
279 description: "List of instances to reject activities from (except deletes)",
280 suggestions: ["example.com", "*.example.com"]
281 },
282 %{
283 key: :accept,
284 type: {:list, :string},
285 description: "List of instances to only accept activities from (except deletes)",
286 suggestions: ["example.com", "*.example.com"]
287 },
288 %{
289 key: :followers_only,
290 type: {:list, :string},
291 description: "Force posts from the given instances to be visible by followers only",
292 suggestions: ["example.com", "*.example.com"]
293 },
294 %{
295 key: :report_removal,
296 type: {:list, :string},
297 description: "List of instances to reject reports from",
298 suggestions: ["example.com", "*.example.com"]
299 },
300 %{
301 key: :avatar_removal,
302 type: {:list, :string},
303 description: "List of instances to strip avatars from",
304 suggestions: ["example.com", "*.example.com"]
305 },
306 %{
307 key: :banner_removal,
308 type: {:list, :string},
309 description: "List of instances to strip banners from",
310 suggestions: ["example.com", "*.example.com"]
311 },
312 %{
313 key: :reject_deletes,
314 type: {:list, :string},
315 description: "List of instances to reject deletions from",
316 suggestions: ["example.com", "*.example.com"]
317 }
318 ]
319 }
320 end
321 end