move to 2.5.5
[anni] / docs / configuration / mrf.md
1 # Message Rewrite Facility
2
3 The Message Rewrite Facility (MRF) is a subsystem that is implemented as a series of hooks that allows the administrator to rewrite or discard messages.
4
5 Possible uses include:
6
7 * marking incoming messages with media from a given account or instance as sensitive
8 * rejecting messages from a specific instance
9 * rejecting reports (flags) from a specific instance
10 * removing/unlisting messages from the public timelines
11 * removing media from messages
12 * sending only public messages to a specific instance
13
14 The MRF provides user-configurable policies. The default policy is `NoOpPolicy`, which disables the MRF functionality. Pleroma also includes an easy to use policy called `SimplePolicy` which maps messages matching certain pre-defined criterion to actions built into the policy module.
15
16 It is possible to use multiple, active MRF policies at the same time.
17
18 ## Quarantine Instances
19
20 You have the ability to prevent from private / followers-only messages from federating with specific instances. Which means they will only get the public or unlisted messages from your instance.
21
22 If, for example, you're using `MIX_ENV=prod` aka using production mode, you would open your configuration file located in `config/prod.secret.exs` and edit or add the option under your `:instance` config object. Then you would specify the instance within quotes.
23
24 ```elixir
25 config :pleroma, :instance,
26   [...]
27   quarantined_instances: ["instance.example", "other.example"]
28 ```
29
30 ## Using `SimplePolicy`
31
32 `SimplePolicy` is capable of handling most common admin tasks.
33
34 To use `SimplePolicy`, you must enable it. Do so by adding the following to your `:instance` config object, so that it looks like this:
35
36 ```elixir
37 config :pleroma, :mrf,
38   [...]
39   policies: Pleroma.Web.ActivityPub.MRF.SimplePolicy
40 ```
41
42 Once `SimplePolicy` is enabled, you can configure various groups in the `:mrf_simple` config object. These groups are:
43
44 * `reject`: Servers in this group will have their messages rejected.
45 * `accept`: If not empty, only messages from these instances will be accepted (whitelist federation).
46 * `media_nsfw`: Servers in this group will have the #nsfw tag and sensitive setting injected into incoming messages which contain media.
47 * `media_removal`: Servers in this group will have media stripped from incoming messages.
48 * `avatar_removal`: Avatars from these servers will be stripped from incoming messages.
49 * `banner_removal`: Banner images from these servers will be stripped from incoming messages.
50 * `report_removal`: Servers in this group will have their reports (flags) rejected.
51 * `federated_timeline_removal`: Servers in this group will have their messages unlisted from the public timelines by flipping the `to` and `cc` fields.
52 * `reject_deletes`: Deletion requests will be rejected from these servers.
53
54 Servers should be configured as lists.
55
56 ### Example
57
58 This example will enable `SimplePolicy`, block media from `illegalporn.biz`, mark media as NSFW from `porn.biz` and `porn.business`, reject messages from `spam.com`, remove messages from `spam.university` from the federated timeline and block reports (flags) from `whiny.whiner`. We also give a reason why the moderation was done:
59
60 ```elixir
61 config :pleroma, :mrf,
62   policies: [Pleroma.Web.ActivityPub.MRF.SimplePolicy]
63
64 config :pleroma, :mrf_simple,
65   media_removal: [{"illegalporn.biz", "Media can contain illegal contant"}],
66   media_nsfw: [{"porn.biz", "unmarked nsfw media"}, {"porn.business", "A lot of unmarked nsfw media"}],
67   reject: [{"spam.com", "They keep spamming our users"}],
68   federated_timeline_removal: [{"spam.university", "Annoying low-quality posts who otherwise fill up TWKN"}],
69   report_removal: [{"whiny.whiner", "Keep spamming us with irrelevant reports"}]
70 ```
71
72 ### Use with Care
73
74 The effects of MRF policies can be very drastic. It is important to use this functionality carefully. Always try to talk to an admin before writing an MRF policy concerning their instance.
75
76 ## Writing your own MRF Policy
77
78 As discussed above, the MRF system is a modular system that supports pluggable policies. This means that an admin may write a custom MRF policy in Elixir or any other language that runs on the Erlang VM, by specifying the module name in the `policies` config setting.
79
80 For example, here is a sample policy module which rewrites all messages to "new message content":
81
82 ```elixir
83 defmodule Pleroma.Web.ActivityPub.MRF.RewritePolicy do
84   @moduledoc "MRF policy which rewrites all Notes to have 'new message content'."
85   @behaviour Pleroma.Web.ActivityPub.MRF.Policy
86
87   # Catch messages which contain Note objects with actual data to filter.
88   # Capture the object as `object`, the message content as `content` and the
89   # message itself as `message`.
90   @impl true
91   def filter(
92         %{"type" => "Create", "object" => %{"type" => "Note", "content" => content} = object} =
93           message
94       )
95       when is_binary(content) do
96     # Subject / CW is stored as summary instead of `name` like other AS2 objects
97     # because of Mastodon doing it that way.
98     summary = object["summary"]
99
100     # Message edits go here.
101     content = "new message content"
102
103     # Assemble the mutated object.
104     object =
105       object
106       |> Map.put("content", content)
107       |> Map.put("summary", summary)
108
109     # Assemble the mutated message.
110     message = Map.put(message, "object", object)
111     {:ok, message}
112   end
113
114   # Let all other messages through without modifying them.
115   @impl true
116   def filter(message), do: {:ok, message}
117
118   @impl true
119   def describe do
120     {:ok, %{mrf_sample: %{content: "new message content"}}}
121   end
122 end
123 ```
124
125 If you save this file as `lib/pleroma/web/activity_pub/mrf/rewrite_policy.ex`, it will be included when you next rebuild Pleroma.  You can enable it in the configuration like so:
126
127 ```elixir
128 config :pleroma, :mrf,
129   policies: [
130     Pleroma.Web.ActivityPub.MRF.SimplePolicy,
131     Pleroma.Web.ActivityPub.MRF.RewritePolicy
132   ]
133 ```
134
135 Please note that the Pleroma developers consider custom MRF policy modules to fall under the purview of the AGPL. As such, you are obligated to release the sources to your custom MRF policy modules upon request.
136
137 ### MRF policies descriptions
138
139 If MRF policy depends on config, it can be added into MRF tab to adminFE by adding `config_description/0` method, which returns a map with a specific structure. See existing MRF's like `lib/pleroma/web/activity_pub/mrf/activity_expiration_policy.ex` for examples. Note that more complex inputs, like tuples or maps, may need extra changes in the adminFE and just adding it to `config_description/0` may not be enough to get these inputs working from the adminFE.
140
141 Example:
142
143 ```elixir
144 %{
145       key: :mrf_activity_expiration,
146       related_policy: "Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy",
147       label: "MRF Activity Expiration Policy",
148       description: "Adds automatic expiration to all local activities",
149       children: [
150         %{
151           key: :days,
152           type: :integer,
153           description: "Default global expiration time for all local activities (in days)",
154           suggestions: [90, 365]
155         }
156       ]
157     }
158 ```