logo

pleroma

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

containment.ex (3018B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Object.Containment do
  5. @moduledoc """
  6. This module contains some useful functions for containing objects to specific
  7. origins and determining those origins. They previously lived in the
  8. ActivityPub `Transmogrifier` module.
  9. Object containment is an important step in validating remote objects to prevent
  10. spoofing, therefore removal of object containment functions is NOT recommended.
  11. """
  12. def get_actor(%{"actor" => actor}) when is_binary(actor) do
  13. actor
  14. end
  15. def get_actor(%{"actor" => actor}) when is_list(actor) do
  16. if is_binary(Enum.at(actor, 0)) do
  17. Enum.at(actor, 0)
  18. else
  19. Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
  20. |> Map.get("id")
  21. end
  22. end
  23. def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
  24. id
  25. end
  26. def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
  27. get_actor(%{"actor" => actor})
  28. end
  29. def get_object(%{"object" => id}) when is_binary(id) do
  30. id
  31. end
  32. def get_object(%{"object" => %{"id" => id}}) when is_binary(id) do
  33. id
  34. end
  35. def get_object(_) do
  36. nil
  37. end
  38. defp compare_uris(%URI{host: host} = _id_uri, %URI{host: host} = _other_uri), do: :ok
  39. defp compare_uris(_id_uri, _other_uri), do: :error
  40. @doc """
  41. Checks whether an URL to fetch from is from the local server.
  42. We never want to fetch from ourselves; if it's not in the database
  43. it can't be authentic and must be a counterfeit.
  44. """
  45. def contain_local_fetch(id) do
  46. case compare_uris(URI.parse(id), Pleroma.Web.Endpoint.struct_url()) do
  47. :ok -> :error
  48. _ -> :ok
  49. end
  50. end
  51. @doc """
  52. Checks that an imported AP object's actor matches the host it came from.
  53. """
  54. def contain_origin(_id, %{"actor" => nil}), do: :error
  55. def contain_origin(id, %{"actor" => _actor} = params) do
  56. id_uri = URI.parse(id)
  57. actor_uri = URI.parse(get_actor(params))
  58. compare_uris(actor_uri, id_uri)
  59. end
  60. def contain_origin(id, %{"attributedTo" => actor} = params),
  61. do: contain_origin(id, Map.put(params, "actor", actor))
  62. def contain_origin(_id, _data), do: :error
  63. def contain_origin_from_id(id, %{"id" => other_id} = _params) when is_binary(other_id) do
  64. id_uri = URI.parse(id)
  65. other_uri = URI.parse(other_id)
  66. compare_uris(id_uri, other_uri)
  67. end
  68. # Mastodon pin activities don't have an id, so we check the object field, which will be pinned.
  69. def contain_origin_from_id(id, %{"object" => object}) when is_binary(object) do
  70. id_uri = URI.parse(id)
  71. object_uri = URI.parse(object)
  72. compare_uris(id_uri, object_uri)
  73. end
  74. def contain_origin_from_id(_id, _data), do: :error
  75. def contain_child(%{"object" => %{"id" => id, "attributedTo" => _} = object}),
  76. do: contain_origin(id, object)
  77. def contain_child(_), do: :ok
  78. end