From d88d06d43428a6d9ebd3c797b266d76ecb9fea29 Mon Sep 17 00:00:00 2001 From: Mark Yu Date: Wed, 24 Apr 2019 22:25:37 +0800 Subject: [PATCH] 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. --- rest_framework/permissions.py | 5 +++-- tests/test_permissions.py | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_framework/permissions.py b/rest_framework/permissions.py index 3a8c58064..a906fd089 100644 --- a/rest_framework/permissions.py +++ b/rest_framework/permissions.py @@ -78,8 +78,9 @@ class OR: 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/tests/test_permissions.py b/tests/test_permissions.py index a016089d7..19aba56a3 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -615,7 +615,6 @@ class PermissionsCompositionTests(TestCase): ) assert composed_perm().has_object_permission(request, None, None) is False - @pytest.mark.skipif(not PY36, reason="assert_called_once() not available") def test_or_lazyness(self): request = factory.get('/1', format='json') request.user = AnonymousUser()