From 67adfa0daf5d30038596e74f8728129d7c387beb Mon Sep 17 00:00:00 2001 From: David Smith <39445562+smithdc1@users.noreply.github.com> Date: Wed, 21 Sep 2022 12:19:33 +0100 Subject: [PATCH] Change semantic of OR of two permission classes (#7522) * Change semantic of OR of two permission classes The original semantic of OR is defined as: the request pass either of the two has_permission() check, and pass either of the two has_object_permission() check, which could lead to situations that a request passes has_permission() but fails on has_object_permission() of Permission Class A, fails has_permission() but passes has_object_permission() of Permission Class B, passes the OR permission check. This should not be the desired permission check semantic in applications, because such a request should fail on either Permission Class (on Django object permission) alone, but passes the OR or the two. My code fix this by changing the semantic so that the request has to pass either class's has_permission() and has_object_permission() to get the Django object permission of the OR check. * Update rest_framework/permissions.py * Update setup.cfg Co-authored-by: Mark Yu Co-authored-by: Tom Christie --- rest_framework/permissions.py | 7 +++++-- setup.cfg | 2 +- tests/test_permissions.py | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/rest_framework/permissions.py b/rest_framework/permissions.py index 3a8c5806465..16459b251f4 100644 --- a/rest_framework/permissions.py +++ b/rest_framework/permissions.py @@ -78,8 +78,11 @@ def has_permission(self, request, view): def has_object_permission(self, request, view, obj): return ( - self.op1.has_object_permission(request, view, obj) or - self.op2.has_object_permission(request, view, obj) + self.op1.has_permission(request, view) + and self.op1.has_object_permission(request, view, obj) + ) or ( + self.op2.has_permission(request, view) + and self.op2.has_object_permission(request, view, obj) ) diff --git a/setup.cfg b/setup.cfg index 1c85a69e4ae..294e9afdd62 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,7 +5,7 @@ license_files = LICENSE.md addopts=--tb=short --strict-markers -ra [flake8] -ignore = E501,W504 +ignore = E501,W503,W504 banned-modules = json = use from rest_framework.utils import json! [isort] diff --git a/tests/test_permissions.py b/tests/test_permissions.py index 4e6cae4b811..f00b57ec19f 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -635,7 +635,7 @@ def test_object_or_lazyness(self): composed_perm = (permissions.IsAuthenticated | permissions.AllowAny) hasperm = composed_perm().has_object_permission(request, None, None) assert hasperm is True - assert mock_deny.call_count == 1 + assert mock_deny.call_count == 0 assert mock_allow.call_count == 1 def test_and_lazyness(self): @@ -677,3 +677,16 @@ def test_object_and_lazyness(self): assert hasperm is False assert mock_deny.call_count == 1 mock_allow.assert_not_called() + + def test_unimplemented_has_object_permission(self): + "test for issue 6402 https://github.com/encode/django-rest-framework/issues/6402" + request = factory.get('/1', format='json') + request.user = AnonymousUser() + + class IsAuthenticatedUserOwner(permissions.IsAuthenticated): + def has_object_permission(self, request, view, obj): + return True + + composed_perm = (IsAuthenticatedUserOwner | permissions.IsAdminUser) + hasperm = composed_perm().has_object_permission(request, None, None) + assert hasperm is False