Validate total attachment size#2953
Conversation
- Added validation to `files/rest.py::create_file` to ensure that the newly requested file's size + existing attachment size total does not exceed the 6MB limit.
There was a problem hiding this comment.
Pull request overview
Adds server-side validation to the template file upload endpoint to cap the combined size of attachments for a template at 6MB, with accompanying tests to verify expected 400/201 behavior (including archived-file handling).
Changes:
- Introduce
MAX_TOTAL_FILE_SIZE(6MB) and reject uploads when existing attachments + requested size exceed the limit. - Return a structured 400 response payload including current usage, requested size, and limit.
- Add unit tests covering over-limit, exact-limit, single-file over-limit, and archived-file exclusion cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
app/files/rest.py |
Adds total-size validation logic and a new size-limit constant in create_file. |
tests/app/files/test_rest.py |
Adds test coverage for the new total-size validation scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| raise InvalidRequest( | ||
| { | ||
| "error": "Total file size exceeds the 6MB limit.", | ||
| "current_usage": existing_size, | ||
| "requested": actual_file_size, | ||
| "limit": MAX_TOTAL_FILE_SIZE, | ||
| }, | ||
| status_code=400, | ||
| ) | ||
|
|
There was a problem hiding this comment.
This seems to be throwing an uncaught exception in API instead of return a 400 to ADMIN.
There was a problem hiding this comment.
You could try something like:
return jsonify({"error": "over_file_limit"}), 400
We'll need to come up with a format there so ADMIN can read it on the other side.
There was a problem hiding this comment.
Updated! Let me know what you think
Summary | Résumé
This PR adds additional defensive validation to
files/rest.py::create_filethat ensures total file attachment size does not exceed 6MB. It takes the size of the newly requested file + existing file attachments into account. TheInvalidRequestraised returns data to aid in constructing an appropriate error message on the front end.Related Issues | Cartes liées
Bug bash issue: https://docs.google.com/document/d/1m_EzXNJbqjJH-WLzxRKMtO4k5CAHwk51uCXi7xJQR44/edit?tab=t.0#task=NbqwOoo-dIWUUrb0
Test instructions | Instructions pour tester la modification
Release Instructions | Instructions pour le déploiement
None.
Reviewer checklist | Liste de vérification du réviseur