recipients fixes/hardening for CreateGenericValidator
[akkoma] / lib / pleroma / web / activity_pub / object_validators / create_generic_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 # Code based on CreateChatMessageValidator
6 # NOTES
7 # - doesn't embed, will only get the object id
8 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do
9 use Ecto.Schema
10
11 alias Pleroma.EctoType.ActivityPub.ObjectValidators
12 alias Pleroma.Object
13 alias Pleroma.User
14 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
15 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
16 alias Pleroma.Web.ActivityPub.Transmogrifier
17
18 import Ecto.Changeset
19
20 @primary_key false
21
22 embedded_schema do
23 field(:id, ObjectValidators.ObjectID, primary_key: true)
24 field(:actor, ObjectValidators.ObjectID)
25 field(:type, :string)
26 field(:to, ObjectValidators.Recipients, default: [])
27 field(:cc, ObjectValidators.Recipients, default: [])
28 field(:bto, ObjectValidators.Recipients, default: [])
29 field(:bcc, ObjectValidators.Recipients, default: [])
30 field(:object, ObjectValidators.ObjectID)
31 field(:expires_at, ObjectValidators.DateTime)
32
33 # Should be moved to object, done for CommonAPI.Utils.make_context
34 field(:context, :string)
35 end
36
37 def cast_data(data, meta \\ []) do
38 data = fix(data, meta)
39
40 %__MODULE__{}
41 |> changeset(data)
42 end
43
44 def cast_and_apply(data) do
45 data
46 |> cast_data
47 |> apply_action(:insert)
48 end
49
50 def cast_and_validate(data, meta \\ []) do
51 data
52 |> cast_data(meta)
53 |> validate_data(meta)
54 end
55
56 def changeset(struct, data) do
57 struct
58 |> cast(data, __schema__(:fields))
59 end
60
61 # CommonFixes.fix_activity_addressing adapted for Create specific behavior
62 defp fix_addressing(data, object) do
63 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["actor"])
64
65 data
66 |> CommonFixes.cast_recipients("to", object["to"])
67 |> CommonFixes.cast_recipients("cc", object["cc"])
68 |> CommonFixes.cast_recipients("bto", object["bto"])
69 |> CommonFixes.cast_recipients("bcc", object["bcc"])
70 |> Transmogrifier.fix_explicit_addressing(follower_collection)
71 |> Transmogrifier.fix_implicit_addressing(follower_collection)
72 end
73
74 def fix(data, meta) do
75 object = meta[:object_data]
76
77 data
78 |> CommonFixes.fix_actor()
79 |> Map.put_new("context", object["context"])
80 |> fix_addressing(object)
81 end
82
83 defp validate_data(cng, meta) do
84 object = meta[:object_data]
85
86 cng
87 |> validate_required([:actor, :type, :object, :to, :cc])
88 |> validate_inclusion(:type, ["Create"])
89 |> CommonValidations.validate_actor_presence()
90 |> validate_actors_match(object)
91 |> validate_context_match(object)
92 |> validate_addressing_match(object)
93 |> validate_object_nonexistence()
94 |> validate_object_containment()
95 end
96
97 def validate_object_containment(cng) do
98 actor = get_field(cng, :actor)
99
100 cng
101 |> validate_change(:object, fn :object, object_id ->
102 %URI{host: object_id_host} = URI.parse(object_id)
103 %URI{host: actor_host} = URI.parse(actor)
104
105 if object_id_host == actor_host do
106 []
107 else
108 [{:object, "The host of the object id doesn't match with the host of the actor"}]
109 end
110 end)
111 end
112
113 def validate_object_nonexistence(cng) do
114 cng
115 |> validate_change(:object, fn :object, object_id ->
116 if Object.get_cached_by_ap_id(object_id) do
117 [{:object, "The object to create already exists"}]
118 else
119 []
120 end
121 end)
122 end
123
124 def validate_actors_match(cng, object) do
125 attributed_to = object["attributedTo"] || object["actor"]
126
127 cng
128 |> validate_change(:actor, fn :actor, actor ->
129 if actor == attributed_to do
130 []
131 else
132 [{:actor, "Actor doesn't match with object attributedTo"}]
133 end
134 end)
135 end
136
137 def validate_context_match(cng, %{"context" => object_context}) do
138 cng
139 |> validate_change(:context, fn :context, context ->
140 if context == object_context do
141 []
142 else
143 [{:context, "context field not matching between Create and object (#{object_context})"}]
144 end
145 end)
146 end
147
148 def validate_addressing_match(cng, object) do
149 [:to, :cc, :bcc, :bto]
150 |> Enum.reduce(cng, fn field, cng ->
151 object_data = object[to_string(field)]
152
153 validate_change(cng, field, fn field, data ->
154 if data == object_data do
155 []
156 else
157 [{field, "field doesn't match with object (#{inspect(object_data)})"}]
158 end
159 end)
160 end)
161 end
162 end