logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

update_validator.ex (1769B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator do
  5. use Ecto.Schema
  6. alias Pleroma.EctoType.ActivityPub.ObjectValidators
  7. import Ecto.Changeset
  8. import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
  9. @primary_key false
  10. embedded_schema do
  11. quote do
  12. unquote do
  13. import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
  14. message_fields()
  15. end
  16. end
  17. field(:actor, ObjectValidators.ObjectID)
  18. # In this case, we save the full object in this activity instead of just a
  19. # reference, so we can always see what was actually changed by this.
  20. field(:object, :map)
  21. end
  22. def cast_data(data) do
  23. %__MODULE__{}
  24. |> cast(data, __schema__(:fields))
  25. end
  26. defp validate_data(cng) do
  27. cng
  28. |> validate_required([:id, :type, :actor, :to, :cc, :object])
  29. |> validate_inclusion(:type, ["Update"])
  30. |> validate_actor_presence()
  31. |> validate_updating_rights()
  32. end
  33. def cast_and_validate(data) do
  34. data
  35. |> cast_data
  36. |> validate_data
  37. end
  38. # For now we only support updating users, and here the rule is easy:
  39. # object id == actor id
  40. def validate_updating_rights(cng) do
  41. with actor = get_field(cng, :actor),
  42. object = get_field(cng, :object),
  43. {:ok, object_id} <- ObjectValidators.ObjectID.cast(object),
  44. actor_uri <- URI.parse(actor),
  45. object_uri <- URI.parse(object_id),
  46. true <- actor_uri.host == object_uri.host do
  47. cng
  48. else
  49. _e ->
  50. cng
  51. |> add_error(:object, "Can't be updated by this actor")
  52. end
  53. end
  54. end