1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
6 @moduledoc "Filter activities depending on their origin instance"
7 @behaviour Pleroma.Web.ActivityPub.MRF
10 alias Pleroma.FollowingRelationship
12 alias Pleroma.Web.ActivityPub.MRF
14 require Pleroma.Constants
16 defp check_accept(%{host: actor_host} = _actor_info, object) do
18 Config.get([:mrf_simple, :accept])
19 |> MRF.subdomains_regex()
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"}
29 defp check_reject(%{host: actor_host} = _actor_info, object) do
31 Config.get([:mrf_simple, :reject])
32 |> MRF.subdomains_regex()
34 if MRF.subdomain_match?(rejects, actor_host) do
35 {:reject, "[SimplePolicy] host in reject list"}
41 defp check_media_removal(
42 %{host: actor_host} = _actor_info,
43 %{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object
45 when length(child_attachment) > 0 do
47 Config.get([:mrf_simple, :media_removal])
48 |> MRF.subdomains_regex()
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)
61 defp check_media_removal(_actor_info, object), do: {:ok, object}
63 defp check_media_nsfw(
64 %{host: actor_host} = _actor_info,
67 "object" => child_object
70 when is_map(child_object) do
72 Config.get([:mrf_simple, :media_nsfw])
73 |> MRF.subdomains_regex()
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)
88 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
90 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
92 Config.get([:mrf_simple, :federated_timeline_removal])
93 |> MRF.subdomains_regex()
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]
101 cc = List.delete(object["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
113 defp intersection(list1, list2) do
114 list1 -- list1 -- list2
117 defp check_followers_only(%{host: actor_host} = _actor_info, object) do
119 Config.get([:mrf_simple, :followers_only])
120 |> MRF.subdomains_regex()
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"] || []
129 to = FollowingRelationship.followers_ap_ids(user, fixed_to)
130 cc = FollowingRelationship.followers_ap_ids(user, fixed_cc)
133 |> Map.put("to", intersection([user.follower_address | to], fixed_to))
134 |> Map.put("cc", intersection([user.follower_address | cc], fixed_cc))
142 defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
144 Config.get([:mrf_simple, :report_removal])
145 |> MRF.subdomains_regex()
147 if MRF.subdomain_match?(report_removal, actor_host) do
148 {:reject, "[SimplePolicy] host in report_removal list"}
154 defp check_report_removal(_actor_info, object), do: {:ok, object}
156 defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do
158 Config.get([:mrf_simple, :avatar_removal])
159 |> MRF.subdomains_regex()
161 if MRF.subdomain_match?(avatar_removal, actor_host) do
162 {:ok, Map.delete(object, "icon")}
168 defp check_avatar_removal(_actor_info, object), do: {:ok, object}
170 defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do
172 Config.get([:mrf_simple, :banner_removal])
173 |> MRF.subdomains_regex()
175 if MRF.subdomain_match?(banner_removal, actor_host) do
176 {:ok, Map.delete(object, "image")}
182 defp check_banner_removal(_actor_info, object), do: {:ok, object}
185 def filter(%{"type" => "Delete", "actor" => actor} = object) do
186 %{host: actor_host} = URI.parse(actor)
189 Config.get([:mrf_simple, :reject_deletes])
190 |> MRF.subdomains_regex()
192 if MRF.subdomain_match?(reject_deletes, actor_host) do
193 {:reject, "[SimplePolicy] host in reject_deletes list"}
200 def filter(%{"actor" => actor} = object) do
201 actor_info = URI.parse(actor)
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
212 {:reject, nil} -> {:reject, "[SimplePolicy]"}
213 {:reject, _} = e -> e
214 _ -> {:reject, "[SimplePolicy]"}
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)
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
228 {:reject, nil} -> {:reject, "[SimplePolicy]"}
229 {:reject, _} = e -> e
230 _ -> {:reject, "[SimplePolicy]"}
234 def filter(object), do: {:ok, object}
238 exclusions = Config.get([:mrf, :transparency_exclusions])
241 Config.get(:mrf_simple)
242 |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn v -> v in exclusions end)} end)
245 {:ok, %{mrf_simple: mrf_simple}}
249 def config_description do
252 related_policy: "Pleroma.Web.ActivityPub.MRF.SimplePolicy",
254 description: "Simple ingress policies",
258 type: {:list, :string},
259 description: "List of instances to strip media attachments from",
260 suggestions: ["example.com", "*.example.com"]
265 type: {:list, :string},
266 description: "List of instances to tag all media as NSFW (sensitive) from",
267 suggestions: ["example.com", "*.example.com"]
270 key: :federated_timeline_removal,
271 type: {:list, :string},
273 "List of instances to remove from the Federated (aka The Whole Known Network) Timeline",
274 suggestions: ["example.com", "*.example.com"]
278 type: {:list, :string},
279 description: "List of instances to reject activities from (except deletes)",
280 suggestions: ["example.com", "*.example.com"]
284 type: {:list, :string},
285 description: "List of instances to only accept activities from (except deletes)",
286 suggestions: ["example.com", "*.example.com"]
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"]
295 key: :report_removal,
296 type: {:list, :string},
297 description: "List of instances to reject reports from",
298 suggestions: ["example.com", "*.example.com"]
301 key: :avatar_removal,
302 type: {:list, :string},
303 description: "List of instances to strip avatars from",
304 suggestions: ["example.com", "*.example.com"]
307 key: :banner_removal,
308 type: {:list, :string},
309 description: "List of instances to strip banners from",
310 suggestions: ["example.com", "*.example.com"]
313 key: :reject_deletes,
314 type: {:list, :string},
315 description: "List of instances to reject deletions from",
316 suggestions: ["example.com", "*.example.com"]