logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

mrf.md (7151B)


  1. # Message Rewrite Facility
  2. 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.
  3. Possible uses include:
  4. * marking incoming messages with media from a given account or instance as sensitive
  5. * rejecting messages from a specific instance
  6. * rejecting reports (flags) from a specific instance
  7. * removing/unlisting messages from the public timelines
  8. * removing media from messages
  9. * sending only public messages to a specific instance
  10. 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.
  11. It is possible to use multiple, active MRF policies at the same time.
  12. ## Quarantine Instances
  13. 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.
  14. 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.
  15. ```elixir
  16. config :pleroma, :instance,
  17. [...]
  18. quarantined_instances: ["instance.example", "other.example"]
  19. ```
  20. ## Using `SimplePolicy`
  21. `SimplePolicy` is capable of handling most common admin tasks.
  22. To use `SimplePolicy`, you must enable it. Do so by adding the following to your `:instance` config object, so that it looks like this:
  23. ```elixir
  24. config :pleroma, :mrf,
  25. [...]
  26. policies: Pleroma.Web.ActivityPub.MRF.SimplePolicy
  27. ```
  28. Once `SimplePolicy` is enabled, you can configure various groups in the `:mrf_simple` config object. These groups are:
  29. * `reject`: Servers in this group will have their messages rejected.
  30. * `accept`: If not empty, only messages from these instances will be accepted (whitelist federation).
  31. * `media_nsfw`: Servers in this group will have the #nsfw tag and sensitive setting injected into incoming messages which contain media.
  32. * `media_removal`: Servers in this group will have media stripped from incoming messages.
  33. * `avatar_removal`: Avatars from these servers will be stripped from incoming messages.
  34. * `banner_removal`: Banner images from these servers will be stripped from incoming messages.
  35. * `report_removal`: Servers in this group will have their reports (flags) rejected.
  36. * `federated_timeline_removal`: Servers in this group will have their messages unlisted from the public timelines by flipping the `to` and `cc` fields.
  37. * `reject_deletes`: Deletion requests will be rejected from these servers.
  38. Servers should be configured as lists.
  39. ### Example
  40. 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:
  41. ```elixir
  42. config :pleroma, :mrf,
  43. policies: [Pleroma.Web.ActivityPub.MRF.SimplePolicy]
  44. config :pleroma, :mrf_simple,
  45. media_removal: [{"illegalporn.biz", "Media can contain illegal contant"}],
  46. media_nsfw: [{"porn.biz", "unmarked nsfw media"}, {"porn.business", "A lot of unmarked nsfw media"}],
  47. reject: [{"spam.com", "They keep spamming our users"}],
  48. federated_timeline_removal: [{"spam.university", "Annoying low-quality posts who otherwise fill up TWKN"}],
  49. report_removal: [{"whiny.whiner", "Keep spamming us with irrelevant reports"}]
  50. ```
  51. ### Use with Care
  52. 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.
  53. ## Writing your own MRF Policy
  54. 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.
  55. For example, here is a sample policy module which rewrites all messages to "new message content":
  56. ```elixir
  57. defmodule Pleroma.Web.ActivityPub.MRF.RewritePolicy do
  58. @moduledoc "MRF policy which rewrites all Notes to have 'new message content'."
  59. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  60. # Catch messages which contain Note objects with actual data to filter.
  61. # Capture the object as `object`, the message content as `content` and the
  62. # message itself as `message`.
  63. @impl true
  64. def filter(
  65. %{"type" => "Create", "object" => %{"type" => "Note", "content" => content} = object} =
  66. message
  67. )
  68. when is_binary(content) do
  69. # Subject / CW is stored as summary instead of `name` like other AS2 objects
  70. # because of Mastodon doing it that way.
  71. summary = object["summary"]
  72. # Message edits go here.
  73. content = "new message content"
  74. # Assemble the mutated object.
  75. object =
  76. object
  77. |> Map.put("content", content)
  78. |> Map.put("summary", summary)
  79. # Assemble the mutated message.
  80. message = Map.put(message, "object", object)
  81. {:ok, message}
  82. end
  83. # Let all other messages through without modifying them.
  84. @impl true
  85. def filter(message), do: {:ok, message}
  86. @impl true
  87. def describe do
  88. {:ok, %{mrf_sample: %{content: "new message content"}}}
  89. end
  90. end
  91. ```
  92. 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:
  93. ```elixir
  94. config :pleroma, :mrf,
  95. policies: [
  96. Pleroma.Web.ActivityPub.MRF.SimplePolicy,
  97. Pleroma.Web.ActivityPub.MRF.RewritePolicy
  98. ]
  99. ```
  100. 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.
  101. ### MRF policies descriptions
  102. 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.
  103. Example:
  104. ```elixir
  105. %{
  106. key: :mrf_activity_expiration,
  107. related_policy: "Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy",
  108. label: "MRF Activity Expiration Policy",
  109. description: "Adds automatic expiration to all local activities",
  110. children: [
  111. %{
  112. key: :days,
  113. type: :integer,
  114. description: "Default global expiration time for all local activities (in days)",
  115. suggestions: [90, 365]
  116. }
  117. ]
  118. }
  119. ```