Fix : Add file size validation for CSV import endpoint - #682
Open
Athrv-D wants to merge 1 commit into
Open
Conversation
Contributor
📝 WalkthroughWalkthroughThe CSV import endpoint now rejects files larger than 10 MB before processing. A test verifies the HTTP 400 response, error message, and absence of created import jobs. ChangesCSV upload validation
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ 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 |
Contributor
There was a problem hiding this comment.
🧹 Nitpick comments (1)
backend/leads/views.py (1)
103-106: 🩺 Stability & Availability | 🔵 TrivialConsider Celery broker payload limits and decoding safety for large files.
- Broker payload limits: While capping the upload size at 10 MB mitigates extreme memory consumption on the web thread, sending up to 10 MB of string payload directly as a Celery task argument downstream (
import_leads_from_csv.delay(...)) can still exceed message broker limits (e.g., Redis or RabbitMQ) and degrade queue performance. Consider persisting the file to object storage or a temporary location and passing a reference to the task instead.- Decoding safety: Just below the size check,
file_obj.read().decode('utf-8')executes without error handling. If a user uploads a non-UTF-8 file (e.g., a binary file renamed to.csv), this will throw aUnicodeDecodeErrorand result in an unhandled 500 error.🤖 Prompt for 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. In `@backend/leads/views.py` around lines 103 - 106, Update the CSV upload flow around the size check and import_leads_from_csv.delay call to avoid placing the full file contents in the Celery broker: persist the uploaded file to temporary or object storage and pass only its reference to the task. Wrap the file_obj.read().decode("utf-8") operation with handling for UnicodeDecodeError and return a client-facing 400 response for invalid UTF-8 uploads.
🤖 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.
Nitpick comments:
In `@backend/leads/views.py`:
- Around line 103-106: Update the CSV upload flow around the size check and
import_leads_from_csv.delay call to avoid placing the full file contents in the
Celery broker: persist the uploaded file to temporary or object storage and pass
only its reference to the task. Wrap the file_obj.read().decode("utf-8")
operation with handling for UnicodeDecodeError and return a client-facing 400
response for invalid UTF-8 uploads.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 5684a457-a82c-4a40-8330-a3f7b3c4240c
📒 Files selected for processing (2)
backend/leads/tests.pybackend/leads/views.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request
🔗 Related Issue
Closes #327
📝 Summary of Changes
🏷️ Type of Change
🧪 Testing
Steps to test:
1 .Upload CSV file larger than 10 MB
2. Verify the API returns HTTP 400 with expected error message.
3. Upload a CSV file smaller than 10 MB and verify it is accepted.
4. Run: python manage.py test leads.tests
📸 Screenshots (if applicable)
✅ Checklist
Summary by CodeRabbit