MediaProxy: detect image content types without rewriting upstream filenames #7976

Open
opencode-fix-bot wants to merge 1 commit from opencode-fix-bot/pleroma:opencode-agent/job-70-g1 into develop
Contributor

opencode-agent:job=70:generation=1

Closes #7961

Summary

Media attachments served without a usable Content-Type (missing or application/octet-stream) and without a file extension are forced to content-disposition: attachment by the reverse proxy and are reported as unknown by the Mastodon API, so clients render them as plain files. An earlier attempt (!7933) was reverted in !7959 because it rewrote every whitelisted inline attachment's content-disposition filename to inline.<ext> (#7948).

This MR re-introduces the content-type sniffing part of !7933 without the disposition-rewriting part that caused the regression:

  • Pleroma.ReverseProxy gains an opt-in sniff_content_type option (enabled by the MediaProxy controller): when the upstream type is missing or application/octet-stream, the first chunk (up to 8 KiB) is sniffed with Majic; if an image/* type is detected, the content-type response header is replaced and the rest of the body is streamed unmodified.
  • content-disposition is never rewritten for sniffed or whitelisted inline types, so upstream filenames (including those in attachment; filename=... dispositions) are always preserved.
  • While sniffing, the upstream request is forced to accept-encoding: identity so the sniffed bytes are not compressed.
  • Pleroma.Web.MastodonAPI.StatusView falls back to the ActivityStreams Image type when the MIME metadata is generic, so clients render the attachment as an image and load it through the (now correctly-typed) proxy.
  • Bonus: max_read_duration: :infinity (a documented option value) no longer crashes the streaming loop with CaseClauseError; the :no_duration_limit sentinel now flows through chunk_reply/5 and read_chunk/3.

Non-image bodies keep the generic type and the attachment disposition, and sniffing failures are logged at debug level and leave the response untouched.

Testing

  • test/pleroma/reverse_proxy_test.exs: new tests assert that a JPEG body served as application/octet-stream is served as image/jpeg while the upstream content-disposition: attachment; filename="photo.jpg" is preserved verbatim (the anti-regression case from #7948), that accept-encoding: identity is forced, and that nothing changes when sniffing is disabled.
  • test/pleroma/web/mastodon_api/views/status_view_test.exs: Image + application/octet-stream renders as image.
  • test/pleroma/web/media_proxy/media_proxy_controller_test.exs: MediaProxy passes sniff_content_type: true to the reverse proxy.

Checklist:

  • Adding a changelog: changelog.d/mediaproxy-sniff-content-type.fix
  • Existing tests pass; only pre-existing, environment-related failures remain (WebFinger suite), verified identical on a clean checkout of develop.
opencode-agent:job=70:generation=1 Closes #7961 ## Summary Media attachments served without a usable `Content-Type` (missing or `application/octet-stream`) and without a file extension are forced to `content-disposition: attachment` by the reverse proxy and are reported as `unknown` by the Mastodon API, so clients render them as plain files. An earlier attempt (!7933) was reverted in !7959 because it rewrote every whitelisted inline attachment's `content-disposition` filename to `inline.<ext>` (#7948). This MR re-introduces the content-type sniffing part of !7933 **without** the disposition-rewriting part that caused the regression: - `Pleroma.ReverseProxy` gains an opt-in `sniff_content_type` option (enabled by the MediaProxy controller): when the upstream type is missing or `application/octet-stream`, the first chunk (up to 8 KiB) is sniffed with Majic; if an `image/*` type is detected, the `content-type` response header is replaced and the rest of the body is streamed unmodified. - `content-disposition` is **never** rewritten for sniffed or whitelisted inline types, so upstream filenames (including those in `attachment; filename=...` dispositions) are always preserved. - While sniffing, the upstream request is forced to `accept-encoding: identity` so the sniffed bytes are not compressed. - `Pleroma.Web.MastodonAPI.StatusView` falls back to the ActivityStreams `Image` type when the MIME metadata is generic, so clients render the attachment as an image and load it through the (now correctly-typed) proxy. - Bonus: `max_read_duration: :infinity` (a documented option value) no longer crashes the streaming loop with `CaseClauseError`; the `:no_duration_limit` sentinel now flows through `chunk_reply/5` and `read_chunk/3`. Non-image bodies keep the generic type and the attachment disposition, and sniffing failures are logged at debug level and leave the response untouched. ## Testing - `test/pleroma/reverse_proxy_test.exs`: new tests assert that a JPEG body served as `application/octet-stream` is served as `image/jpeg` while the upstream `content-disposition: attachment; filename="photo.jpg"` is preserved verbatim (the anti-regression case from #7948), that `accept-encoding: identity` is forced, and that nothing changes when sniffing is disabled. - `test/pleroma/web/mastodon_api/views/status_view_test.exs`: `Image` + `application/octet-stream` renders as `image`. - `test/pleroma/web/media_proxy/media_proxy_controller_test.exs`: MediaProxy passes `sniff_content_type: true` to the reverse proxy. Checklist: - [x] Adding a changelog: `changelog.d/mediaproxy-sniff-content-type.fix` - [x] Existing tests pass; only pre-existing, environment-related failures remain (WebFinger suite), verified identical on a clean checkout of `develop`.
MediaProxy: detect image content types without rewriting upstream filenames
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ee07b27694
Media attachments served without a usable Content-Type (missing or
application/octet-stream) and without a file extension are forced to
content-disposition: attachment by the reverse proxy and are reported as
unknown by the Mastodon API, so they end up being shown as plain files
(#7961).

Detect an image MIME type from the first chunk of the proxied stream via
Majic and serve it as the detected type. The body is streamed unmodified
and content-disposition is never rewritten, so upstream filenames are
always preserved - unlike the previous attempt in !7933 which replaced
every whitelisted inline attachment's filename with "inline.<ext>" and
had to be reverted in !7959.

Also treat ActivityStreams Image attachments as images when their MIME
metadata is generic, and let the max_read_duration: :infinity sentinel
flow through the streaming loop instead of crashing with CaseClauseError.
@ -276,0 +345,4 @@
defp maybe_put_image_content_type(headers, data) do
with false <- data == "",
{:ok, %{mime_type: "image/" <> _ = content_type}} <-
Majic.perform({:bytes, binary_part(data, 0, min(byte_size(data), @sniff_bytes))},
Member

Will look at this later, but Majic already truncates {:bytes, binary} input itself and to a smaller value than @sniff_bytes

https://git.pleroma.social/pleroma-elixir-libraries/majic/src/branch/develop/lib/majic/server.ex#L327

Will look at this later, but Majic already truncates `{:bytes, binary}` input itself and to a smaller value than `@sniff_bytes` https://git.pleroma.social/pleroma-elixir-libraries/majic/src/branch/develop/lib/majic/server.ex#L327
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u opencode-agent/job-70-g1:opencode-fix-bot-opencode-agent/job-70-g1
git switch opencode-fix-bot-opencode-agent/job-70-g1

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch develop
git merge --no-ff opencode-fix-bot-opencode-agent/job-70-g1
git switch opencode-fix-bot-opencode-agent/job-70-g1
git rebase develop
git switch develop
git merge --ff-only opencode-fix-bot-opencode-agent/job-70-g1
git switch opencode-fix-bot-opencode-agent/job-70-g1
git rebase develop
git switch develop
git merge --no-ff opencode-fix-bot-opencode-agent/job-70-g1
git switch develop
git merge --squash opencode-fix-bot-opencode-agent/job-70-g1
git switch develop
git merge --ff-only opencode-fix-bot-opencode-agent/job-70-g1
git switch develop
git merge opencode-fix-bot-opencode-agent/job-70-g1
git push origin develop
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
pleroma/pleroma!7976
No description provided.