-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When specifying a single required group name as a string, we mistakenly iterate each character of the string as a valid group name! If groups were named a certain way, this could have made for an access issue. Oops. Thankfully, there was never an issue. Use stricter typing too, which would have caught this.
- Loading branch information
Showing
7 changed files
with
61 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.contrib.auth.models import Group | ||
from django.core.exceptions import PermissionDenied | ||
from django.http import HttpRequest, HttpResponse | ||
from django.test import RequestFactory, TestCase | ||
|
||
from ws import decorators | ||
from ws.tests import factories | ||
|
||
|
||
class GroupRequiredTest(TestCase): | ||
def test_single_group_required(self) -> None: | ||
"""Reproduce a bug that only happened if groups had name collisions. | ||
Specifically, we accidentally used substring comparison when we | ||
meant to do exact string matching! | ||
If a view was marked as requiring a *single* group, we accidentally | ||
failed to convert that one string to a *collection* of strings, but | ||
because Python strings *are* collections of strings... it "worked." | ||
We never had access issues becaus the few groups we use are unique. | ||
""" | ||
docs = Group.objects.create(name="leaders_with_medical_degrees") | ||
# Not a real group, but using this to demonstrate a bug! | ||
leaders = Group.objects.get(name="leaders") | ||
|
||
@decorators.group_required("leaders_with_medical_degrees") | ||
def mds_only(request: HttpRequest) -> HttpResponse: | ||
return HttpResponse() | ||
|
||
participant = factories.ParticipantFactory.create() | ||
leaders.user_set.add(participant.user) | ||
|
||
request = RequestFactory().get("/") | ||
request.user = participant.user | ||
request.participant = participant # type: ignore[attr-defined] | ||
|
||
with self.assertRaises(PermissionDenied): | ||
mds_only(request) | ||
|
||
docs.user_set.add(participant.user) | ||
mds_only(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters