AP ingest: harden attachment parsing

This commit is contained in:
Lain Soykaf 2026-01-17 00:08:19 +04:00
commit e781a7ef9b
6 changed files with 182 additions and 6 deletions

View file

@ -100,10 +100,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
defp fix_replies(data), do: Map.delete(data, "replies")
def fix_attachments(%{"attachment" => attachment} = data) when is_map(attachment),
do: Map.put(data, "attachment", [attachment])
def fix_attachments(data), do: data
def fix_attachments(data), do: Transmogrifier.fix_attachments(data)
defp fix(data) do
data

View file

@ -293,6 +293,12 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|> Map.drop(["conversation"])
end
defp valid_http_url?(href) when is_binary(href) do
match?({:ok, _}, ObjectValidators.Uri.cast(href))
end
defp valid_http_url?(_), do: false
def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
attachments =
Enum.map(attachment, fn data ->
@ -305,7 +311,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
media_type =
cond do
is_map(url) && url =~ Pleroma.Constants.mime_regex() ->
is_map(url) && is_bitstring(url["mediaType"]) &&
url["mediaType"] =~ Pleroma.Constants.mime_regex() ->
url["mediaType"]
is_bitstring(data["mediaType"]) && data["mediaType"] =~ Pleroma.Constants.mime_regex() ->
@ -326,7 +333,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
true -> nil
end
if href do
if href && valid_http_url?(href) do
attachment_url =
%{
"href" => href,