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
71 Config.get([:mrf_simple, :media_nsfw])
72 |> MRF.subdomains_regex()
75 if MRF.subdomain_match?(media_nsfw, actor_host) do
76 Kernel.put_in(object, ["object", "sensitive"], true)
84 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
86 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
88 Config.get([:mrf_simple, :federated_timeline_removal])
89 |> MRF.subdomains_regex()
92 with true <- MRF.subdomain_match?(timeline_removal, actor_host),
93 user <- User.get_cached_by_ap_id(object["actor"]),
94 true <- Pleroma.Constants.as_public() in object["to"] do
95 to = List.delete(object["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
97 cc = List.delete(object["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
109 defp intersection(list1, list2) do
110 list1 -- list1 -- list2
113 defp check_followers_only(%{host: actor_host} = _actor_info, object) do
115 Config.get([:mrf_simple, :followers_only])
116 |> MRF.subdomains_regex()
119 with true <- MRF.subdomain_match?(followers_only, actor_host),
120 user <- User.get_cached_by_ap_id(object["actor"]) do
121 # Don't use Map.get/3 intentionally, these must not be nil
122 fixed_to = object["to"] || []
123 fixed_cc = object["cc"] || []
125 to = FollowingRelationship.followers_ap_ids(user, fixed_to)
126 cc = FollowingRelationship.followers_ap_ids(user, fixed_cc)
129 |> Map.put("to", intersection([user.follower_address | to], fixed_to))
130 |> Map.put("cc", intersection([user.follower_address | cc], fixed_cc))
138 defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
140 Config.get([:mrf_simple, :report_removal])
141 |> MRF.subdomains_regex()
143 if MRF.subdomain_match?(report_removal, actor_host) do
144 {:reject, "[SimplePolicy] host in report_removal list"}
150 defp check_report_removal(_actor_info, object), do: {:ok, object}
152 defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do
154 Config.get([:mrf_simple, :avatar_removal])
155 |> MRF.subdomains_regex()
157 if MRF.subdomain_match?(avatar_removal, actor_host) do
158 {:ok, Map.delete(object, "icon")}
164 defp check_avatar_removal(_actor_info, object), do: {:ok, object}
166 defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do
168 Config.get([:mrf_simple, :banner_removal])
169 |> MRF.subdomains_regex()
171 if MRF.subdomain_match?(banner_removal, actor_host) do
172 {:ok, Map.delete(object, "image")}
178 defp check_banner_removal(_actor_info, object), do: {:ok, object}
181 def filter(%{"type" => "Delete", "actor" => actor} = object) do
182 %{host: actor_host} = URI.parse(actor)
185 Config.get([:mrf_simple, :reject_deletes])
186 |> MRF.subdomains_regex()
188 if MRF.subdomain_match?(reject_deletes, actor_host) do
189 {:reject, "[SimplePolicy] host in reject_deletes list"}
196 def filter(%{"actor" => actor} = object) do
197 actor_info = URI.parse(actor)
199 with {:ok, object} <- check_accept(actor_info, object),
200 {:ok, object} <- check_reject(actor_info, object),
201 {:ok, object} <- check_media_removal(actor_info, object),
202 {:ok, object} <- check_media_nsfw(actor_info, object),
203 {:ok, object} <- check_ftl_removal(actor_info, object),
204 {:ok, object} <- check_followers_only(actor_info, object),
205 {:ok, object} <- check_report_removal(actor_info, object) do
208 {:reject, nil} -> {:reject, "[SimplePolicy]"}
209 {:reject, _} = e -> e
210 _ -> {:reject, "[SimplePolicy]"}
214 def filter(%{"id" => actor, "type" => obj_type} = object)
215 when obj_type in ["Application", "Group", "Organization", "Person", "Service"] do
216 actor_info = URI.parse(actor)
218 with {:ok, object} <- check_accept(actor_info, object),
219 {:ok, object} <- check_reject(actor_info, object),
220 {:ok, object} <- check_avatar_removal(actor_info, object),
221 {:ok, object} <- check_banner_removal(actor_info, object) do
224 {:reject, nil} -> {:reject, "[SimplePolicy]"}
225 {:reject, _} = e -> e
226 _ -> {:reject, "[SimplePolicy]"}
230 def filter(object), do: {:ok, object}
234 exclusions = Config.get([:mrf, :transparency_exclusions])
237 Config.get(:mrf_simple)
238 |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn v -> v in exclusions end)} end)
241 {:ok, %{mrf_simple: mrf_simple}}
245 def config_description do
248 related_policy: "Pleroma.Web.ActivityPub.MRF.SimplePolicy",
250 description: "Simple ingress policies",
254 type: {:list, :string},
255 description: "List of instances to strip media attachments from",
256 suggestions: ["example.com", "*.example.com"]
261 type: {:list, :string},
262 description: "List of instances to tag all media as NSFW (sensitive) from",
263 suggestions: ["example.com", "*.example.com"]
266 key: :federated_timeline_removal,
267 type: {:list, :string},
269 "List of instances to remove from the Federated (aka The Whole Known Network) Timeline",
270 suggestions: ["example.com", "*.example.com"]
274 type: {:list, :string},
275 description: "List of instances to reject activities from (except deletes)",
276 suggestions: ["example.com", "*.example.com"]
280 type: {:list, :string},
281 description: "List of instances to only accept activities from (except deletes)",
282 suggestions: ["example.com", "*.example.com"]
285 key: :followers_only,
286 type: {:list, :string},
287 description: "Force posts from the given instances to be visible by followers only",
288 suggestions: ["example.com", "*.example.com"]
291 key: :report_removal,
292 type: {:list, :string},
293 description: "List of instances to reject reports from",
294 suggestions: ["example.com", "*.example.com"]
297 key: :avatar_removal,
298 type: {:list, :string},
299 description: "List of instances to strip avatars from",
300 suggestions: ["example.com", "*.example.com"]
303 key: :banner_removal,
304 type: {:list, :string},
305 description: "List of instances to strip banners from",
306 suggestions: ["example.com", "*.example.com"]
309 key: :reject_deletes,
310 type: {:list, :string},
311 description: "List of instances to reject deletions from",
312 suggestions: ["example.com", "*.example.com"]