Files
romm/backend/tests/endpoints/test_states.py
Georges-Antoine Assi 96833892ce security: enforce asset upload limits before spooling, cover screenshots
Addresses the Greptile review on #3952.

- Reject oversized asset uploads from Content-Length in middleware, before
  FastAPI parses and spools the multipart body to temporary storage. The
  handler-level check stays as a backstop for chunked requests.
- Apply the limit to screenshot fields on the save, state and standalone
  screenshot endpoints, not just the primary file.
- Clamp the autocleanup limit to at least 1 so a non-positive value cannot
  wipe every save in a slot.
- Document the new settings in env.template.

Rename MAX_SAVE_UPLOAD_SIZE_BYTES to MAX_ASSET_UPLOAD_SIZE_BYTES now that it
covers states and screenshots too.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 09:24:42 -04:00

155 lines
4.9 KiB
Python

from unittest import mock
import pytest
from fastapi import status
from handler.database import db_screenshot_handler, db_state_handler
from handler.database.base_handler import sync_session
from models.assets import Screenshot, State
from models.permission import HiddenEntity, PermEntity
from models.platform import Platform
from models.rom import Rom
from models.user import User
from utils import uploads
def _auth(token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {token}"}
def _hide(entity: PermEntity, entity_id: int, user_id: int) -> None:
with sync_session.begin() as s:
s.add(HiddenEntity(entity=entity, entity_id=entity_id, user_id=user_id))
@mock.patch("endpoints.states.fs_asset_handler.validate_path")
def test_owner_downloads_own_state(
mock_validate_path, client, access_token: str, state: State, tmp_path
):
test_file = tmp_path / "test.state"
test_file.write_bytes(b"STATE_DATA")
mock_validate_path.return_value = test_file
response = client.get(
f"/api/states/{state.id}/content", headers=_auth(access_token)
)
assert response.status_code == status.HTTP_200_OK
assert response.content == b"STATE_DATA"
def test_other_user_cannot_download_private_state(
client, viewer_access_token: str, state: State
):
response = client.get(
f"/api/states/{state.id}/content", headers=_auth(viewer_access_token)
)
assert response.status_code == status.HTTP_404_NOT_FOUND
@mock.patch("endpoints.states.fs_asset_handler.validate_path")
def test_other_user_downloads_public_state(
mock_validate_path, client, viewer_access_token: str, state: State, tmp_path
):
db_state_handler.update_state(state.id, {"is_public": True})
test_file = tmp_path / "test.state"
test_file.write_bytes(b"SHARED_STATE")
mock_validate_path.return_value = test_file
response = client.get(
f"/api/states/{state.id}/content", headers=_auth(viewer_access_token)
)
assert response.status_code == status.HTTP_200_OK
assert response.content == b"SHARED_STATE"
def test_hidden_rom_masks_public_state_download(
client, viewer_access_token: str, viewer_user: User, state: State, rom: Rom
):
# A public state on a ROM hidden from the caller must stay 404-masked;
# sharing cannot override the hidden-resource boundary.
db_state_handler.update_state(state.id, {"is_public": True})
_hide(PermEntity.ROMS, rom.id, viewer_user.id)
response = client.get(
f"/api/states/{state.id}/content", headers=_auth(viewer_access_token)
)
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_hidden_platform_masks_public_state_download(
client,
viewer_access_token: str,
viewer_user: User,
state: State,
platform: Platform,
):
# Hiding the parent platform cascades to its states as well.
db_state_handler.update_state(state.id, {"is_public": True})
_hide(PermEntity.PLATFORMS, platform.id, viewer_user.id)
response = client.get(
f"/api/states/{state.id}/content", headers=_auth(viewer_access_token)
)
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_download_state_not_found(client, access_token: str):
response = client.get("/api/states/99999/content", headers=_auth(access_token))
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_sharing_state_syncs_thumbnail_visibility(
client,
access_token: str,
state: State,
rom: Rom,
platform: Platform,
admin_user: User,
):
# Thumbnail whose filename stem matches the state (how State.screenshot links).
thumb = db_screenshot_handler.add_screenshot(
Screenshot(
rom_id=rom.id,
user_id=admin_user.id,
file_name="test_state.png",
file_path=f"{platform.slug}/screenshots",
file_size_bytes=1,
is_public=False,
)
)
response = client.put(
f"/api/states/{state.id}/visibility",
json={"is_public": True},
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["is_public"] is True
refreshed = db_screenshot_handler.get_screenshot_by_id(thumb.id)
assert refreshed is not None and refreshed.is_public is True
@pytest.mark.parametrize(
"files",
[
{"stateFile": ("game.state", b"x" * 64, "application/octet-stream")},
{
"stateFile": ("game.state", b"small", "application/octet-stream"),
"screenshotFile": ("shot.png", b"x" * 64, "image/png"),
},
],
ids=["state-file", "screenshot-file"],
)
def test_add_state_rejects_oversized_uploads(
client, access_token: str, rom: Rom, files: dict
):
with mock.patch.object(uploads, "MAX_ASSET_UPLOAD_SIZE_BYTES", 32):
response = client.post(
f"/api/states?rom_id={rom.id}",
files=files,
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_413_CONTENT_TOO_LARGE