Skip to content

Commit

Permalink
Fix POST requests slipping through AdminOrReadOnlyVisible
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAda committed Oct 8, 2023
1 parent 528e496 commit 0131bb9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/backend/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def has_object_permission(self, request, view, obj):
return True
return request.user.is_authenticated and obj.is_visible and request.method in permissions.SAFE_METHODS

def has_permission(self, request, view):
if request.method not in permissions.SAFE_METHODS:
return request.user.is_staff and not request.user.should_deny_admin()
return request.user.is_authenticated


class AdminOrReadOnly(permissions.BasePermission):
def has_permission(self, request, view):
Expand Down
10 changes: 10 additions & 0 deletions src/member/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ def test_patch_member_admin(self):
)
self.assertEqual(response.status_code, HTTP_200_OK)

def test_unauthenticated_post_request_is_rejected(self):
self.admin_user.is_visible = True
self.admin_user.save()
self.client.force_authenticate(self.user)
response = self.client.post(
reverse("member-list"),
data={"username": "test2"},
)
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)


class UserIPTest(APITestCase):
def test_not_authenticated(self):
Expand Down

0 comments on commit 0131bb9

Please sign in to comment.