Merge remote-tracking branch 'pleroma/develop' into cycles-router
[akkoma] / lib / pleroma / web / activity_pub / object_validators / common_validations.ex
index 140555a45e6ab8706f76058eeaeddbd2f636dd86..94043058817ec081c6d3bc4cc5a0296f8ffb142e 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
   alias Pleroma.Object
   alias Pleroma.User
 
+  @spec validate_any_presence(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
   def validate_any_presence(cng, fields) do
     non_empty =
       fields
@@ -29,32 +30,26 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
     end
   end
 
+  @spec validate_actor_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
   def validate_actor_presence(cng, options \\ []) do
     field_name = Keyword.get(options, :field_name, :actor)
 
     cng
     |> validate_change(field_name, fn field_name, actor ->
-      if User.get_cached_by_ap_id(actor) do
-        []
-      else
-        [{field_name, "can't find user"}]
-      end
-    end)
-  end
+      case User.get_cached_by_ap_id(actor) do
+        %User{is_active: false} ->
+          [{field_name, "user is deactivated"}]
 
-  def validate_actor_is_active(cng, options \\ []) do
-    field_name = Keyword.get(options, :field_name, :actor)
+        %User{} ->
+          []
 
-    cng
-    |> validate_change(field_name, fn field_name, actor ->
-      if %User{deactivated: false} = User.get_cached_by_ap_id(actor) do
-        []
-      else
-        [{field_name, "can't find user (or deactivated)"}]
+        _ ->
+          [{field_name, "can't find user"}]
       end
     end)
   end
 
+  @spec validate_object_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
   def validate_object_presence(cng, options \\ []) do
     field_name = Keyword.get(options, :field_name, :object)
     allowed_types = Keyword.get(options, :allowed_types, false)
@@ -76,6 +71,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
     end)
   end
 
+  @spec validate_object_or_user_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
   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)
@@ -91,28 +87,63 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
     if actor_cng.valid?, do: actor_cng, else: object_cng
   end
 
+  @spec validate_host_match(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
   def validate_host_match(cng, fields \\ [:id, :actor]) do
-    unique_hosts =
+    if same_domain?(cng, fields) do
+      cng
+    else
       fields
-      |> Enum.map(fn field ->
-        %URI{host: host} =
-          cng
-          |> get_field(field)
-          |> URI.parse()
-
-        host
+      |> Enum.reduce(cng, fn field, cng ->
+        cng
+        |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
       end)
-      |> Enum.uniq()
-      |> Enum.count()
+    end
+  end
 
-    if unique_hosts == 1 do
+  @spec validate_fields_match(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
+  def validate_fields_match(cng, fields) do
+    if map_unique?(cng, fields) do
       cng
     else
       fields
       |> Enum.reduce(cng, fn field, cng ->
         cng
-        |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
+        |> add_error(field, "Fields #{inspect(fields)} aren't matching")
       end)
     end
   end
+
+  defp map_unique?(cng, fields, func \\ & &1) do
+    Enum.reduce_while(fields, nil, fn field, acc ->
+      value =
+        cng
+        |> get_field(field)
+        |> func.()
+
+      case {value, acc} do
+        {value, nil} -> {:cont, value}
+        {value, value} -> {:cont, value}
+        _ -> {:halt, false}
+      end
+    end)
+  end
+
+  @spec same_domain?(Ecto.Changeset.t(), [atom()]) :: boolean()
+  def same_domain?(cng, fields \\ [:actor, :object]) do
+    map_unique?(cng, fields, fn value -> URI.parse(value).host end)
+  end
+
+  # This figures out if a user is able to create, delete or modify something
+  # based on the domain and superuser status
+  @spec validate_modification_rights(Ecto.Changeset.t()) :: Ecto.Changeset.t()
+  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
 end