fix: enforce 10 MB size limit on avatar uploads#658
Conversation
upload_profile_avatar previously read the entire file into memory with await file.read() and no size check, making it trivial to exhaust server memory with an oversized upload. Switch to a chunked read loop that rejects payloads above 10 MB with HTTP 413, consistent with the sample upload (50 MB) and audio import (200 MB) endpoints that already enforce their own limits.
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe avatar upload handler is refactored to stream chunks of the request body into a temporary file instead of reading the entire upload into memory. Two constants define the 10 MB max file size and 1 MB chunk size. The handler rejects uploads exceeding the limit with HTTP 413, derives the temp filename suffix from the original upload filename (defaulting to ChangesAvatar Upload Streaming
🎯 2 (Simple) | ⏱️ ~10 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@backend/routes/profiles.py`:
- Line 239: The code currently sets suffix = Path(file.filename or "").suffix or
".png" without validating it; add an allowed image extensions set (e.g.,
ALLOWED_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp"}) and
normalize suffix to lowercase, then check that suffix is in that set before
continuing. If the suffix is missing or not allowed, respond with an error
(raise HTTPException / return 400) instead of defaulting to ".png". Update the
upload handler that references file.filename and the suffix variable to perform
this check and reject disallowed extensions.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 431b82ef-3f23-4441-8fc2-b523bb7b271e
📒 Files selected for processing (1)
backend/routes/profiles.py
Reject uploads with unsupported extensions (e.g. .exe, .php) with a 400 rather than accepting any extension. Only .png, .jpg, .jpeg, .gif, .webp, .bmp, and .svg are allowed; missing extension defaults to .png.
|
Addressed the CodeRabbit review comment: Validate uploaded file extensions: Added an |
The avatar upload endpoint previously had no server-side size check. An oversized image would be fully read into memory before anything rejected it — a trivial vector for memory exhaustion.
This adds an explicit 10 MB limit enforced before the file is read, returning a clear 413 error to the client. Consistent with the upload size guard applied elsewhere in the codebase.
Summary by CodeRabbit