2019-11-05 14:02:09 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-11-05 14:02:09 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
2020-05-05 12:17:47 +00:00
|
|
|
alias Pleroma.Activity
|
2019-11-05 14:02:09 +00:00
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.User
|
|
|
|
|
2020-06-11 18:23:10 +00:00
|
|
|
def validate_any_presence(cng, fields) do
|
2020-04-29 17:09:51 +00:00
|
|
|
non_empty =
|
|
|
|
fields
|
|
|
|
|> Enum.map(fn field -> get_field(cng, field) end)
|
|
|
|
|> Enum.any?(fn
|
2020-09-10 09:11:10 +00:00
|
|
|
nil -> false
|
2020-04-29 17:09:51 +00:00
|
|
|
[] -> false
|
|
|
|
_ -> true
|
|
|
|
end)
|
|
|
|
|
|
|
|
if non_empty do
|
|
|
|
cng
|
|
|
|
else
|
|
|
|
fields
|
|
|
|
|> Enum.reduce(cng, fn field, cng ->
|
|
|
|
cng
|
2020-06-11 18:23:10 +00:00
|
|
|
|> add_error(field, "none of #{inspect(fields)} present")
|
2020-04-29 17:09:51 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-01 11:34:47 +00:00
|
|
|
def validate_actor_presence(cng, options \\ []) do
|
|
|
|
field_name = Keyword.get(options, :field_name, :actor)
|
|
|
|
|
2019-11-05 14:02:09 +00:00
|
|
|
cng
|
|
|
|
|> validate_change(field_name, fn field_name, actor ->
|
2020-08-04 12:17:03 +00:00
|
|
|
case User.get_cached_by_ap_id(actor) do
|
2020-10-12 22:42:27 +00:00
|
|
|
%User{is_active: false} ->
|
2020-08-04 12:17:03 +00:00
|
|
|
[{field_name, "user is deactivated"}]
|
|
|
|
|
|
|
|
%User{} ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
[{field_name, "can't find user"}]
|
2019-11-05 14:02:09 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-05-01 11:34:47 +00:00
|
|
|
def validate_object_presence(cng, options \\ []) do
|
|
|
|
field_name = Keyword.get(options, :field_name, :object)
|
|
|
|
allowed_types = Keyword.get(options, :allowed_types, false)
|
|
|
|
|
2019-11-05 14:02:09 +00:00
|
|
|
cng
|
2020-05-01 11:34:47 +00:00
|
|
|
|> validate_change(field_name, fn field_name, object_id ->
|
2020-05-07 12:52:37 +00:00
|
|
|
object = Object.get_cached_by_ap_id(object_id) || Activity.get_by_ap_id(object_id)
|
2020-05-01 11:34:47 +00:00
|
|
|
|
|
|
|
cond do
|
|
|
|
!object ->
|
|
|
|
[{field_name, "can't find object"}]
|
|
|
|
|
|
|
|
object && allowed_types && object.data["type"] not in allowed_types ->
|
|
|
|
[{field_name, "object not in allowed types"}]
|
|
|
|
|
|
|
|
true ->
|
|
|
|
[]
|
2019-11-05 14:02:09 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-04-30 13:42:30 +00:00
|
|
|
|
2020-05-01 11:34:47 +00:00
|
|
|
def validate_object_or_user_presence(cng, options \\ []) do
|
|
|
|
field_name = Keyword.get(options, :field_name, :object)
|
|
|
|
options = Keyword.put(options, :field_name, field_name)
|
|
|
|
|
|
|
|
actor_cng =
|
|
|
|
cng
|
|
|
|
|> validate_actor_presence(options)
|
|
|
|
|
|
|
|
object_cng =
|
|
|
|
cng
|
|
|
|
|> validate_object_presence(options)
|
|
|
|
|
|
|
|
if actor_cng.valid?, do: actor_cng, else: object_cng
|
2020-04-30 13:42:30 +00:00
|
|
|
end
|
2020-06-18 02:05:42 +00:00
|
|
|
|
|
|
|
def validate_host_match(cng, fields \\ [:id, :actor]) do
|
2020-08-05 15:56:12 +00:00
|
|
|
if same_domain?(cng, fields) do
|
2020-06-18 02:05:42 +00:00
|
|
|
cng
|
|
|
|
else
|
|
|
|
fields
|
|
|
|
|> Enum.reduce(cng, fn field, cng ->
|
|
|
|
cng
|
|
|
|
|> add_error(field, "hosts of #{inspect(fields)} aren't matching")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2020-06-25 22:07:43 +00:00
|
|
|
|
|
|
|
def validate_fields_match(cng, fields) do
|
2020-08-05 15:56:12 +00:00
|
|
|
if map_unique?(cng, fields) do
|
2020-06-25 22:07:43 +00:00
|
|
|
cng
|
|
|
|
else
|
|
|
|
fields
|
|
|
|
|> Enum.reduce(cng, fn field, cng ->
|
|
|
|
cng
|
|
|
|
|> add_error(field, "Fields #{inspect(fields)} aren't matching")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2020-08-05 15:26:53 +00:00
|
|
|
|
2020-08-05 15:56:12 +00:00
|
|
|
defp map_unique?(cng, fields, func \\ & &1) do
|
|
|
|
Enum.reduce_while(fields, nil, fn field, acc ->
|
|
|
|
value =
|
|
|
|
cng
|
|
|
|
|> get_field(field)
|
|
|
|
|> func.()
|
2020-08-05 15:26:53 +00:00
|
|
|
|
2020-08-05 15:56:12 +00:00
|
|
|
case {value, acc} do
|
|
|
|
{value, nil} -> {:cont, value}
|
|
|
|
{value, value} -> {:cont, value}
|
|
|
|
_ -> {:halt, false}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-08-05 15:26:53 +00:00
|
|
|
|
2020-08-05 15:56:12 +00:00
|
|
|
def same_domain?(cng, fields \\ [:actor, :object]) do
|
|
|
|
map_unique?(cng, fields, fn value -> URI.parse(value).host end)
|
2020-08-05 15:26:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# This figures out if a user is able to create, delete or modify something
|
|
|
|
# based on the domain and superuser status
|
|
|
|
def validate_modification_rights(cng) do
|
|
|
|
actor = User.get_cached_by_ap_id(get_field(cng, :actor))
|
|
|
|
|
|
|
|
if User.superuser?(actor) || same_domain?(cng) do
|
|
|
|
cng
|
|
|
|
else
|
|
|
|
cng
|
|
|
|
|> add_error(:actor, "is not allowed to modify object")
|
|
|
|
end
|
|
|
|
end
|
2019-11-05 14:02:09 +00:00
|
|
|
end
|