logo

pleroma

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

containment.ex (2658B)


  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 that an imported AP object's actor matches the host it came from.
  42. """
  43. def contain_origin(_id, %{"actor" => nil}), do: :error
  44. def contain_origin(id, %{"actor" => _actor} = params) do
  45. id_uri = URI.parse(id)
  46. actor_uri = URI.parse(get_actor(params))
  47. compare_uris(actor_uri, id_uri)
  48. end
  49. def contain_origin(id, %{"attributedTo" => actor} = params),
  50. do: contain_origin(id, Map.put(params, "actor", actor))
  51. def contain_origin(_id, _data), do: :error
  52. def contain_origin_from_id(id, %{"id" => other_id} = _params) when is_binary(other_id) do
  53. id_uri = URI.parse(id)
  54. other_uri = URI.parse(other_id)
  55. compare_uris(id_uri, other_uri)
  56. end
  57. # Mastodon pin activities don't have an id, so we check the object field, which will be pinned.
  58. def contain_origin_from_id(id, %{"object" => object}) when is_binary(object) do
  59. id_uri = URI.parse(id)
  60. object_uri = URI.parse(object)
  61. compare_uris(id_uri, object_uri)
  62. end
  63. def contain_origin_from_id(_id, _data), do: :error
  64. def contain_child(%{"object" => %{"id" => id, "attributedTo" => _} = object}),
  65. do: contain_origin(id, object)
  66. def contain_child(_), do: :ok
  67. end