mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
fix(steam): serve hand-edited Steam metadata without raising
`raw_steam_metadata` is a form field that stores any JSON object, and
only its top level is checked, so `{"achievements": [123]}` reaches
`merged_steam_metadata`, which spread each entry and matched its icon
name against a regex. Both raise on a value of the wrong type, and the
stored blob keeps raising on every later read of the ROM and of any
gallery page holding it.
An achievements value that is not a list is now served as stored, an
entry that is not an object is dropped, and an icon name that is not a
string yields no path.
`merged_ra_metadata` spreads entries the same way but has no raw form
field, so RA metadata only ever comes from the provider.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NH5s4gb7QNE7xa4pRUsBzx
This commit is contained in:
@@ -798,10 +798,10 @@ _STEAM_ICON_FILENAME = re.compile(r"[\w-]+\.(?:jpe?g|png|gif)", re.IGNORECASE)
|
||||
|
||||
|
||||
def steam_achievement_icon_path(
|
||||
platform_id: int, rom_id: int, icon_file: str | None
|
||||
platform_id: int, rom_id: int, icon_file: object
|
||||
) -> str | None:
|
||||
"""Where a Steam achievement icon is stored, or None for an unusable name."""
|
||||
if not icon_file or not _STEAM_ICON_FILENAME.fullmatch(icon_file):
|
||||
if not isinstance(icon_file, str) or not _STEAM_ICON_FILENAME.fullmatch(icon_file):
|
||||
return None
|
||||
return (
|
||||
f"{rom_fs_resources_path(platform_id, rom_id)}/steam/achievements/{icon_file}"
|
||||
@@ -1378,10 +1378,14 @@ class Rom(BaseModel):
|
||||
|
||||
@cached_property
|
||||
def merged_steam_metadata(self) -> dict[str, Any] | None:
|
||||
if not self.steam_metadata or "achievements" not in self.steam_metadata:
|
||||
if not self.steam_metadata:
|
||||
return self.steam_metadata
|
||||
# `raw_steam_metadata` stores any JSON object, so the shape is not given.
|
||||
stored = self.steam_metadata.get("achievements")
|
||||
if not isinstance(stored, list):
|
||||
return self.steam_metadata
|
||||
|
||||
def resource_url(icon_file: str | None) -> str | None:
|
||||
def resource_url(icon_file: object) -> str | None:
|
||||
path = steam_achievement_icon_path(self.platform_id, self.id, icon_file)
|
||||
return f"{FRONTEND_RESOURCES_PATH}/{path}" if path else None
|
||||
|
||||
@@ -1391,7 +1395,8 @@ class Rom(BaseModel):
|
||||
"icon_path": resource_url(achievement.get("icon_file")),
|
||||
"icon_gray_path": resource_url(achievement.get("icon_gray_file")),
|
||||
}
|
||||
for achievement in self.steam_metadata["achievements"]
|
||||
for achievement in stored
|
||||
if isinstance(achievement, Mapping)
|
||||
]
|
||||
return {**self.steam_metadata, "achievements": achievements}
|
||||
|
||||
|
||||
@@ -567,3 +567,33 @@ class TestMergedSteamMetadata:
|
||||
rom = Rom(id=9, platform_id=2, steam_metadata=stored)
|
||||
|
||||
assert rom.merged_steam_metadata is stored
|
||||
|
||||
@pytest.mark.parametrize("achievements", [None, 123, "a", {"a": 1}])
|
||||
def test_achievements_of_the_wrong_type_are_served_as_stored(
|
||||
self, achievements: Any
|
||||
):
|
||||
stored = {"genres": ["Puzzle"], "achievements": achievements}
|
||||
rom = Rom(id=9, platform_id=2, steam_metadata=stored)
|
||||
|
||||
assert rom.merged_steam_metadata is stored
|
||||
|
||||
def test_entries_that_are_not_objects_are_dropped(self):
|
||||
stored = {"achievements": [123, None, "a", {"api_name": "WIN"}]}
|
||||
rom = Rom(id=9, platform_id=2, steam_metadata=stored)
|
||||
|
||||
merged = rom.merged_steam_metadata
|
||||
|
||||
assert merged is not None
|
||||
assert merged["achievements"] == [
|
||||
{"api_name": "WIN", "icon_path": None, "icon_gray_path": None}
|
||||
]
|
||||
|
||||
@pytest.mark.parametrize("icon_file", [123, ["a.jpg"], {"name": "a.jpg"}, True])
|
||||
def test_icon_names_that_are_not_strings_yield_no_path(self, icon_file: Any):
|
||||
stored = {"achievements": [{"api_name": "WIN", "icon_file": icon_file}]}
|
||||
rom = Rom(id=9, platform_id=2, steam_metadata=stored)
|
||||
|
||||
merged = rom.merged_steam_metadata
|
||||
|
||||
assert merged is not None
|
||||
assert merged["achievements"][0]["icon_path"] is None
|
||||
|
||||
Reference in New Issue
Block a user