-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hard to use "NOT" bitwise operator for permissions due to default values #6598
Comments
My issue is related I think (#6596). In general I think the split in two methods ( The first Everything would be a lot easier if there is a single method, which optionally receives an object, in such a way that there is only a single point where the permissions are checked. Should be doable, but it will break backwards compatibility. |
Grrr. This has come up a few times. Basic issue is people not implementing both methods. I’m inclined towards something like raising a warning if a permission is composed but doesn’t implement both methods. (And otherwise just leaving the previous behaviour as is.) |
Usually, I'd want to only negate object or view -wide permissions, not both at the same time: Taking the example of a group permission to access a view, regardless of the object:
Negating this with ~ ends up in has_object_permission always returning False, so any detail=True action gets 403-ed. Returning False for has_object_permission breaks the permission for the non-negated case. Am I missing the use-case ? |
It may just be that in this use-case you need to define a custom permission... but yes, let’s have a think about what if anything we can do to make this work. |
I think the general problem is in combining the logical combinators with the two-method approach. As it is I wouldn't use them and define custom permissions instead. In any case, the logical combinators are perhaps not even considered public API (since it's not referenced in the docs)? Was it recently removed? Still I think a single method is easier. E.g. can we define |
If you are not implementing class MyPermission(BasePermission):
def has_permission(self, request, view):
[...]
return result
def has_object_permission(self, request, view, obj):
return self.has_permission(request, view) |
This is not an issue per se, but a consequence of a new behavior and I would like to open up discussions on it.
#6361 recently added support for bitwise not (aka ~) to negate permissions.
Due to how permissions are shared for both view and object checks, relying on calling the correct method (has_permission or has_object_permission), the default value "True" becomes False for any negative check, making the permission extremely restrictive.
Example:
Calling retrieve will call get_object, which in turns calls has_object_permission, returning False (not True), ending the call into a 403.
Im currently circumventing this by specifying which of has_permission or has_object_permission should get negated, but that makes the ~ operator nearly unusable for generic views (it's still very useful if you only need one of the two functions!).
The docs mention that not can be used in the context of an APIView which is what caught me offguard. Maybe a clarification on how it works is needed?
Thanks.
The text was updated successfully, but these errors were encountered: