mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
EmulatorJS's nightly build (loaded when netplay is on) names a downloaded ROM with /filename="?([^";]+)"?/ against Content-Disposition. With filename* first, that matched "*=UTF-8''xmcota.zip", so FBNeo and the MAME cores, which identify a romset by its file name, reported an unknown romset. The nightly now keeps arcade zips whole, so the name is all that still broke them. RFC 6266 appendix D asks for filename before filename* for this reason; browsers still prefer filename*. The header is built by one helper now, and ZipResponse takes the raw name so it is encoded once. Fixes #3279 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
142 lines
4.5 KiB
Python
142 lines
4.5 KiB
Python
"""Tests for the nginx response helpers."""
|
|
|
|
import re
|
|
from pathlib import PurePath
|
|
|
|
import pytest
|
|
|
|
from utils.nginx import (
|
|
FileRedirectResponse,
|
|
ZipContentLine,
|
|
ZipResponse,
|
|
content_disposition,
|
|
)
|
|
|
|
# The pattern EmulatorJS's downloader names a fetched ROM with.
|
|
EMULATORJS_FILENAME = re.compile(r'filename="?([^";]+)"?')
|
|
|
|
|
|
class TestZipContentLine:
|
|
"""The manifest is line-oriented, so a line must render as exactly one record."""
|
|
|
|
def test_renders_the_mod_zip_field_order(self):
|
|
line = ZipContentLine(
|
|
crc32="deadbeef",
|
|
size_bytes=42,
|
|
encoded_location="/library/roms/gc/game/disc.iso",
|
|
filename="roms/gc/game/disc.iso",
|
|
)
|
|
|
|
assert (
|
|
str(line)
|
|
== "deadbeef 42 /library/roms/gc/game/disc.iso roms/gc/game/disc.iso"
|
|
)
|
|
|
|
def test_renders_a_missing_crc32_as_a_hyphen(self):
|
|
line = ZipContentLine(
|
|
crc32=None,
|
|
size_bytes=42,
|
|
encoded_location="/library/rom.iso",
|
|
filename="rom.iso",
|
|
)
|
|
|
|
assert str(line).startswith("- 42 ")
|
|
|
|
@pytest.mark.parametrize("control", ["\n", "\r", "\t", "\0", "\x7f"])
|
|
def test_replaces_control_characters_in_the_filename(self, control: str):
|
|
line = ZipContentLine(
|
|
crc32=None,
|
|
size_bytes=3,
|
|
encoded_location="/library/rom.iso",
|
|
filename=f"a{control}b",
|
|
)
|
|
|
|
assert line.filename == "a_b"
|
|
assert str(line).count("\n") == 0
|
|
|
|
def test_a_forged_filename_stays_one_record(self):
|
|
"""A folder named on disk to look like a second manifest entry."""
|
|
line = ZipContentLine(
|
|
crc32=None,
|
|
size_bytes=8,
|
|
encoded_location="/library/roms/gc/game/disc.iso",
|
|
filename="roms/gc/a\n- 3 /decode?value=cG5n pwned/disc.iso",
|
|
)
|
|
|
|
assert "\n" not in str(line)
|
|
|
|
@pytest.mark.parametrize(
|
|
"location",
|
|
["/library/a b.iso", "/library/a\nb.iso", "/decode?value=cG5n\x7f"],
|
|
)
|
|
def test_rejects_an_unencoded_location(self, location: str):
|
|
with pytest.raises(ValueError, match="URL-encoded"):
|
|
ZipContentLine(
|
|
crc32=None,
|
|
size_bytes=1,
|
|
encoded_location=location,
|
|
filename="rom.iso",
|
|
)
|
|
|
|
def test_accepts_the_m3u_decode_location(self):
|
|
line = ZipContentLine(
|
|
crc32="deadbeef",
|
|
size_bytes=4,
|
|
encoded_location="/decode?value=YS9iCg==",
|
|
filename="game.m3u",
|
|
)
|
|
|
|
assert line.encoded_location == "/decode?value=YS9iCg=="
|
|
|
|
|
|
class TestZipResponse:
|
|
def test_body_holds_one_line_per_entry(self):
|
|
lines = [
|
|
ZipContentLine(
|
|
crc32=None,
|
|
size_bytes=3,
|
|
encoded_location=f"/library/rom{i}.iso",
|
|
filename=f"a\nrom{i}.iso",
|
|
)
|
|
for i in range(3)
|
|
]
|
|
|
|
response = ZipResponse(content_lines=lines, filename="roms.zip")
|
|
|
|
assert len(bytes(response.body).decode().splitlines()) == 3
|
|
assert response.headers["x-archive-files"] == "zip"
|
|
|
|
def test_rejects_a_caller_supplied_body(self):
|
|
with pytest.raises(ValueError, match="must not be provided"):
|
|
ZipResponse(content_lines=[], filename="roms.zip", content="whatever")
|
|
|
|
|
|
class TestContentDisposition:
|
|
def test_puts_the_plain_filename_first(self):
|
|
assert (
|
|
content_disposition("sf2.zip")
|
|
== "attachment; filename=\"sf2.zip\"; filename*=UTF-8''sf2.zip"
|
|
)
|
|
|
|
def test_a_first_match_parser_reads_the_bare_name(self):
|
|
"""Arcade cores find a romset by its file name, so it must survive intact."""
|
|
match = EMULATORJS_FILENAME.search(content_disposition("xmcota.zip"))
|
|
|
|
assert match is not None
|
|
assert match.group(1) == "xmcota.zip"
|
|
|
|
def test_percent_encodes_the_name_once(self):
|
|
assert content_disposition("Zoë & co.zip", "inline") == (
|
|
'inline; filename="Zo%C3%AB%20%26%20co.zip"; '
|
|
"filename*=UTF-8''Zo%C3%AB%20%26%20co.zip"
|
|
)
|
|
|
|
def test_zip_and_redirect_responses_use_it(self):
|
|
zipped = ZipResponse(content_lines=[], filename="a b.zip")
|
|
redirected = FileRedirectResponse(download_path=PurePath("/library/a b.iso"))
|
|
|
|
assert zipped.headers["content-disposition"] == content_disposition("a b.zip")
|
|
assert redirected.headers["content-disposition"] == content_disposition(
|
|
"a b.iso"
|
|
)
|