Compare the DCR initial access token as bytes so a non-ASCII token gets a 401, not a 500

hmac.compare_digest raises TypeError when two str values hold non-ASCII
characters, and the bearer value comes from the request.
This commit is contained in:
Jeonghun Mun committed 2026-11-08 08:46:34 +00:00
1 parent 34f6066187
commit 97d5c05a3c
2 files changed
+13 -1

No files matched your search

+1 -1
View File
@@ -88,7 +88,7 @@ def _dcr_initial_access_token_ok(request):
scheme, _, value = header.partition(" ")
if scheme.lower() != "bearer" or not value:
return False
return hmac.compare_digest(value, expected)
return hmac.compare_digest(value.encode(), expected.encode())
@require_http_methods(["GET"])
+12
View File
@@ -224,6 +224,18 @@ class DynamicClientRegistrationInitialAccessTokenTests(TestCase):
self.assertEqual(response.json()["error"], "invalid_token")
self.assertEqual(Application.objects.count(), 0)
def test_rejects_registration_with_non_ascii_initial_access_token(self):
response = self.client.post(
reverse("oauth-dynamic-client-registration"),
data=json.dumps({"redirect_uris": ["http://127.0.0.1:8765/callback"]}),
content_type="application/json",
HTTP_AUTHORIZATION="Bearer s3cret-iat\u00e9",
)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.json()["error"], "invalid_token")
self.assertEqual(Application.objects.count(), 0)
def test_allows_registration_with_initial_access_token(self):
response = self.client.post(
reverse("oauth-dynamic-client-registration"),