fix(media-proxy): safely detect extensionless images

This commit is contained in:
Lain Soykaf 2026-07-25 00:06:03 +04:00
commit d5f48101b8
No known key found for this signature in database
11 changed files with 211 additions and 609 deletions

View file

@ -1,59 +0,0 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentTypeSniffer do
@moduledoc """
Best-effort MIME sniffing for remote attachments whose declared type is
unusable (missing or `application/octet-stream`).
Fetches the first chunk of the URL and runs it through libmagic. Only
`image/*` results are returned; anything else, or any failure, yields `nil` —
so this never reclassifies non-image media and never blocks ingestion. This
exists because some remotes (e.g. extensionless Cloudflare Images URLs) ship
attachments with no usable `mediaType`, which would otherwise render in the
timeline as a clickable link instead of an inline image.
"""
alias Pleroma.HTTP
require Logger
# libmagic only needs the file header; a few KB is plenty and keeps the
# request cheap even when the origin ignores Range.
@sniff_bytes 8 * 1024
@timeout 5_000
@spec sniff_image_type(binary() | nil) :: {:ok, binary() | nil}
def sniff_image_type(url) when is_binary(url) and url != "" do
do_sniff(url)
rescue
e ->
Logger.debug(
"Attachment type sniff failed for #{url}: #{Exception.format(:error, e, __STACKTRACE__)}"
)
{:ok, nil}
end
def sniff_image_type(_), do: {:ok, nil}
defp do_sniff(url) do
headers = [{"range", "bytes=0-#{@sniff_bytes - 1}"}]
opts = [pool: :media, timeout: @timeout, recv_timeout: @timeout]
with {:ok, %Tesla.Env{status: status, body: body}} when status in 200..299 <-
HTTP.get(url, headers, opts),
false <- blank?(body),
{:ok, %{mime_type: mime}} <- Majic.perform({:bytes, body}, pool: Pleroma.MajicPool),
true <- String.starts_with?(mime, "image/") do
{:ok, mime}
else
_ -> {:ok, nil}
end
end
defp blank?(nil), do: true
defp blank?(""), do: true
defp blank?(_), do: false
end

View file

@ -6,7 +6,6 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
use Ecto.Schema
alias Pleroma.EctoType.ActivityPub.ObjectValidators
alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentTypeSniffer
import Ecto.Changeset
@ -62,27 +61,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
end
def fix_media_type(data) do
current = data["mediaType"] || data["mimeType"] || "application/octet-stream"
resolved =
if sniffable?(current) do
case AttachmentTypeSniffer.sniff_image_type(data["href"]) do
{:ok, mime} when is_binary(mime) -> mime
_ -> current
end
else
current
end
Map.put(data, "mediaType", resolved)
Map.put_new(data, "mediaType", data["mimeType"] || "application/octet-stream")
end
# Only sniff when the declared type carries no real information. Everything
# else (including unrecognized-but-present types) is left as the remote sent it.
defp sniffable?("application/octet-stream"), do: true
defp sniffable?(""), do: true
defp sniffable?(_), do: false
defp handle_href(href, mediaType, data) do
[
%{

View file

@ -620,6 +620,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
String.contains?(media_type, "image") -> "image"
String.contains?(media_type, "video") -> "video"
String.contains?(media_type, "audio") -> "audio"
attachment["type"] == "Image" -> "image"
true -> "unknown"
end

View file

@ -208,6 +208,7 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
defp media_proxy_opts do
Config.get([:media_proxy, :proxy_opts], [])
|> Keyword.put_new(:sniff_content_type, true)
end
defp sandbox(conn, _params) do