logo

pleroma

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

mrf.ex (7018B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.MRF do
  5. require Logger
  6. @behaviour Pleroma.Web.ActivityPub.MRF.PipelineFiltering
  7. @mrf_config_descriptions [
  8. %{
  9. group: :pleroma,
  10. key: :mrf,
  11. tab: :mrf,
  12. label: "MRF",
  13. type: :group,
  14. description: "General MRF settings",
  15. children: [
  16. %{
  17. key: :policies,
  18. type: [:module, {:list, :module}],
  19. description:
  20. "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.",
  21. suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF.Policy}
  22. },
  23. %{
  24. key: :transparency,
  25. label: "MRF transparency",
  26. type: :boolean,
  27. description:
  28. "Make the content of your Message Rewrite Facility settings public (via nodeinfo)"
  29. },
  30. %{
  31. key: :transparency_exclusions,
  32. label: "MRF transparency exclusions",
  33. type: {:list, :tuple},
  34. key_placeholder: "instance",
  35. value_placeholder: "reason",
  36. description:
  37. "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.",
  38. suggestions: [
  39. "exclusion.com"
  40. ]
  41. }
  42. ]
  43. }
  44. ]
  45. @default_description %{
  46. label: "",
  47. description: ""
  48. }
  49. @required_description_keys [:key, :related_policy]
  50. def filter_one(policy, message) do
  51. Code.ensure_loaded(policy)
  52. should_plug_history? =
  53. if function_exported?(policy, :history_awareness, 0) do
  54. policy.history_awareness()
  55. else
  56. :manual
  57. end
  58. |> Kernel.==(:auto)
  59. if not should_plug_history? do
  60. policy.filter(message)
  61. else
  62. main_result = policy.filter(message)
  63. with {_, {:ok, main_message}} <- {:main, main_result},
  64. {_,
  65. %{
  66. "formerRepresentations" => %{
  67. "orderedItems" => [_ | _]
  68. }
  69. }} = {_, object} <- {:object, message["object"]},
  70. {_, {:ok, new_history}} <-
  71. {:history,
  72. Pleroma.Object.Updater.for_each_history_item(
  73. object["formerRepresentations"],
  74. object,
  75. fn item ->
  76. with {:ok, filtered} <- policy.filter(Map.put(message, "object", item)) do
  77. {:ok, filtered["object"]}
  78. else
  79. e -> e
  80. end
  81. end
  82. )} do
  83. {:ok, put_in(main_message, ["object", "formerRepresentations"], new_history)}
  84. else
  85. {:main, _} -> main_result
  86. {:object, _} -> main_result
  87. {:history, e} -> e
  88. end
  89. end
  90. end
  91. def filter(policies, %{} = message) do
  92. policies
  93. |> Enum.reduce({:ok, message}, fn
  94. policy, {:ok, message} -> filter_one(policy, message)
  95. _, error -> error
  96. end)
  97. end
  98. def filter(%{} = object), do: get_policies() |> filter(object)
  99. def id_filter(policies, id) when is_binary(id) do
  100. policies
  101. |> Enum.filter(&function_exported?(&1, :id_filter, 1))
  102. |> Enum.all?(& &1.id_filter(id))
  103. end
  104. def id_filter(id) when is_binary(id), do: get_policies() |> id_filter(id)
  105. @impl true
  106. def pipeline_filter(%{} = message, meta) do
  107. object = meta[:object_data]
  108. ap_id = message["object"]
  109. if object && ap_id do
  110. with {:ok, message} <- filter(Map.put(message, "object", object)) do
  111. meta = Keyword.put(meta, :object_data, message["object"])
  112. {:ok, Map.put(message, "object", ap_id), meta}
  113. else
  114. {err, message} -> {err, message, meta}
  115. end
  116. else
  117. {err, message} = filter(message)
  118. {err, message, meta}
  119. end
  120. end
  121. def get_policies do
  122. Pleroma.Config.get([:mrf, :policies], [])
  123. |> get_policies()
  124. |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
  125. end
  126. defp get_policies(policy) when is_atom(policy), do: [policy]
  127. defp get_policies(policies) when is_list(policies), do: policies
  128. defp get_policies(_), do: []
  129. @spec subdomains_regex([String.t()]) :: [Regex.t()]
  130. def subdomains_regex(domains) when is_list(domains) do
  131. for domain <- domains do
  132. try do
  133. target = String.replace(domain, "*.", "(.*\\.)*")
  134. ~r<^#{target}$>i
  135. rescue
  136. e ->
  137. Logger.error("MRF: Invalid subdomain Regex: #{domain}")
  138. reraise e, __STACKTRACE__
  139. end
  140. end
  141. end
  142. @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
  143. def subdomain_match?(domains, host) do
  144. Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
  145. end
  146. @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
  147. def instance_list_from_tuples(list) do
  148. Enum.map(list, fn {instance, _} -> instance end)
  149. end
  150. def describe(policies) do
  151. {:ok, policy_configs} =
  152. policies
  153. |> Enum.reduce({:ok, %{}}, fn
  154. policy, {:ok, data} ->
  155. {:ok, policy_data} = policy.describe()
  156. {:ok, Map.merge(data, policy_data)}
  157. _, error ->
  158. error
  159. end)
  160. mrf_policies =
  161. get_policies()
  162. |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
  163. exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
  164. base =
  165. %{
  166. mrf_policies: mrf_policies,
  167. exclusions: length(exclusions) > 0
  168. }
  169. |> Map.merge(policy_configs)
  170. {:ok, base}
  171. end
  172. def describe, do: get_policies() |> describe()
  173. def config_descriptions do
  174. Pleroma.Web.ActivityPub.MRF.Policy
  175. |> Pleroma.Docs.Generator.list_behaviour_implementations()
  176. |> config_descriptions()
  177. end
  178. def config_descriptions(policies) do
  179. Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
  180. Code.ensure_loaded(policy)
  181. if function_exported?(policy, :config_description, 0) do
  182. description =
  183. @default_description
  184. |> Map.merge(policy.config_description)
  185. |> Map.put(:group, :pleroma)
  186. |> Map.put(:tab, :mrf)
  187. |> Map.put(:type, :group)
  188. if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
  189. [description | acc]
  190. else
  191. Logger.warning(
  192. "#{policy} config description doesn't have one or all required keys #{inspect(@required_description_keys)}"
  193. )
  194. acc
  195. end
  196. else
  197. Logger.debug(
  198. "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."
  199. )
  200. acc
  201. end
  202. end)
  203. end
  204. end