fix(upload): recheck platform visibility when completing a chunked upload (#5458)

/start checked that the caller could see the platform, but /complete wrote
into the platform folder using the fs_slug cached in the upload session. An
Editor who opened a session before an admin hid the platform could still
finish it during the 24h session TTL.

Completion now re-resolves the platform through the same helper /start
uses, so a hidden or deleted platform answers "Platform not found" and the
staged chunks are cleaned up. The session no longer caches the fs_slug; the
folder comes from the platform's current row. The ROM-folder branch already
rechecked ROM visibility at completion.


Claude-Session: https://claude.ai/code/session_011waFLHbQCsqHfeX1CKGZ7s

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-11-02 08:07:43 -05:00
committed by GitHub
parent de38cc0e75
commit 8a82d4a5a5
2 changed files with 56 additions and 13 deletions

View File

@@ -42,6 +42,7 @@ from handler.rom_upload import (
)
from logger.logger import log
from models.audit_event import AuditAction
from models.platform import Platform
from models.rom import Rom, RomFile
from utils.router import APIRouter
@@ -124,6 +125,18 @@ def _sanitized_filename(filename: str) -> str:
) from exc
def _get_upload_platform(request: Request, platform_id: int) -> Platform:
platform = db_platform_handler.get_platform(platform_id)
# A hidden platform answers like a missing one, so a restricted caller can
# neither write into its folder nor tell the two apart.
if not platform or not get_permissions(request).can_see_platform(platform_id):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Platform not found",
)
return platform
def _get_upload_rom(request: Request, rom_id: int, platform_id: int) -> Rom:
rom = db_rom_handler.get_rom_simple(rom_id)
if not rom:
@@ -242,8 +255,10 @@ async def _resolve_destination(
filename = session["filename"]
rom_id = session.get("rom_id")
if rom_id is None:
# Visibility can be revoked while the session is open.
platform = _get_upload_platform(request, session["platform_id"])
try:
roms_path = fs_rom_handler.get_roms_upload_path(session["platform_fs_slug"])
roms_path = fs_rom_handler.get_roms_upload_path(platform.fs_slug)
location = fs_rom_handler.validate_path(f"{roms_path}/{filename}")
except ValueError as exc:
raise HTTPException(
@@ -316,16 +331,7 @@ async def start_chunked_upload(
detail="Chunk count does not match the file size",
)
db_platform = db_platform_handler.get_platform(platform_id)
# A hidden platform answers like a missing one, so a restricted caller can
# neither write into its folder nor tell the two apart.
if not db_platform or not get_permissions(request).can_see_platform(platform_id):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Platform not found",
)
platform_fs_slug = db_platform.fs_slug
db_platform = _get_upload_platform(request, platform_id)
if target and target.filename is not None:
filename = target.filename
safe_filename = _sanitized_filename(filename)
@@ -335,7 +341,7 @@ async def start_chunked_upload(
if rom_id is None:
try:
roms_path = fs_rom_handler.get_roms_upload_path(platform_fs_slug)
roms_path = fs_rom_handler.get_roms_upload_path(db_platform.fs_slug)
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)
@@ -370,7 +376,6 @@ async def start_chunked_upload(
session = {
"upload_id": upload_id,
"platform_id": platform_id,
"platform_fs_slug": platform_fs_slug,
"filename": safe_filename,
"total_chunks": total_chunks,
"total_size": total_size,

View File

@@ -14,8 +14,10 @@ from tests.redis_stubs import fail_expire, record_pipelines
from endpoints.roms import upload as upload_endpoint
from handler import rom_upload
from handler.database import db_rom_handler
from handler.database.base_handler import sync_session
from handler.filesystem import fs_rom_handler
from handler.redis_handler import sync_cache
from models.permission import HiddenEntity, PermEntity
from models.platform import Platform
from models.rom import DocSource, Rom, RomFile, RomFileCategory
from models.user import User
@@ -267,6 +269,42 @@ def test_complete_missing_chunks_returns_400(
assert complete_response.json()["detail"] == "Missing chunks: [1]"
def test_complete_after_the_platform_is_hidden_returns_400(
client: TestClient,
editor_user: User,
editor_access_token: str,
platform: Platform,
upload_fs: dict[str, Any],
):
start_response = _start_upload(
client, editor_access_token, platform.id, total_size=6, total_chunks=1
)
upload_id = start_response.json()["upload_id"]
with sync_session.begin() as s:
s.add(
HiddenEntity(
entity=PermEntity.PLATFORMS,
entity_id=platform.id,
user_id=editor_user.id,
)
)
client.put(
f"/api/roms/upload/{upload_id}",
headers={**_auth_headers(editor_access_token), "x-chunk-index": "0"},
content=b"ABCDEF",
)
complete_response = client.post(
f"/api/roms/upload/{upload_id}/complete",
headers=_auth_headers(editor_access_token),
)
assert complete_response.status_code == status.HTTP_400_BAD_REQUEST
assert complete_response.json()["detail"] == "Platform not found"
assert not (upload_fs["final_dir"] / "game.zip").exists()
assert not (upload_fs["uploads_tmp"] / upload_id).exists()
def test_complete_invalid_upload_id(client: TestClient, access_token: str):
response = client.post(
"/api/roms/upload/not-a-uuid/complete",