From 89a91d7e2924d5a911b4c9b66db4f4f587417240 Mon Sep 17 00:00:00 2001 From: zurdi Date: Fri, 18 Sep 2026 03:41:45 +0200 Subject: [PATCH] fix(upload): refuse an empty file that declares chunks Addresses the Copilot review on #4610. /start accepted a zero-byte file declaring chunks, which then took chunk uploads and failed at assembly on the size check. Zero chunks and zero bytes now have to go together, both ways, and a parametrized test covers each mismatch. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_018mKNYZbuF4aMns81Cptvxc --- backend/endpoints/roms/upload.py | 6 +++--- backend/tests/endpoints/roms/test_upload.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/endpoints/roms/upload.py b/backend/endpoints/roms/upload.py index b69d93ae0..f12d2971a 100644 --- a/backend/endpoints/roms/upload.py +++ b/backend/endpoints/roms/upload.py @@ -279,11 +279,11 @@ async def start_chunked_upload( ) -> dict: """Initiate a chunked ROM upload session.""" - # An empty file takes no chunks and goes straight to /complete. - if total_size > 0 and total_chunks == 0: + # Only an empty file takes no chunks, and it goes straight to /complete. + if (total_size == 0) != (total_chunks == 0): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, - detail="A non-empty file needs at least one chunk", + detail="Chunk count does not match the file size", ) db_platform = db_platform_handler.get_platform(platform_id) diff --git a/backend/tests/endpoints/roms/test_upload.py b/backend/tests/endpoints/roms/test_upload.py index 3e76c69ce..ae2cdb54b 100644 --- a/backend/tests/endpoints/roms/test_upload.py +++ b/backend/tests/endpoints/roms/test_upload.py @@ -162,18 +162,25 @@ def test_upload_empty_file_without_chunks( assert final_file.read_bytes() == b"" -def test_start_non_empty_file_without_chunks_returns_400( +@pytest.mark.parametrize(("total_size", "total_chunks"), [(11, 0), (0, 2)]) +def test_start_with_chunks_that_do_not_match_the_size_returns_400( client: TestClient, access_token: str, platform: Platform, upload_fs: dict, + total_size: int, + total_chunks: int, ): response = _start_upload( - client, access_token, platform.id, total_size=11, total_chunks=0 + client, + access_token, + platform.id, + total_size=total_size, + total_chunks=total_chunks, ) assert response.status_code == status.HTTP_400_BAD_REQUEST - assert response.json()["detail"] == "A non-empty file needs at least one chunk" + assert response.json()["detail"] == "Chunk count does not match the file size" def test_upload_chunk_invalid_upload_id(client: TestClient, access_token: str):