From 9583d6d96f02d3ee105c73a47084661feef3e811 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 13:10:46 +0000 Subject: [PATCH] fix(profile): address code review feedback - early size check, CSRF helper, test constants Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> --- app/api/profile.py | 8 ++++++++ frontend/templates/profile.html | 32 ++++++++++++++------------------ tests/test_api_profile.py | 33 +++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/app/api/profile.py b/app/api/profile.py index eb3b0e5b..a12324d8 100644 --- a/app/api/profile.py +++ b/app/api/profile.py @@ -231,6 +231,14 @@ async def upload_avatar( detail=f"Unsupported image type '{content_type}'. Allowed: JPEG, PNG, GIF, WebP.", ) + # Check declared size first (available when the client sends a Content-Length header) + if file.size is not None and file.size > _MAX_AVATAR_BYTES: + raise HTTPException( + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, + detail="Avatar image must be 2 MB or smaller.", + ) + + # Read up to one byte past the limit so we can detect oversized uploads raw = await file.read(_MAX_AVATAR_BYTES + 1) if len(raw) > _MAX_AVATAR_BYTES: raise HTTPException( diff --git a/frontend/templates/profile.html b/frontend/templates/profile.html index 75c04d39..cd506cfb 100644 --- a/frontend/templates/profile.html +++ b/frontend/templates/profile.html @@ -77,8 +77,8 @@

- Upload a JPEG, PNG, GIF, or WebP image up to 2 MB. - If no custom picture is set, your Gravatar is shown. + Upload a JPEG, PNG, GIF, or WebP image up to 2 MB. + If no custom picture is set, your Gravatar is shown.