Self hosted FLOSS fitness/workout, nutrition and weight tracker
Restrict access to routine logs and stats to owners only
Roland Geider47d646c24178
diff --git a/wger/manager/api/permissions.py b/wger/manager/api/permissions.pyindex 167d81f28..d9790642b 100644--- a/wger/manager/api/permissions.py+++ b/wger/manager/api/permissions.py@@ -27,6 +27,11 @@ class RoutinePermission(permissions.BasePermission):return request.user and request.user.is_authenticated+ # Routine actions that expose the owner's personal training history+ # rather than the public template structure. They must stay+ # owner-only, even on public templates.+ OWNER_ONLY_ACTIONS = frozenset({'logs', 'stats'})+def has_object_permission(self, request, view, obj):"""If the routine is a public template, allow read-only access for everyone.@@ -35,7 +40,7 @@ class RoutinePermission(permissions.BasePermission):if obj.user == request.user:return True- if obj.is_template:+ if obj.is_template and getattr(view, 'action', None) not in self.OWNER_ONLY_ACTIONS:return request.method in permissions.SAFE_METHODSreturn Falsediff --git a/wger/manager/tests/test_routine.py b/wger/manager/tests/test_routine.pyindex bd2f9ed05..3b30802bd 100644--- a/wger/manager/tests/test_routine.py+++ b/wger/manager/tests/test_routine.py@@ -15,6 +15,9 @@# Standard Libraryimport datetime+# Django+from django.urls import reverse+# wgerfrom wger.core.tests.api_base_test import ApiBaseResourceTestCasefrom wger.core.tests.base_testcase import WgerTestCase@@ -505,3 +508,55 @@ class RoutineApiTestCase(ApiBaseResourceTestCase):'start': '2024-03-11','end': '2024-06-20',}+++class RoutineLogsAndStatsScopeTestCase(WgerTestCase):+ """+ The /logs/ and /stats/ actions of a routine return the owner's+ private workout history. They must never be accessible to anyone but+ the routine owner, even when the routine is a public template.+ """++ PUBLIC_TEMPLATE_PK = 5+ # owned by trainer2 (user 5), is_template + is_public++ @property+ def logs_url(self):+ return reverse('routine-logs', kwargs={'pk': self.PUBLIC_TEMPLATE_PK})++ @property+ def stats_url(self):+ return reverse('routine-stats', kwargs={'pk': self.PUBLIC_TEMPLATE_PK})++ @property+ def detail_url(self):+ return reverse('routine-detail', kwargs={'pk': self.PUBLIC_TEMPLATE_PK})++ def test_non_owner_cannot_read_template_logs(self):+ self.user_login('test')+ response = self.client.get(self.logs_url)+ self.assertEqual(response.status_code, 403)++ def test_non_owner_cannot_read_template_stats(self):+ self.user_login('test')+ response = self.client.get(self.stats_url)+ self.assertEqual(response.status_code, 403)++ def test_owner_can_read_own_template_logs(self):+ self.user_login('trainer2')+ response = self.client.get(self.logs_url)+ self.assertEqual(response.status_code, 200)++ def test_owner_can_read_own_template_stats(self):+ self.user_login('trainer2')+ response = self.client.get(self.stats_url)+ self.assertEqual(response.status_code, 200)++ def test_template_structure_remains_publicly_readable(self):+ """+ The template's exercise structure is the public part of the+ feature — only logs/stats need to be restricted.+ """+ self.user_login('test')+ response = self.client.get(self.detail_url)+ self.assertEqual(response.status_code, 200)