Apply suggestion to config/description.exs
[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 alias Pleroma.User
7 alias Pleroma.Web.ActivityPub.MRF
8 @moduledoc "Filter activities depending on their origin instance"
9 @behaviour Pleroma.Web.ActivityPub.MRF
10
11 require Pleroma.Constants
12
13 defp check_accept(%{host: actor_host} = _actor_info, object) do
14 accepts =
15 Pleroma.Config.get([:mrf_simple, :accept])
16 |> MRF.subdomains_regex()
17
18 cond do
19 accepts == [] -> {:ok, object}
20 actor_host == Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host]) -> {:ok, object}
21 MRF.subdomain_match?(accepts, actor_host) -> {:ok, object}
22 true -> {:reject, nil}
23 end
24 end
25
26 defp check_reject(%{host: actor_host} = _actor_info, object) do
27 rejects =
28 Pleroma.Config.get([:mrf_simple, :reject])
29 |> MRF.subdomains_regex()
30
31 if MRF.subdomain_match?(rejects, actor_host) do
32 {:reject, nil}
33 else
34 {:ok, object}
35 end
36 end
37
38 defp check_media_removal(
39 %{host: actor_host} = _actor_info,
40 %{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object
41 )
42 when length(child_attachment) > 0 do
43 media_removal =
44 Pleroma.Config.get([:mrf_simple, :media_removal])
45 |> MRF.subdomains_regex()
46
47 object =
48 if MRF.subdomain_match?(media_removal, actor_host) do
49 child_object = Map.delete(object["object"], "attachment")
50 Map.put(object, "object", child_object)
51 else
52 object
53 end
54
55 {:ok, object}
56 end
57
58 defp check_media_removal(_actor_info, object), do: {:ok, object}
59
60 defp check_media_nsfw(
61 %{host: actor_host} = _actor_info,
62 %{
63 "type" => "Create",
64 "object" => child_object
65 } = object
66 ) do
67 media_nsfw =
68 Pleroma.Config.get([:mrf_simple, :media_nsfw])
69 |> MRF.subdomains_regex()
70
71 object =
72 if MRF.subdomain_match?(media_nsfw, actor_host) do
73 tags = (child_object["tag"] || []) ++ ["nsfw"]
74 child_object = Map.put(child_object, "tag", tags)
75 child_object = Map.put(child_object, "sensitive", true)
76 Map.put(object, "object", child_object)
77 else
78 object
79 end
80
81 {:ok, object}
82 end
83
84 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
85
86 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
87 timeline_removal =
88 Pleroma.Config.get([:mrf_simple, :federated_timeline_removal])
89 |> MRF.subdomains_regex()
90
91 object =
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]
96
97 cc = List.delete(object["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
98
99 object
100 |> Map.put("to", to)
101 |> Map.put("cc", cc)
102 else
103 _ -> object
104 end
105
106 {:ok, object}
107 end
108
109 defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
110 report_removal =
111 Pleroma.Config.get([:mrf_simple, :report_removal])
112 |> MRF.subdomains_regex()
113
114 if MRF.subdomain_match?(report_removal, actor_host) do
115 {:reject, nil}
116 else
117 {:ok, object}
118 end
119 end
120
121 defp check_report_removal(_actor_info, object), do: {:ok, object}
122
123 defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do
124 avatar_removal =
125 Pleroma.Config.get([:mrf_simple, :avatar_removal])
126 |> MRF.subdomains_regex()
127
128 if MRF.subdomain_match?(avatar_removal, actor_host) do
129 {:ok, Map.delete(object, "icon")}
130 else
131 {:ok, object}
132 end
133 end
134
135 defp check_avatar_removal(_actor_info, object), do: {:ok, object}
136
137 defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do
138 banner_removal =
139 Pleroma.Config.get([:mrf_simple, :banner_removal])
140 |> MRF.subdomains_regex()
141
142 if MRF.subdomain_match?(banner_removal, actor_host) do
143 {:ok, Map.delete(object, "image")}
144 else
145 {:ok, object}
146 end
147 end
148
149 defp check_banner_removal(_actor_info, object), do: {:ok, object}
150
151 @impl true
152 def filter(%{"type" => "Delete", "actor" => actor} = object) do
153 %{host: actor_host} = URI.parse(actor)
154
155 reject_deletes =
156 Pleroma.Config.get([:mrf_simple, :reject_deletes])
157 |> MRF.subdomains_regex()
158
159 if MRF.subdomain_match?(reject_deletes, actor_host) do
160 {:reject, nil}
161 else
162 {:ok, object}
163 end
164 end
165
166 @impl true
167 def filter(%{"actor" => actor} = object) do
168 actor_info = URI.parse(actor)
169
170 with {:ok, object} <- check_accept(actor_info, object),
171 {:ok, object} <- check_reject(actor_info, object),
172 {:ok, object} <- check_media_removal(actor_info, object),
173 {:ok, object} <- check_media_nsfw(actor_info, object),
174 {:ok, object} <- check_ftl_removal(actor_info, object),
175 {:ok, object} <- check_report_removal(actor_info, object) do
176 {:ok, object}
177 else
178 _e -> {:reject, nil}
179 end
180 end
181
182 def filter(%{"id" => actor, "type" => obj_type} = object)
183 when obj_type in ["Application", "Group", "Organization", "Person", "Service"] do
184 actor_info = URI.parse(actor)
185
186 with {:ok, object} <- check_accept(actor_info, object),
187 {:ok, object} <- check_reject(actor_info, object),
188 {:ok, object} <- check_avatar_removal(actor_info, object),
189 {:ok, object} <- check_banner_removal(actor_info, object) do
190 {:ok, object}
191 else
192 _e -> {:reject, nil}
193 end
194 end
195
196 def filter(object), do: {:ok, object}
197
198 @impl true
199 def describe do
200 exclusions = Pleroma.Config.get([:instance, :mrf_transparency_exclusions])
201
202 mrf_simple =
203 Pleroma.Config.get(:mrf_simple)
204 |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn v -> v in exclusions end)} end)
205 |> Enum.into(%{})
206
207 {:ok, %{mrf_simple: mrf_simple}}
208 end
209 end