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.Object.Containment do
7 This module contains some useful functions for containing objects to specific
8 origins and determining those origins. They previously lived in the
9 ActivityPub `Transmogrifier` module.
11 Object containment is an important step in validating remote objects to prevent
12 spoofing, therefore removal of object containment functions is NOT recommended.
14 def get_actor(%{"actor" => actor}) when is_binary(actor) do
18 def get_actor(%{"actor" => actor}) when is_list(actor) do
19 if is_binary(Enum.at(actor, 0)) do
22 Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
27 def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
31 def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
32 get_actor(%{"actor" => actor})
35 def get_object(%{"object" => id}) when is_binary(id) do
39 def get_object(%{"object" => %{"id" => id}}) when is_binary(id) do
47 defp compare_uris(%URI{host: host} = _id_uri, %URI{host: host} = _other_uri), do: :ok
48 defp compare_uris(_id_uri, _other_uri), do: :error
51 Checks that an imported AP object's actor matches the host it came from.
53 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))
59 compare_uris(actor_uri, id_uri)
62 def contain_origin(id, %{"attributedTo" => actor} = params),
63 do: contain_origin(id, Map.put(params, "actor", actor))
65 def contain_origin(_id, _data), do: :error
67 def contain_origin_from_id(id, %{"id" => other_id} = _params) when is_binary(other_id) do
68 id_uri = URI.parse(id)
69 other_uri = URI.parse(other_id)
71 compare_uris(id_uri, other_uri)
74 # Mastodon pin activities don't have an id, so we check the object field, which will be pinned.
75 def contain_origin_from_id(id, %{"object" => object}) when is_binary(object) do
76 id_uri = URI.parse(id)
77 object_uri = URI.parse(object)
79 compare_uris(id_uri, object_uri)
82 def contain_origin_from_id(_id, _data), do: :error
84 def contain_child(%{"object" => %{"id" => id, "attributedTo" => _} = object}),
85 do: contain_origin(id, object)
87 def contain_child(_), do: :ok