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