Argus icon indicating copy to clipboard operation
Argus copied to clipboard

Api views should honour DEFAULT_PERMISSION_CLASSES

Open elfjes opened this issue 9 months ago • 10 comments

DRF has the DEFAULT_PERMISSION_CLASSES settting which Argus sets to:

 "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),

This is nice, so we can update this setting and add our custom authorization logic so that we can restrict access to certain users (<- this is something that we need)

Except that we can't... Most if not all Argus ApiView classes completely ignore the default and set their own. And most of the time they just set [IsAuthenticated], which just reimplements the default.

Now I could whip up a PR that makes the ApiViews honour the default permissions (and then add the required additional permissions such as IsOwner etc.). This would mean that the permission classes are unchanged as long as the DEFAULT_PERMISSION_CLASSES are kept to the default settings in argus.site.settings.base. However, I'm not sure if there are Argus instances that have a custom DEFAULT_PERMISSION_CLASSES setting that would reduce security. Is any of you more aware of what DEFAULT_PERMISSION_CLASSES other Argus instances are using and wether it would be safe to read from the settings instead of overriding them everywhere?

elfjes avatar May 22 '25 09:05 elfjes

I haven't heard of anyone else customizing as much as you. We could just flag the change very clearly.

hmpf avatar May 22 '25 09:05 hmpf

Removing explicit permission_classes when it is identical to DEFAULT_PERMISSION_CLASSES sounds like a good idea.

We could also add a howto on how to customize per existing view.

hmpf avatar May 22 '25 09:05 hmpf

We could also add a howto on how to customize per existing view.

That would also be interesting (as an additional option). How can you customize an existing view? The only thing I can think of is to subclass and then create a custom urlconf and use that, but that does not sound appealing. Is there another way?

elfjes avatar May 22 '25 09:05 elfjes

IsOwner assumes the model has a foreign key user pointing to User on it. It's only used by notificationprofile-views so I'm tempted to move that permission to argus.notificationprofile.drf.permissions.

hmpf avatar May 22 '25 09:05 hmpf

I don't know. It's nice to have them all in one place as much as possible

elfjes avatar May 22 '25 09:05 elfjes

We could also add a howto on how to customize per existing view.

That would also be interesting (as an additional option). How can you customize an existing view? The only thing I can think of is to subclass and then create a custom urlconf and use that, but that does not sound appealing. Is there another way?

No need to subclass, but you probably need a custom urlconf.

from rest_framework.permissions import IsAuthenticated

from argus.auth.views import CurrentUserView

from myargus.premssions import NeverOnSunday


CurrentUserView.permission_classes = [NeverOnSunday, IsAuthenticated]

hmpf avatar May 22 '25 09:05 hmpf

from rest_framework.permissions import IsAuthenticated

from argus.auth.views import CurrentUserView

from myargus.premssions import NeverOnSunday

CurrentUserView.permission_classes = [NeverOnSunday, IsAuthenticated]

I see... I'll keep that in mind for if we have to something exotic :D

elfjes avatar May 22 '25 09:05 elfjes

from rest_framework.permissions import IsAuthenticated from argus.auth.views import CurrentUserView from myargus.premssions import NeverOnSunday CurrentUserView.permission_classes = [NeverOnSunday, IsAuthenticated]

I see... I'll keep that in mind for if we have to something exotic :D

This... is... Python!!!

hmpf avatar May 22 '25 09:05 hmpf

Might be an idea to have a mixin-class that looks for the class attribute additional_permission_classes and sets premission_classes as DEFAULT_PERMISSION_CLASSES + additional_permission_classes.

hmpf avatar May 22 '25 09:05 hmpf

But seriously, we could have an IsStaff or IsMemberOfGroupChthulhu to make the API less open.

hmpf avatar May 22 '25 09:05 hmpf