3d1f26e60343c63f09e06c723350b5d67f894c35
[akkoma] / test / web / activity_pub / mrf / simple_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8 alias Pleroma.Config
9 alias Pleroma.Web.ActivityPub.MRF.SimplePolicy
10
11 setup do
12 orig = Config.get!(:mrf_simple)
13
14 Config.put(:mrf_simple,
15 media_removal: [],
16 media_nsfw: [],
17 federated_timeline_removal: [],
18 report_removal: [],
19 reject: [],
20 accept: [],
21 avatar_removal: [],
22 banner_removal: []
23 )
24
25 on_exit(fn ->
26 Config.put(:mrf_simple, orig)
27 end)
28 end
29
30 describe "when :media_removal" do
31 test "is empty" do
32 Config.put([:mrf_simple, :media_removal], [])
33 media_message = build_media_message()
34 local_message = build_local_message()
35
36 assert SimplePolicy.filter(media_message) == {:ok, media_message}
37 assert SimplePolicy.filter(local_message) == {:ok, local_message}
38 end
39
40 test "has a matching host" do
41 Config.put([:mrf_simple, :media_removal], ["remote.instance"])
42 media_message = build_media_message()
43 local_message = build_local_message()
44
45 assert SimplePolicy.filter(media_message) ==
46 {:ok,
47 media_message
48 |> Map.put("object", Map.delete(media_message["object"], "attachment"))}
49
50 assert SimplePolicy.filter(local_message) == {:ok, local_message}
51 end
52 end
53
54 describe "when :media_nsfw" do
55 test "is empty" do
56 Config.put([:mrf_simple, :media_nsfw], [])
57 media_message = build_media_message()
58 local_message = build_local_message()
59
60 assert SimplePolicy.filter(media_message) == {:ok, media_message}
61 assert SimplePolicy.filter(local_message) == {:ok, local_message}
62 end
63
64 test "has a matching host" do
65 Config.put([:mrf_simple, :media_nsfw], ["remote.instance"])
66 media_message = build_media_message()
67 local_message = build_local_message()
68
69 assert SimplePolicy.filter(media_message) ==
70 {:ok,
71 media_message
72 |> put_in(["object", "tag"], ["foo", "nsfw"])
73 |> put_in(["object", "sensitive"], true)}
74
75 assert SimplePolicy.filter(local_message) == {:ok, local_message}
76 end
77 end
78
79 defp build_media_message do
80 %{
81 "actor" => "https://remote.instance/users/bob",
82 "type" => "Create",
83 "object" => %{
84 "attachment" => [%{}],
85 "tag" => ["foo"],
86 "sensitive" => false
87 }
88 }
89 end
90
91 describe "when :report_removal" do
92 test "is empty" do
93 Config.put([:mrf_simple, :report_removal], [])
94 report_message = build_report_message()
95 local_message = build_local_message()
96
97 assert SimplePolicy.filter(report_message) == {:ok, report_message}
98 assert SimplePolicy.filter(local_message) == {:ok, local_message}
99 end
100
101 test "has a matching host" do
102 Config.put([:mrf_simple, :report_removal], ["remote.instance"])
103 report_message = build_report_message()
104 local_message = build_local_message()
105
106 assert SimplePolicy.filter(report_message) == {:reject, nil}
107 assert SimplePolicy.filter(local_message) == {:ok, local_message}
108 end
109 end
110
111 defp build_report_message do
112 %{
113 "actor" => "https://remote.instance/users/bob",
114 "type" => "Flag"
115 }
116 end
117
118 describe "when :federated_timeline_removal" do
119 test "is empty" do
120 Config.put([:mrf_simple, :federated_timeline_removal], [])
121 {_, ftl_message} = build_ftl_actor_and_message()
122 local_message = build_local_message()
123
124 assert SimplePolicy.filter(ftl_message) == {:ok, ftl_message}
125 assert SimplePolicy.filter(local_message) == {:ok, local_message}
126 end
127
128 test "has a matching host" do
129 {actor, ftl_message} = build_ftl_actor_and_message()
130
131 ftl_message_actor_host =
132 ftl_message
133 |> Map.fetch!("actor")
134 |> URI.parse()
135 |> Map.fetch!(:host)
136
137 Config.put([:mrf_simple, :federated_timeline_removal], [ftl_message_actor_host])
138 local_message = build_local_message()
139
140 assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message)
141 assert actor.follower_address in ftl_message["to"]
142 refute actor.follower_address in ftl_message["cc"]
143 refute "https://www.w3.org/ns/activitystreams#Public" in ftl_message["to"]
144 assert "https://www.w3.org/ns/activitystreams#Public" in ftl_message["cc"]
145
146 assert SimplePolicy.filter(local_message) == {:ok, local_message}
147 end
148 end
149
150 defp build_ftl_actor_and_message do
151 actor = insert(:user)
152
153 {actor,
154 %{
155 "actor" => actor.ap_id,
156 "to" => ["https://www.w3.org/ns/activitystreams#Public", "http://foo.bar/baz"],
157 "cc" => [actor.follower_address, "http://foo.bar/qux"]
158 }}
159 end
160
161 describe "when :reject" do
162 test "is empty" do
163 Config.put([:mrf_simple, :reject], [])
164
165 remote_message = build_remote_message()
166
167 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
168 end
169
170 test "has a matching host" do
171 Config.put([:mrf_simple, :reject], ["remote.instance"])
172
173 remote_message = build_remote_message()
174
175 assert SimplePolicy.filter(remote_message) == {:reject, nil}
176 end
177 end
178
179 describe "when :accept" do
180 test "is empty" do
181 Config.put([:mrf_simple, :accept], [])
182
183 local_message = build_local_message()
184 remote_message = build_remote_message()
185
186 assert SimplePolicy.filter(local_message) == {:ok, local_message}
187 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
188 end
189
190 test "is not empty but it doesn't have a matching host" do
191 Config.put([:mrf_simple, :accept], ["non.matching.remote"])
192
193 local_message = build_local_message()
194 remote_message = build_remote_message()
195
196 assert SimplePolicy.filter(local_message) == {:ok, local_message}
197 assert SimplePolicy.filter(remote_message) == {:reject, nil}
198 end
199
200 test "has a matching host" do
201 Config.put([:mrf_simple, :accept], ["remote.instance"])
202
203 local_message = build_local_message()
204 remote_message = build_remote_message()
205
206 assert SimplePolicy.filter(local_message) == {:ok, local_message}
207 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
208 end
209 end
210
211 describe "when :avatar_removal" do
212 test "is empty" do
213 Config.put([:mrf_simple, :avatar_removal], [])
214
215 remote_user = build_remote_user()
216
217 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
218 end
219
220 test "is not empty but it doesn't have a matching host" do
221 Config.put([:mrf_simple, :avatar_removal], ["non.matching.remote"])
222
223 remote_user = build_remote_user()
224
225 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
226 end
227
228 test "has a matching host" do
229 Config.put([:mrf_simple, :avatar_removal], ["remote.instance"])
230
231 remote_user = build_remote_user()
232 {:ok, filtered} = SimplePolicy.filter(remote_user)
233
234 refute filtered["icon"]
235 end
236 end
237
238 describe "when :banner_removal" do
239 test "is empty" do
240 Config.put([:mrf_simple, :banner_removal], [])
241
242 remote_user = build_remote_user()
243
244 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
245 end
246
247 test "is not empty but it doesn't have a matching host" do
248 Config.put([:mrf_simple, :banner_removal], ["non.matching.remote"])
249
250 remote_user = build_remote_user()
251
252 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
253 end
254
255 test "has a matching host" do
256 Config.put([:mrf_simple, :banner_removal], ["remote.instance"])
257
258 remote_user = build_remote_user()
259 {:ok, filtered} = SimplePolicy.filter(remote_user)
260
261 refute filtered["image"]
262 end
263 end
264
265 defp build_local_message do
266 %{
267 "actor" => "#{Pleroma.Web.base_url()}/users/alice",
268 "to" => [],
269 "cc" => []
270 }
271 end
272
273 defp build_remote_message do
274 %{"actor" => "https://remote.instance/users/bob"}
275 end
276
277 defp build_remote_user do
278 %{
279 "id" => "https://remote.instance/users/bob",
280 "icon" => %{
281 "url" => "http://example.com/image.jpg",
282 "type" => "Image"
283 },
284 "image" => %{
285 "url" => "http://example.com/image.jpg",
286 "type" => "Image"
287 },
288 "type" => "Person"
289 }
290 end
291 end