mirror of
https://github.com/rommapp/romm.git
synced 2026-12-05 05:07:31 +01:00
Merge pull request #3714 from rommapp/posthog-code/fix-manual-inline-viewer
fix(backend): serve PDF/Markdown manuals inline so the viewer can render them
This commit is contained in:
@@ -18,7 +18,11 @@ from logger.formatter import highlight as hl
|
||||
from logger.logger import log
|
||||
from models.rom import RomFileCategory
|
||||
from utils.audio_tags import guess_audio_media_type
|
||||
from utils.media_types import guess_media_file_type, is_allowed_media_file
|
||||
from utils.media_types import (
|
||||
guess_media_file_type,
|
||||
is_allowed_document_file,
|
||||
is_allowed_media_file,
|
||||
)
|
||||
from utils.nginx import FileRedirectResponse
|
||||
from utils.router import APIRouter
|
||||
|
||||
@@ -104,6 +108,14 @@ async def get_romfile_content(
|
||||
if file.category == RomFileCategory.SOUNDTRACK:
|
||||
media_type = guess_audio_media_type(file.file_name)
|
||||
disposition = "inline"
|
||||
elif file.category == RomFileCategory.MANUAL and is_allowed_document_file(
|
||||
file.file_name
|
||||
):
|
||||
# Only manuals are served inline as documents so the in-page viewer can
|
||||
# render them; a game/extra file that happens to end in .pdf/.md still
|
||||
# downloads.
|
||||
media_type = guess_media_file_type(file.file_name)
|
||||
disposition = "inline"
|
||||
elif is_allowed_media_file(file.file_name):
|
||||
media_type = guess_media_file_type(file.file_name)
|
||||
disposition = "inline"
|
||||
@@ -111,6 +123,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 +138,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 +146,5 @@ async def get_romfile_content(
|
||||
download_path=Path(f"/library/{file.full_path}"),
|
||||
disposition=disposition,
|
||||
media_type=media_type,
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
@@ -72,6 +72,59 @@ 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_non_manual_document_served_as_attachment(
|
||||
client: TestClient, access_token: str, admin_user: User, platform: Platform
|
||||
):
|
||||
# A game/extra file that happens to end in .pdf must still download; only
|
||||
# manual-category documents are served inline.
|
||||
rom = _make_rom(admin_user, platform)
|
||||
file = _add_file(rom, "readme.pdf", RomFileCategory.GAME)
|
||||
|
||||
r = client.get(
|
||||
f"/api/roms/{file.id}/files/content/readme.pdf",
|
||||
headers=_auth(access_token),
|
||||
)
|
||||
|
||||
assert r.status_code == status.HTTP_200_OK
|
||||
assert r.headers["content-type"].startswith("application/octet-stream")
|
||||
assert r.headers["content-disposition"].startswith("attachment")
|
||||
|
||||
|
||||
def test_rom_file_served_as_attachment(
|
||||
client: TestClient, access_token: str, admin_user: User, platform: Platform
|
||||
):
|
||||
|
||||
@@ -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,6 +55,12 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user