[bug] Require only 'view' permission to read model metadata (#13348)

ContentTypePermission works out 'view' for safe methods and 'change'
otherwise, but its fallback for models without check_permission()
(e.g. Part, StockItem) still passed a hardcoded 'change'. Reading
metadata via the generic metadata endpoint therefore required 'change'
permission, unlike the model's own detail endpoint, which needs 'view'.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Leon KiesgenandClaude Opus 5.5 authored and GitHub committed 2026-11-24 09:27:17 +11:00
1 parent adfd393c92
commit 3744e74671
2 files changed
+20 -1

No files matched your search

@@ -570,6 +570,6 @@ class ContentTypePermission(OASTokenMixin, permissions.BasePermission):
)
return users.permissions.check_user_permission(
request.user, model_class, 'change'
request.user, model_class, permission
)
return False
+19
View File
@@ -3460,6 +3460,25 @@ class GenericMetadataAuthorizationTests(InvenTreeAPITestCase):
self.assignRole('part.change')
self.patch(url, {'metadata': {'x': 1}}, expected_code=200)
def test_part_metadata_requires_view_to_read_and_change_to_write(self):
"""Part metadata is scoped by 'view'/'change' on Part itself.
Part has RuleSet permissions of its own and no check_permission(), so
reads must require 'view' only, like the Part detail endpoint.
"""
url = self._metadata_url('part', self.part.pk)
self.get(url, expected_code=403)
self.patch(url, {'metadata': {'x': 1}}, expected_code=403)
self.assignRole('part.view')
self.get(url, expected_code=200)
self.patch(url, {'metadata': {'x': 1}}, expected_code=403)
self.put(url, {'metadata': {'x': 1}}, expected_code=403)
self.assignRole('part.change')
self.patch(url, {'metadata': {'x': 1}}, expected_code=200)
def test_project_code_metadata_requires_staff_for_write(self):
"""ProjectCode metadata mirrors the staff-only write restriction of its own endpoint."""
url = self._metadata_url('projectcode', self.project_code.pk)