1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF do
8 @behaviour Pleroma.Web.ActivityPub.MRF.PipelineFiltering
10 @mrf_config_descriptions [
17 description: "General MRF settings",
21 type: [:module, {:list, :module}],
23 "A list of MRF policies enabled. Module names are shortened (removed leading `Pleroma.Web.ActivityPub.MRF.` part), but on adding custom module you need to use full name.",
24 suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF.Policy}
28 label: "MRF transparency",
31 "Make the content of your Message Rewrite Facility settings public (via nodeinfo)"
34 key: :transparency_exclusions,
35 label: "MRF transparency exclusions",
36 type: {:list, :tuple},
37 key_placeholder: "instance",
38 value_placeholder: "reason",
40 "Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value. You can also provide a reason for excluding these instance names. The instances and reasons won't be publicly disclosed.",
46 key: :transparency_obfuscate_domains,
47 label: "MRF domain obfuscation",
48 type: {:list, :string},
50 "Obfuscate domains in MRF transparency. This is useful if the domain you're blocking contains words you don't want displayed, but still want to disclose the MRF settings.",
59 @default_description %{
64 @required_description_keys [:key, :related_policy]
66 def filter(policies, %{} = message) do
68 |> Enum.reduce({:ok, message}, fn
69 policy, {:ok, message} -> policy.filter(message)
74 def filter(%{} = object), do: get_policies() |> filter(object)
77 def pipeline_filter(%{} = message, meta) do
78 object = meta[:object_data]
79 ap_id = message["object"]
82 with {:ok, message} <- filter(Map.put(message, "object", object)) do
83 meta = Keyword.put(meta, :object_data, message["object"])
84 {:ok, Map.put(message, "object", ap_id), meta}
86 {err, message} -> {err, message, meta}
89 {err, message} = filter(message)
96 Pleroma.Config.get([:mrf, :policies], [])
98 |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
101 defp get_policies(policy) when is_atom(policy), do: [policy]
102 defp get_policies(policies) when is_list(policies), do: policies
103 defp get_policies(_), do: []
105 @spec subdomains_regex([String.t()]) :: [Regex.t()]
106 def subdomains_regex(domains) when is_list(domains) do
107 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
110 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
111 def subdomain_match?(domains, host) do
112 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
115 @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
116 def instance_list_from_tuples(list) do
117 Enum.map(list, fn {instance, _} -> instance end)
120 def describe(policies) do
121 {:ok, policy_configs} =
123 |> Enum.reduce({:ok, %{}}, fn
124 policy, {:ok, data} ->
125 {:ok, policy_data} = policy.describe()
126 {:ok, Map.merge(data, policy_data)}
134 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
136 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
140 mrf_policies: mrf_policies,
141 exclusions: length(exclusions) > 0
143 |> Map.merge(policy_configs)
148 def describe, do: get_policies() |> describe()
150 def config_descriptions do
151 Pleroma.Web.ActivityPub.MRF.Policy
152 |> Pleroma.Docs.Generator.list_behaviour_implementations()
153 |> config_descriptions()
156 def config_descriptions(policies) do
157 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
158 if function_exported?(policy, :config_description, 0) do
161 |> Map.merge(policy.config_description)
162 |> Map.put(:group, :pleroma)
163 |> Map.put(:tab, :mrf)
164 |> Map.put(:type, :group)
166 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
170 "#{policy} config description doesn't have one or all required keys #{inspect(@required_description_keys)}"
177 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."