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: fix the owner-role endpoint import, hide internal errors (ADM-01)

Omega (QA)c7a0a7550623
diff --git a/backend/admin_routes.py b/backend/admin_routes.py
index b07cada..45e4fd4 100644
--- a/backend/admin_routes.py
+++ b/backend/admin_routes.py
@@ -351,17 +351,23 @@ def transfer_ownership():
def fix_owner_role():
"""Temporary endpoint to manually fix owner role - can be removed after fix"""
try:
- # Import ensure_owner_exists if available
+ # ensure_owner_exists lives in the package __init__, not in
+ # db_handler. Importing it from db_handler raised ImportError
+ # for every caller, and the fallback import then raised
+ # ModuleNotFoundError, so this endpoint 500d for every
+ # authorized caller (ADM-01). Imported here rather than at
+ # module level to avoid a circular import with the package
+ # __init__, which imports this blueprint.
try:
- from .db_handler import ensure_owner_exists
+ from . import ensure_owner_exists
except ImportError:
- from db_handler import ensure_owner_exists
-
+ from backend import ensure_owner_exists
+
ensure_owner_exists()
return jsonify({'success': True, 'message': 'Owner role fix attempted. Check logs for results.'}), 200
except Exception as e:
logger.error(f"Error in fix_owner_role endpoint: {e}")
- return jsonify({'success': False, 'message': f'Error: {str(e)}'}), 500
+ return jsonify({'success': False, 'message': 'Failed to fix the owner role. Check the server logs for details.'}), 500
# ============================
# Site Settings Routes
diff --git a/tests/test_admin_fix_owner.py b/tests/test_admin_fix_owner.py
new file mode 100644
index 0000000..37d1ace
--- /dev/null
+++ b/tests/test_admin_fix_owner.py
@@ -0,0 +1,45 @@
+"""ADM-01: POST /api/admin/fix-owner-role imported ensure_owner_exists
+from db_handler, where it does not live, so it returned 500 for every
+authorized caller, with the internal error text in the body. The
+function lives in the package __init__; the endpoint must succeed,
+and failures must not disclose internal error text.
+"""
+
+
+def _promote(db_connection, username):
+ with db_connection, db_connection.cursor() as cur:
+ cur.execute("UPDATE users SET is_admin = TRUE WHERE username = %s",
+ (username,))
+
+
+def test_fix_owner_role_succeeds_for_admin(client, admin_user, db_connection):
+ _promote(db_connection, admin_user["username"])
+ r = client.post("/api/admin/fix-owner-role",
+ headers=admin_user["headers"], json={})
+ assert r.status_code == 200, r.get_data(as_text=True)
+ assert r.get_json()["success"] is True
+
+
+def test_fix_owner_role_rejects_non_admin(client, user_factory):
+ user = user_factory()
+ r = client.post("/api/admin/fix-owner-role",
+ headers=user["headers"], json={})
+ assert r.status_code == 403
+
+
+def test_fix_owner_role_failure_hides_internal_error(
+ client, admin_user, db_connection, monkeypatch):
+ _promote(db_connection, admin_user["username"])
+
+ def boom():
+ raise RuntimeError("s3cret-db-host.internal:5432 exploded")
+
+ import backend
+ monkeypatch.setattr(backend, "ensure_owner_exists", boom)
+ r = client.post("/api/admin/fix-owner-role",
+ headers=admin_user["headers"], json={})
+ assert r.status_code == 500
+ body = r.get_data(as_text=True)
+ assert "s3cret-db-host.internal" not in body
+ assert "exploded" not in body
+ assert r.get_json()["success"] is False