CommonAPI: Prevent users from accessing media of other users
commit 1afde067b1 upstream.
This commit is contained in:
parent
1f4be2b349
commit
535a5ecad0
9 changed files with 85 additions and 30 deletions
|
|
@ -40,7 +40,11 @@ defmodule Pleroma.ScheduledActivity do
|
|||
%{changes: %{params: %{"media_ids" => media_ids} = params}} = changeset
|
||||
)
|
||||
when is_list(media_ids) do
|
||||
media_attachments = Utils.attachments_from_ids(%{media_ids: media_ids})
|
||||
media_attachments =
|
||||
Utils.attachments_from_ids(
|
||||
%{media_ids: media_ids},
|
||||
User.get_cached_by_id(changeset.data.user_id)
|
||||
)
|
||||
|
||||
params =
|
||||
params
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ defmodule Pleroma.Web.CommonAPI do
|
|||
|
||||
def post_chat_message(%User{} = user, %User{} = recipient, content, opts \\ []) do
|
||||
with maybe_attachment <- opts[:media_id] && Object.get_by_id(opts[:media_id]),
|
||||
:ok <- validate_chat_attachment_attribution(maybe_attachment, user),
|
||||
:ok <- validate_chat_content_length(content, !!maybe_attachment),
|
||||
{_, {:ok, chat_message_data, _meta}} <-
|
||||
{:build_object,
|
||||
|
|
@ -71,6 +72,17 @@ defmodule Pleroma.Web.CommonAPI do
|
|||
text
|
||||
end
|
||||
|
||||
defp validate_chat_attachment_attribution(nil, _), do: :ok
|
||||
|
||||
defp validate_chat_attachment_attribution(attachment, user) do
|
||||
with :ok <- Object.authorize_access(attachment, user) do
|
||||
:ok
|
||||
else
|
||||
e ->
|
||||
e
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_chat_content_length(_, true), do: :ok
|
||||
defp validate_chat_content_length(nil, false), do: {:error, :no_content}
|
||||
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
|
|||
end
|
||||
|
||||
defp attachments(%{params: params} = draft) do
|
||||
attachments = Utils.attachments_from_ids(params)
|
||||
attachments = Utils.attachments_from_ids(params, draft.user)
|
||||
draft = %__MODULE__{draft | attachments: attachments}
|
||||
|
||||
case Utils.validate_attachments_count(attachments) do
|
||||
|
|
|
|||
|
|
@ -23,21 +23,21 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
|||
require Logger
|
||||
require Pleroma.Constants
|
||||
|
||||
def attachments_from_ids(%{media_ids: ids, descriptions: desc}) do
|
||||
attachments_from_ids_descs(ids, desc)
|
||||
def attachments_from_ids(%{media_ids: ids, descriptions: desc}, user) do
|
||||
attachments_from_ids_descs(ids, desc, user)
|
||||
end
|
||||
|
||||
def attachments_from_ids(%{media_ids: ids}) do
|
||||
attachments_from_ids_no_descs(ids)
|
||||
def attachments_from_ids(%{media_ids: ids}, user) do
|
||||
attachments_from_ids_no_descs(ids, user)
|
||||
end
|
||||
|
||||
def attachments_from_ids(_), do: []
|
||||
def attachments_from_ids(_, _), do: []
|
||||
|
||||
def attachments_from_ids_no_descs([]), do: []
|
||||
def attachments_from_ids_no_descs([], _), do: []
|
||||
|
||||
def attachments_from_ids_no_descs(ids) do
|
||||
def attachments_from_ids_no_descs(ids, user) do
|
||||
Enum.map(ids, fn media_id ->
|
||||
case get_attachment(media_id) do
|
||||
case get_attachment(media_id, user) do
|
||||
%Object{data: data} -> data
|
||||
_ -> nil
|
||||
end
|
||||
|
|
@ -45,21 +45,26 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
|||
|> Enum.reject(&is_nil/1)
|
||||
end
|
||||
|
||||
def attachments_from_ids_descs([], _), do: []
|
||||
def attachments_from_ids_descs([], _, _), do: []
|
||||
|
||||
def attachments_from_ids_descs(ids, descs_str) do
|
||||
def attachments_from_ids_descs(ids, descs_str, user) do
|
||||
{_, descs} = Jason.decode(descs_str)
|
||||
|
||||
Enum.map(ids, fn media_id ->
|
||||
with %Object{data: data} <- get_attachment(media_id) do
|
||||
with %Object{data: data} <- get_attachment(media_id, user) do
|
||||
Map.put(data, "name", descs[media_id])
|
||||
end
|
||||
end)
|
||||
|> Enum.reject(&is_nil/1)
|
||||
end
|
||||
|
||||
defp get_attachment(media_id) do
|
||||
Repo.get(Object, media_id)
|
||||
defp get_attachment(media_id, user) do
|
||||
with %Object{data: _data} = object <- Repo.get(Object, media_id),
|
||||
:ok <- Object.authorize_access(object, user) do
|
||||
object
|
||||
else
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@spec get_to_and_cc(ActivityDraft.t()) :: {list(String.t()), list(String.t())}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue