fix(backend): serve PDF/Markdown manuals inline so the viewer can render them

Uploaded manual files (RomFile with category="manual") were served from the
content endpoint as application/octet-stream + attachment, so the in-page
manual viewer showed a blank area while nav buttons and download still worked.

Include .pdf and .md in is_allowed_media_file (with explicit MIME overrides)
so they are served inline, and set X-Content-Type-Options: nosniff on inline
responses so a Markdown manual can't be sniffed into HTML.

Fixes #3712

Generated-By: PostHog Code
Task-Id: 73be371c-908a-4f9f-9748-7ddba2df71e2
This commit is contained in:
Georges-Antoine Assi
2026-07-09 19:38:16 -04:00
parent e2e539faad
commit def9258636
3 changed files with 60 additions and 3 deletions

View File

@@ -111,6 +111,11 @@ async def get_romfile_content(
media_type = "application/octet-stream"
disposition = "attachment"
# Inline files are served under an explicit, trusted Content-Type; nosniff
# keeps the browser from sniffing them into anything script-capable (e.g. a
# Markdown manual into HTML).
headers = {"X-Content-Type-Options": "nosniff"} if disposition == "inline" else {}
# Serve the file directly in development mode for emulatorjs
if DEV_MODE:
rom_path = fs_rom_handler.validate_path(file.full_path)
@@ -121,6 +126,7 @@ async def get_romfile_content(
filename=file.file_name,
media_type=media_type,
content_disposition_type=disposition,
headers=headers,
)
# Otherwise proxy through nginx (which parses Range itself via X-Accel-Redirect)
@@ -128,4 +134,5 @@ async def get_romfile_content(
download_path=Path(f"/library/{file.full_path}"),
disposition=disposition,
media_type=media_type,
headers=headers,
)

View File

@@ -72,6 +72,41 @@ def test_video_file_served_inline(
assert r.headers["content-disposition"].startswith("inline")
def test_pdf_manual_served_inline(
client: TestClient, access_token: str, admin_user: User, platform: Platform
):
rom = _make_rom(admin_user, platform)
file = _add_file(rom, "manual.pdf", RomFileCategory.MANUAL)
r = client.get(
f"/api/roms/{file.id}/files/content/manual.pdf",
headers=_auth(access_token),
)
assert r.status_code == status.HTTP_200_OK
assert r.headers["content-type"].startswith("application/pdf")
assert r.headers["content-disposition"].startswith("inline")
assert r.headers["x-content-type-options"] == "nosniff"
def test_markdown_manual_served_inline(
client: TestClient, access_token: str, admin_user: User, platform: Platform
):
rom = _make_rom(admin_user, platform)
file = _add_file(rom, "manual.md", RomFileCategory.MANUAL)
r = client.get(
f"/api/roms/{file.id}/files/content/manual.md",
headers=_auth(access_token),
)
assert r.status_code == status.HTTP_200_OK
assert r.headers["content-type"].startswith("text/markdown")
assert r.headers["content-disposition"].startswith("inline")
# nosniff keeps the browser from sniffing the Markdown into HTML.
assert r.headers["x-content-type-options"] == "nosniff"
def test_rom_file_served_as_attachment(
client: TestClient, access_token: str, admin_user: User, platform: Platform
):

View File

@@ -8,11 +8,14 @@ from typing import Final
# browser) from the ROM download endpoint. Images and videos never execute
# scripts, so serving them inline under an explicit, trusted Content-Type
# avoids the content-sniffing/XSS risk that keeps arbitrary files served as
# attachments.
# attachments. Manuals (PDF/Markdown) are served inline too so the in-page
# manual viewer can render them; the browser renders PDFs in a sandboxed
# viewer and Markdown is served as text/markdown (never sniffed to HTML).
ALLOWED_IMAGE_EXTENSIONS = frozenset(
{".png", ".jpg", ".jpeg", ".webp", ".gif", ".tiff", ".tif", ".bmp", ".avif"}
)
ALLOWED_VIDEO_EXTENSIONS = frozenset({".mp4", ".webm", ".ogv", ".mov", ".m4v"})
ALLOWED_DOCUMENT_EXTENSIONS = frozenset({".pdf", ".md"})
# MIME types the stdlib mimetypes module guesses inconsistently (or not at
# all) across platforms.
@@ -24,6 +27,8 @@ MEDIA_MIME_OVERRIDES = {
".webm": "video/webm",
".ogv": "video/ogg",
".mov": "video/quicktime",
".pdf": "application/pdf",
".md": "text/markdown",
}
# Image MIME types we trust to (a) accept as avatar uploads and (b) serve
@@ -50,9 +55,19 @@ def is_allowed_video_file(file_name: str) -> bool:
return ext in ALLOWED_VIDEO_EXTENSIONS
def is_allowed_document_file(file_name: str) -> bool:
"""Whether a library file is a manual document the browser can render inline."""
ext = os.path.splitext(file_name)[1].lower()
return ext in ALLOWED_DOCUMENT_EXTENSIONS
def is_allowed_media_file(file_name: str) -> bool:
"""Whether a library file is an image/video the browser can render inline."""
return is_allowed_image_file(file_name) or is_allowed_video_file(file_name)
"""Whether a library file is an image/video/document the browser can render inline."""
return (
is_allowed_image_file(file_name)
or is_allowed_video_file(file_name)
or is_allowed_document_file(file_name)
)
def guess_media_file_type(file_name: str) -> str: