Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Return 404 instead of 500 for missing file downloads

Omega (QA)bb247b4e6fcc
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5efb030..14d00c0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@
- _Files: `frontend/js/components/tagManager.js`_
### Fixed
+- **Missing file download 500 fixed:** `GET /api/files/<missing>` returned HTTP 500 because `serve_file` let `send_from_directory`'s NotFound fall into the generic 500 handler. It now checks existence up front and returns a JSON 404 ("File not found"), matching the `/secure-file/` route. Non-owners still get 403 without leaking file existence. Found by the daily QA sweep.
+ - _Files: `backend/file_routes.py`
- **Same-second file-upload collision fixed:** stored upload filenames used a second-resolution timestamp only, so two uploads of the same filename within one second shared a storage path and the second overwrote the first. Filenames now include a random suffix (`unique_upload_filename()` helper, applied to invoice/manual/photo/other-document uploads in both add and update). Found by the daily QA sweep.
- _Files: `backend/warranties_routes.py`
- **Oversized uploads now return HTTP 413 instead of 500/dropped connection:** files over `MAX_CONTENT_LENGTH` raised `RequestEntityTooLarge`, which was swallowed into 500s by the generic `except` blocks in the warranty write endpoints and the `token_required` decorator. Added an app-wide 413 handler returning a JSON error naming the configured limit, and both layers now let HTTP exceptions propagate. Found by the daily QA sweep.
diff --git a/backend/file_routes.py b/backend/file_routes.py
index afec4ba..b8666b7 100644
--- a/backend/file_routes.py
+++ b/backend/file_routes.py
@@ -112,6 +112,10 @@ def serve_file(filename):
# Remove 'uploads/' prefix for send_from_directory
file_path = filename[8:] if filename.startswith('uploads/') else filename
+ full_path = os.path.join(current_app.config['UPLOAD_FOLDER'], file_path)
+ if not os.path.isfile(full_path):
+ return jsonify({"message": "File not found"}), 404
+
return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_path)
except Exception as e:
logger.error(f"Error serving file {filename}: {e}")