Merge branch 'develop' into feature/digest-email
[akkoma] / lib / pleroma / web / activity_pub / mrf / simple_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 MRF
10
11 defp check_accept(%{host: actor_host} = _actor_info, object) do
12 accepts =
13 Pleroma.Config.get([:mrf_simple, :accept])
14 |> MRF.subdomains_regex()
15
16 cond do
17 accepts == [] -> {:ok, object}
18 actor_host == Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host]) -> {:ok, object}
19 MRF.subdomain_match?(accepts, actor_host) -> {:ok, object}
20 true -> {:reject, nil}
21 end
22 end
23
24 defp check_reject(%{host: actor_host} = _actor_info, object) do
25 rejects =
26 Pleroma.Config.get([:mrf_simple, :reject])
27 |> MRF.subdomains_regex()
28
29 if MRF.subdomain_match?(rejects, actor_host) do
30 {:reject, nil}
31 else
32 {:ok, object}
33 end
34 end
35
36 defp check_media_removal(
37 %{host: actor_host} = _actor_info,
38 %{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object
39 )
40 when length(child_attachment) > 0 do
41 media_removal =
42 Pleroma.Config.get([:mrf_simple, :media_removal])
43 |> MRF.subdomains_regex()
44
45 object =
46 if MRF.subdomain_match?(media_removal, actor_host) do
47 child_object = Map.delete(object["object"], "attachment")
48 Map.put(object, "object", child_object)
49 else
50 object
51 end
52
53 {:ok, object}
54 end
55
56 defp check_media_removal(_actor_info, object), do: {:ok, object}
57
58 defp check_media_nsfw(
59 %{host: actor_host} = _actor_info,
60 %{
61 "type" => "Create",
62 "object" => child_object
63 } = object
64 ) do
65 media_nsfw =
66 Pleroma.Config.get([:mrf_simple, :media_nsfw])
67 |> MRF.subdomains_regex()
68
69 object =
70 if MRF.subdomain_match?(media_nsfw, actor_host) do
71 tags = (child_object["tag"] || []) ++ ["nsfw"]
72 child_object = Map.put(child_object, "tag", tags)
73 child_object = Map.put(child_object, "sensitive", true)
74 Map.put(object, "object", child_object)
75 else
76 object
77 end
78
79 {:ok, object}
80 end
81
82 defp check_media_nsfw(_actor_info, object), do: {:ok, object}
83
84 defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
85 timeline_removal =
86 Pleroma.Config.get([:mrf_simple, :federated_timeline_removal])
87 |> MRF.subdomains_regex()
88
89 object =
90 with true <- MRF.subdomain_match?(timeline_removal, actor_host),
91 user <- User.get_cached_by_ap_id(object["actor"]),
92 true <- "https://www.w3.org/ns/activitystreams#Public" in object["to"] do
93 to =
94 List.delete(object["to"], "https://www.w3.org/ns/activitystreams#Public") ++
95 [user.follower_address]
96
97 cc =
98 List.delete(object["cc"], user.follower_address) ++
99 ["https://www.w3.org/ns/activitystreams#Public"]
100
101 object
102 |> Map.put("to", to)
103 |> Map.put("cc", cc)
104 else
105 _ -> object
106 end
107
108 {:ok, object}
109 end
110
111 defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
112 report_removal =
113 Pleroma.Config.get([:mrf_simple, :report_removal])
114 |> MRF.subdomains_regex()
115
116 if MRF.subdomain_match?(report_removal, actor_host) do
117 {:reject, nil}
118 else
119 {:ok, object}
120 end
121 end
122
123 defp check_report_removal(_actor_info, object), do: {:ok, object}
124
125 defp check_avatar_removal(%{host: actor_host} = _actor_info, %{"icon" => _icon} = object) do
126 avatar_removal =
127 Pleroma.Config.get([:mrf_simple, :avatar_removal])
128 |> MRF.subdomains_regex()
129
130 if MRF.subdomain_match?(avatar_removal, actor_host) do
131 {:ok, Map.delete(object, "icon")}
132 else
133 {:ok, object}
134 end
135 end
136
137 defp check_avatar_removal(_actor_info, object), do: {:ok, object}
138
139 defp check_banner_removal(%{host: actor_host} = _actor_info, %{"image" => _image} = object) do
140 banner_removal =
141 Pleroma.Config.get([:mrf_simple, :banner_removal])
142 |> MRF.subdomains_regex()
143
144 if MRF.subdomain_match?(banner_removal, actor_host) do
145 {:ok, Map.delete(object, "image")}
146 else
147 {:ok, object}
148 end
149 end
150
151 defp check_banner_removal(_actor_info, object), do: {:ok, object}
152
153 @impl true
154 def filter(%{"actor" => actor} = object) do
155 actor_info = URI.parse(actor)
156
157 with {:ok, object} <- check_accept(actor_info, object),
158 {:ok, object} <- check_reject(actor_info, object),
159 {:ok, object} <- check_media_removal(actor_info, object),
160 {:ok, object} <- check_media_nsfw(actor_info, object),
161 {:ok, object} <- check_ftl_removal(actor_info, object),
162 {:ok, object} <- check_report_removal(actor_info, object) do
163 {:ok, object}
164 else
165 _e -> {:reject, nil}
166 end
167 end
168
169 def filter(%{"id" => actor, "type" => obj_type} = object)
170 when obj_type in ["Application", "Group", "Organization", "Person", "Service"] do
171 actor_info = URI.parse(actor)
172
173 with {: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 end