Skip to content

Derive media content types from file content, not the request - #174

Open
snoopdave wants to merge 1 commit into
masterfrom
media-content-type-handling
Open

Derive media content types from file content, not the request#174
snoopdave wants to merge 1 commit into
masterfrom
media-content-type-handling

Conversation

@snoopdave

Copy link
Copy Markdown
Contributor

This change derives a stored media file's content type from the file itself
rather than from the type declared with the upload, and serves media inline only
for a small explicit allow-list of passive formats.

What changed

  • Add MediaTypePolicy as the single place for the stored type, the inline
    allow-list, and the response headers.
  • Derive the stored type from the filename; consult the declared type only for
    opaque names and never adopt an active type (an explicit list plus any +xml
    suffix).
  • Send X-Content-Type-Options: nosniff on every media response.
  • Serve inline only passive images, audio, video, and PDF; serve everything else
    as application/octet-stream with an attachment disposition. SVG is excluded.
  • Route all five upload entry points and the three serving paths through the
    policy.

Note for the release notes: media held as CSS or JavaScript now downloads
instead of loading inline — a user-visible compatibility change.

Tests

MediaTypePolicyTest (9 behavioral cases plus 3 source audits asserting every
caller routes through the policy) covers a file whose declared type does not
match its name, SVG / XHTML and unknown types, genuine images retaining their
type, and nosniff on every response.

The type an upload declares is treated as a hint, and the stored type is
derived from the file name through one shared MediaTypePolicy. Serving applies
the other half of the policy: only a short list of passively-rendered formats is
sent inline, everything else is sent as an attachment, and every media response
carries nosniff.

All paths that accept an upload and all paths that serve uploaded media route
through the policy, including the entry editor's replacement-body path and the
resource servlets' uploaded-media fallback.

Files whose type is outside the inline list, such as CSS and JavaScript held as
media, now download rather than render. That is a behaviour change and belongs in
the release notes.

Claude-Session: https://claude.ai/code/session_01A1fhY1E2PCFU6UAPXu2WtV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant