Remove containment tests from transmogrifier and fix thread visibility solver
[akkoma] / lib / pleroma / object / containment.ex
1 defmodule Pleroma.Object.Containment do
2 @moduledoc """
3 # Object Containment
4
5 This module contains some useful functions for containing objects to specific
6 origins and determining those origins. They previously lived in the
7 ActivityPub `Transmogrifier` module.
8
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
16 def get_actor(%{"actor" => actor}) when is_list(actor) do
17 if is_binary(Enum.at(actor, 0)) do
18 Enum.at(actor, 0)
19 else
20 Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
21 |> Map.get("id")
22 end
23 end
24
25 def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
26 id
27 end
28
29 def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
30 get_actor(%{"actor" => actor})
31 end
32
33 @doc """
34 Checks that an imported AP object's actor matches the domain it came from.
35 """
36 def contain_origin(_id, %{"actor" => nil}), do: :error
37
38 def contain_origin(id, %{"actor" => _actor} = params) do
39 id_uri = URI.parse(id)
40 actor_uri = URI.parse(get_actor(params))
41
42 if id_uri.host == actor_uri.host do
43 :ok
44 else
45 :error
46 end
47 end
48
49 def contain_origin_from_id(_id, %{"id" => nil}), do: :error
50
51 def contain_origin_from_id(id, %{"id" => other_id} = _params) do
52 id_uri = URI.parse(id)
53 other_uri = URI.parse(other_id)
54
55 if id_uri.host == other_uri.host do
56 :ok
57 else
58 :error
59 end
60 end
61 end