1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do
9 alias Pleroma.Web.ActivityPub.MRF.SimplePolicy
12 clear_config(:mrf_simple,
15 federated_timeline_removal: [],
23 describe "when :media_removal" do
25 Config.put([:mrf_simple, :media_removal], [])
26 media_message = build_media_message()
27 local_message = build_local_message()
29 assert SimplePolicy.filter(media_message) == {:ok, media_message}
30 assert SimplePolicy.filter(local_message) == {:ok, local_message}
33 test "has a matching host" do
34 Config.put([:mrf_simple, :media_removal], ["remote.instance"])
35 media_message = build_media_message()
36 local_message = build_local_message()
38 assert SimplePolicy.filter(media_message) ==
41 |> Map.put("object", Map.delete(media_message["object"], "attachment"))}
43 assert SimplePolicy.filter(local_message) == {:ok, local_message}
46 test "match with wildcard domain" do
47 Config.put([:mrf_simple, :media_removal], ["*.remote.instance"])
48 media_message = build_media_message()
49 local_message = build_local_message()
51 assert SimplePolicy.filter(media_message) ==
54 |> Map.put("object", Map.delete(media_message["object"], "attachment"))}
56 assert SimplePolicy.filter(local_message) == {:ok, local_message}
60 describe "when :media_nsfw" do
62 Config.put([:mrf_simple, :media_nsfw], [])
63 media_message = build_media_message()
64 local_message = build_local_message()
66 assert SimplePolicy.filter(media_message) == {:ok, media_message}
67 assert SimplePolicy.filter(local_message) == {:ok, local_message}
70 test "has a matching host" do
71 Config.put([:mrf_simple, :media_nsfw], ["remote.instance"])
72 media_message = build_media_message()
73 local_message = build_local_message()
75 assert SimplePolicy.filter(media_message) ==
78 |> put_in(["object", "tag"], ["foo", "nsfw"])
79 |> put_in(["object", "sensitive"], true)}
81 assert SimplePolicy.filter(local_message) == {:ok, local_message}
84 test "match with wildcard domain" do
85 Config.put([:mrf_simple, :media_nsfw], ["*.remote.instance"])
86 media_message = build_media_message()
87 local_message = build_local_message()
89 assert SimplePolicy.filter(media_message) ==
92 |> put_in(["object", "tag"], ["foo", "nsfw"])
93 |> put_in(["object", "sensitive"], true)}
95 assert SimplePolicy.filter(local_message) == {:ok, local_message}
99 defp build_media_message do
101 "actor" => "https://remote.instance/users/bob",
104 "attachment" => [%{}],
111 describe "when :report_removal" do
113 Config.put([:mrf_simple, :report_removal], [])
114 report_message = build_report_message()
115 local_message = build_local_message()
117 assert SimplePolicy.filter(report_message) == {:ok, report_message}
118 assert SimplePolicy.filter(local_message) == {:ok, local_message}
121 test "has a matching host" do
122 Config.put([:mrf_simple, :report_removal], ["remote.instance"])
123 report_message = build_report_message()
124 local_message = build_local_message()
126 assert SimplePolicy.filter(report_message) == {:reject, nil}
127 assert SimplePolicy.filter(local_message) == {:ok, local_message}
130 test "match with wildcard domain" do
131 Config.put([:mrf_simple, :report_removal], ["*.remote.instance"])
132 report_message = build_report_message()
133 local_message = build_local_message()
135 assert SimplePolicy.filter(report_message) == {:reject, nil}
136 assert SimplePolicy.filter(local_message) == {:ok, local_message}
140 defp build_report_message do
142 "actor" => "https://remote.instance/users/bob",
147 describe "when :federated_timeline_removal" do
149 Config.put([:mrf_simple, :federated_timeline_removal], [])
150 {_, ftl_message} = build_ftl_actor_and_message()
151 local_message = build_local_message()
153 assert SimplePolicy.filter(ftl_message) == {:ok, ftl_message}
154 assert SimplePolicy.filter(local_message) == {:ok, local_message}
157 test "has a matching host" do
158 {actor, ftl_message} = build_ftl_actor_and_message()
160 ftl_message_actor_host =
162 |> Map.fetch!("actor")
166 Config.put([:mrf_simple, :federated_timeline_removal], [ftl_message_actor_host])
167 local_message = build_local_message()
169 assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message)
170 assert actor.follower_address in ftl_message["to"]
171 refute actor.follower_address in ftl_message["cc"]
172 refute "https://www.w3.org/ns/activitystreams#Public" in ftl_message["to"]
173 assert "https://www.w3.org/ns/activitystreams#Public" in ftl_message["cc"]
175 assert SimplePolicy.filter(local_message) == {:ok, local_message}
178 test "match with wildcard domain" do
179 {actor, ftl_message} = build_ftl_actor_and_message()
181 ftl_message_actor_host =
183 |> Map.fetch!("actor")
187 Config.put([:mrf_simple, :federated_timeline_removal], ["*." <> ftl_message_actor_host])
188 local_message = build_local_message()
190 assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message)
191 assert actor.follower_address in ftl_message["to"]
192 refute actor.follower_address in ftl_message["cc"]
193 refute "https://www.w3.org/ns/activitystreams#Public" in ftl_message["to"]
194 assert "https://www.w3.org/ns/activitystreams#Public" in ftl_message["cc"]
196 assert SimplePolicy.filter(local_message) == {:ok, local_message}
199 test "has a matching host but only as:Public in to" do
200 {_actor, ftl_message} = build_ftl_actor_and_message()
202 ftl_message_actor_host =
204 |> Map.fetch!("actor")
208 ftl_message = Map.put(ftl_message, "cc", [])
210 Config.put([:mrf_simple, :federated_timeline_removal], [ftl_message_actor_host])
212 assert {:ok, ftl_message} = SimplePolicy.filter(ftl_message)
213 refute "https://www.w3.org/ns/activitystreams#Public" in ftl_message["to"]
214 assert "https://www.w3.org/ns/activitystreams#Public" in ftl_message["cc"]
218 defp build_ftl_actor_and_message do
219 actor = insert(:user)
223 "actor" => actor.ap_id,
224 "to" => ["https://www.w3.org/ns/activitystreams#Public", "http://foo.bar/baz"],
225 "cc" => [actor.follower_address, "http://foo.bar/qux"]
229 describe "when :reject" do
231 Config.put([:mrf_simple, :reject], [])
233 remote_message = build_remote_message()
235 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
238 test "activity has a matching host" do
239 Config.put([:mrf_simple, :reject], ["remote.instance"])
241 remote_message = build_remote_message()
243 assert SimplePolicy.filter(remote_message) == {:reject, nil}
246 test "activity matches with wildcard domain" do
247 Config.put([:mrf_simple, :reject], ["*.remote.instance"])
249 remote_message = build_remote_message()
251 assert SimplePolicy.filter(remote_message) == {:reject, nil}
254 test "actor has a matching host" do
255 Config.put([:mrf_simple, :reject], ["remote.instance"])
257 remote_user = build_remote_user()
259 assert SimplePolicy.filter(remote_user) == {:reject, nil}
263 describe "when :accept" do
265 Config.put([:mrf_simple, :accept], [])
267 local_message = build_local_message()
268 remote_message = build_remote_message()
270 assert SimplePolicy.filter(local_message) == {:ok, local_message}
271 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
274 test "is not empty but activity doesn't have a matching host" do
275 Config.put([:mrf_simple, :accept], ["non.matching.remote"])
277 local_message = build_local_message()
278 remote_message = build_remote_message()
280 assert SimplePolicy.filter(local_message) == {:ok, local_message}
281 assert SimplePolicy.filter(remote_message) == {:reject, nil}
284 test "activity has a matching host" do
285 Config.put([:mrf_simple, :accept], ["remote.instance"])
287 local_message = build_local_message()
288 remote_message = build_remote_message()
290 assert SimplePolicy.filter(local_message) == {:ok, local_message}
291 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
294 test "activity matches with wildcard domain" do
295 Config.put([:mrf_simple, :accept], ["*.remote.instance"])
297 local_message = build_local_message()
298 remote_message = build_remote_message()
300 assert SimplePolicy.filter(local_message) == {:ok, local_message}
301 assert SimplePolicy.filter(remote_message) == {:ok, remote_message}
304 test "actor has a matching host" do
305 Config.put([:mrf_simple, :accept], ["remote.instance"])
307 remote_user = build_remote_user()
309 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
313 describe "when :avatar_removal" do
315 Config.put([:mrf_simple, :avatar_removal], [])
317 remote_user = build_remote_user()
319 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
322 test "is not empty but it doesn't have a matching host" do
323 Config.put([:mrf_simple, :avatar_removal], ["non.matching.remote"])
325 remote_user = build_remote_user()
327 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
330 test "has a matching host" do
331 Config.put([:mrf_simple, :avatar_removal], ["remote.instance"])
333 remote_user = build_remote_user()
334 {:ok, filtered} = SimplePolicy.filter(remote_user)
336 refute filtered["icon"]
339 test "match with wildcard domain" do
340 Config.put([:mrf_simple, :avatar_removal], ["*.remote.instance"])
342 remote_user = build_remote_user()
343 {:ok, filtered} = SimplePolicy.filter(remote_user)
345 refute filtered["icon"]
349 describe "when :banner_removal" do
351 Config.put([:mrf_simple, :banner_removal], [])
353 remote_user = build_remote_user()
355 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
358 test "is not empty but it doesn't have a matching host" do
359 Config.put([:mrf_simple, :banner_removal], ["non.matching.remote"])
361 remote_user = build_remote_user()
363 assert SimplePolicy.filter(remote_user) == {:ok, remote_user}
366 test "has a matching host" do
367 Config.put([:mrf_simple, :banner_removal], ["remote.instance"])
369 remote_user = build_remote_user()
370 {:ok, filtered} = SimplePolicy.filter(remote_user)
372 refute filtered["image"]
375 test "match with wildcard domain" do
376 Config.put([:mrf_simple, :banner_removal], ["*.remote.instance"])
378 remote_user = build_remote_user()
379 {:ok, filtered} = SimplePolicy.filter(remote_user)
381 refute filtered["image"]
385 defp build_local_message do
387 "actor" => "#{Pleroma.Web.base_url()}/users/alice",
393 defp build_remote_message do
394 %{"actor" => "https://remote.instance/users/bob"}
397 defp build_remote_user do
399 "id" => "https://remote.instance/users/bob",
401 "url" => "http://example.com/image.jpg",
405 "url" => "http://example.com/image.jpg",