Skip to content

Commit 0de024e

Browse files
BB2-4326: Throw 404 when non-UUID passed to authorize endpoint (#1440)
* BB2-4326: Throw 404 if invalid uuid passed to authorize endpoint * Clean up tests
1 parent ae54284 commit 0de024e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

apps/dot_ext/tests/test_authorization.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.test import Client
1414
from unittest.mock import patch, MagicMock
1515
from urllib.parse import parse_qs, urlencode, urlparse
16+
import uuid
1617
from waffle.testutils import override_switch
1718

1819
from apps.test import BaseApiTest
@@ -1123,3 +1124,37 @@ def test_permission_denied_raised_for_refresh_token_app_not_in_flag(
11231124

11241125
with self.assertRaises(AccessDeniedTokenCustomError):
11251126
view_instance.validate_v3_token_call(request)
1127+
1128+
def test_invalid_uuid_authorize_call(self):
1129+
"""BB2-4326: Ensure a 404 is thrown if a non-UUID is passed to an authorize endpoint
1130+
"""
1131+
auth_uri_v1 = reverse("oauth2_provider:authorize-instance", args=['jolokia'])
1132+
auth_uri_v2 = reverse("oauth2_provider_v2:authorize-instance-v2", args=['jolokia'])
1133+
auth_uri_v3 = reverse("oauth2_provider_v3:authorize-instance-v3", args=['jolokia'])
1134+
1135+
response_v1 = self.client.get(auth_uri_v1)
1136+
response_v2 = self.client.get(auth_uri_v2)
1137+
response_v3 = self.client.get(auth_uri_v3)
1138+
1139+
assert response_v1.status_code == HTTPStatus.NOT_FOUND
1140+
assert response_v2.status_code == HTTPStatus.NOT_FOUND
1141+
assert response_v3.status_code == HTTPStatus.NOT_FOUND
1142+
1143+
@override_switch('v3_endpoints', active=True)
1144+
def test_valid_uuid_authorize_call(self):
1145+
"""BB2-4326: Ensure a 302 is thrown if a valid UUID is passed to an authorize endpoint
1146+
"""
1147+
auth_uri_v1 = reverse("oauth2_provider:authorize-instance", args=[uuid.uuid4()])
1148+
auth_uri_v2 = reverse("oauth2_provider_v2:authorize-instance-v2", args=[uuid.uuid4()])
1149+
auth_uri_v3 = reverse("oauth2_provider_v3:authorize-instance-v3", args=[uuid.uuid4()])
1150+
1151+
response_v1 = self.client.get(auth_uri_v1)
1152+
response_v2 = self.client.get(auth_uri_v2)
1153+
response_v3 = self.client.get(auth_uri_v3)
1154+
1155+
assert response_v1.status_code == HTTPStatus.FOUND
1156+
assert response_v2.status_code == HTTPStatus.FOUND
1157+
# The behavior is different for v3, as we check v3 authorize calls to see if the application is in
1158+
# the v3_early_adopter flag (part of BB2-4250). Because all of the mocks are not included in this test
1159+
# such that the authorize call will return a 302 for v3, v3 in this test throws a 403
1160+
assert response_v3.status_code == HTTPStatus.FORBIDDEN

apps/dot_ext/views/authorization.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from oauthlib.oauth2 import AccessDeniedError
3131
from oauthlib.oauth2.rfc6749.errors import InvalidClientError, InvalidGrantError, InvalidRequestError
3232
from urllib.parse import urlparse, parse_qs
33+
import uuid
3334
import html
3435
from apps.dot_ext.scopes import CapabilitiesScopes
3536
import apps.logging.request_logger as bb2logging
@@ -395,7 +396,20 @@ def __init__(self, version):
395396
self.version = version
396397
super().__init__(version=version)
397398

399+
def is_valid_uuid(self, value: str) -> bool:
400+
try:
401+
uuid.UUID(str(value))
402+
return True
403+
except ValueError:
404+
return False
405+
398406
def dispatch(self, request, uuid, *args, **kwargs):
407+
# BB2-4326: If we do not receive a valid uuid in the authorize call, throw a 404
408+
if not self.is_valid_uuid(uuid):
409+
return JsonResponse(
410+
{'status_code': 404, 'message': 'Not found.'},
411+
status=404,
412+
)
399413

400414
# Get auth_uuid to set again after super() return. It gets cleared out otherwise.
401415
auth_flow_dict = get_session_auth_flow_trace(request)

0 commit comments

Comments
 (0)