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