Merge branch 'chores/bump-copyright' into 'develop'
[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(%{"actor" => actor} = object) do
153 actor_info = URI.parse(actor)
154
155 with {:ok, object} <- check_accept(actor_info, object),
156 {:ok, object} <- check_reject(actor_info, object),
157 {:ok, object} <- check_media_removal(actor_info, object),
158 {:ok, object} <- check_media_nsfw(actor_info, object),
159 {:ok, object} <- check_ftl_removal(actor_info, object),
160 {:ok, object} <- check_report_removal(actor_info, object) do
161 {:ok, object}
162 else
163 _e -> {:reject, nil}
164 end
165 end
166
167 def filter(%{"id" => actor, "type" => obj_type} = object)
168 when obj_type in ["Application", "Group", "Organization", "Person", "Service"] do
169 actor_info = URI.parse(actor)
170
171 with {:ok, object} <- check_accept(actor_info, object),
172 {:ok, object} <- check_reject(actor_info, object),
173 {:ok, object} <- check_avatar_removal(actor_info, object),
174 {:ok, object} <- check_banner_removal(actor_info, object) do
175 {:ok, object}
176 else
177 _e -> {:reject, nil}
178 end
179 end
180
181 def filter(object), do: {:ok, object}
182
183 @impl true
184 def describe do
185 exclusions = Pleroma.Config.get([:instance, :mrf_transparency_exclusions])
186
187 mrf_simple =
188 Pleroma.Config.get(:mrf_simple)
189 |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn v -> v in exclusions end)} end)
190 |> Enum.into(%{})
191
192 {:ok, %{mrf_simple: mrf_simple}}
193 end
194 end