Skip to content

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

Security: serve UI document route with no-store and nosniff (FILE-01)

Omega (QA)2ebf73d386f3
diff --git a/backend/file_routes.py b/backend/file_routes.py
index f452915..a97f421 100644
--- a/backend/file_routes.py
+++ b/backend/file_routes.py
@@ -145,7 +145,15 @@ def serve_file(filename):
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)
+ # FILE-01: these bytes are authorization-gated, so they must not
+ # sit in a shared cache for a year (the Flask default) and must
+ # not be content-sniffed. Match the /secure-file/ headers.
+ response = send_from_directory(current_app.config['UPLOAD_FOLDER'], file_path)
+ response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
+ response.headers['Pragma'] = 'no-cache'
+ response.headers['Expires'] = '0'
+ response.headers['X-Content-Type-Options'] = 'nosniff'
+ return response
except Exception as e:
logger.error(f"Error serving file {filename}: {e}")
return jsonify({"message": "Error accessing file"}), 500
diff --git a/tests/test_file_access.py b/tests/test_file_access.py
index 7824d2b..943391d 100644
--- a/tests/test_file_access.py
+++ b/tests/test_file_access.py
@@ -109,3 +109,13 @@ def test_missing_file_returns_json_404(client, files_scenario):
def test_file_requires_auth(client, files_scenario):
r = client.get(f"/api/files/{files_scenario.paths['invoice_path']}")
assert r.status_code == 401
+
+
+def test_served_file_is_not_cached_and_not_sniffed(client, files_scenario):
+ # FILE-01: the route the UI uses must match /secure-file/: no-store
+ # plus nosniff, not the one-year public cache Flask defaults to.
+ r = _get(client, files_scenario.owner, files_scenario.paths['invoice_path'])
+ assert r.status_code == 200
+ assert 'no-store' in r.headers.get('Cache-Control', '')
+ assert 'max-age=31536000' not in r.headers.get('Cache-Control', '')
+ assert r.headers.get('X-Content-Type-Options') == 'nosniff'