Remove sensitive-property setting #nsfw, create HashtagPolicy
[akkoma] / lib / pleroma / web / activity_pub / mrf / keyword_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
6 require Pleroma.Constants
7
8 @moduledoc "Reject or Word-Replace messages with a keyword or regex"
9
10 @behaviour Pleroma.Web.ActivityPub.MRF
11 defp string_matches?(string, _) when not is_binary(string) do
12 false
13 end
14
15 defp string_matches?(string, pattern) when is_binary(pattern) do
16 String.contains?(string, pattern)
17 end
18
19 defp string_matches?(string, pattern) do
20 String.match?(string, pattern)
21 end
22
23 defp object_payload(%{} = object) do
24 [object["content"], object["summary"], object["name"]]
25 |> Enum.filter(& &1)
26 |> Enum.join("\n")
27 end
28
29 defp check_reject(%{"object" => %{} = object} = message) do
30 payload = object_payload(object)
31
32 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :reject]), fn pattern ->
33 string_matches?(payload, pattern)
34 end) do
35 {:reject, "[KeywordPolicy] Matches with rejected keyword"}
36 else
37 {:ok, message}
38 end
39 end
40
41 defp check_ftl_removal(%{"to" => to, "object" => %{} = object} = message) do
42 payload = object_payload(object)
43
44 if Pleroma.Constants.as_public() in to and
45 Enum.any?(Pleroma.Config.get([:mrf_keyword, :federated_timeline_removal]), fn pattern ->
46 string_matches?(payload, pattern)
47 end) do
48 to = List.delete(to, Pleroma.Constants.as_public())
49 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
50
51 message =
52 message
53 |> Map.put("to", to)
54 |> Map.put("cc", cc)
55
56 {:ok, message}
57 else
58 {:ok, message}
59 end
60 end
61
62 defp check_replace(%{"object" => %{} = object} = message) do
63 object =
64 ["content", "name", "summary"]
65 |> Enum.filter(fn field -> Map.has_key?(object, field) && object[field] end)
66 |> Enum.reduce(object, fn field, object ->
67 data =
68 Enum.reduce(
69 Pleroma.Config.get([:mrf_keyword, :replace]),
70 object[field],
71 fn {pat, repl}, acc -> String.replace(acc, pat, repl) end
72 )
73
74 Map.put(object, field, data)
75 end)
76
77 message = Map.put(message, "object", object)
78
79 {:ok, message}
80 end
81
82 @impl true
83 def filter(%{"type" => "Create", "object" => %{"content" => _content}} = message) do
84 with {:ok, message} <- check_reject(message),
85 {:ok, message} <- check_ftl_removal(message),
86 {:ok, message} <- check_replace(message) do
87 {:ok, message}
88 else
89 {:reject, nil} -> {:reject, "[KeywordPolicy] "}
90 {:reject, _} = e -> e
91 _e -> {:reject, "[KeywordPolicy] "}
92 end
93 end
94
95 @impl true
96 def filter(message), do: {:ok, message}
97
98 @impl true
99 def describe do
100 # This horror is needed to convert regex sigils to strings
101 mrf_keyword =
102 Pleroma.Config.get(:mrf_keyword, [])
103 |> Enum.map(fn {key, value} ->
104 {key,
105 Enum.map(value, fn
106 {pattern, replacement} ->
107 %{
108 "pattern" =>
109 if not is_binary(pattern) do
110 inspect(pattern)
111 else
112 pattern
113 end,
114 "replacement" => replacement
115 }
116
117 pattern ->
118 if not is_binary(pattern) do
119 inspect(pattern)
120 else
121 pattern
122 end
123 end)}
124 end)
125 |> Enum.into(%{})
126
127 {:ok, %{mrf_keyword: mrf_keyword}}
128 end
129
130 @impl true
131 def config_description do
132 %{
133 key: :mrf_keyword,
134 related_policy: "Pleroma.Web.ActivityPub.MRF.KeywordPolicy",
135 label: "MRF Keyword",
136 description:
137 "Reject or Word-Replace messages matching a keyword or [Regex](https://hexdocs.pm/elixir/Regex.html).",
138 children: [
139 %{
140 key: :reject,
141 type: {:list, :string},
142 description: """
143 A list of patterns which result in message being rejected.
144
145 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
146 """,
147 suggestions: ["foo", ~r/foo/iu]
148 },
149 %{
150 key: :federated_timeline_removal,
151 type: {:list, :string},
152 description: """
153 A list of patterns which result in message being removed from federated timelines (a.k.a unlisted).
154
155 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
156 """,
157 suggestions: ["foo", ~r/foo/iu]
158 },
159 %{
160 key: :replace,
161 type: {:list, :tuple},
162 description: """
163 **Pattern**: a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
164
165 **Replacement**: a string. Leaving the field empty is permitted.
166 """
167 }
168 ]
169 }
170 end
171 end