Skip to content

Commit 9c18de2

Browse files
AetherUnboundn2ygk
andauthored
Handle invalid hex values in query strings in DRF extension (#1444)
* Handle invalid hex values in query strings in DRF extension --------- Co-authored-by: Alan Crosswell <alan@crosswell.us>
1 parent ba75297 commit 9c18de2

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Kristian Rune Larsen
8383
Lazaros Toumanidis
8484
Ludwig Hähne
8585
Łukasz Skarżyński
86+
Madison Swain-Bowden
8687
Marcus Sonestedt
8788
Matias Seniquiel
8889
Michael Howitz

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
* #1425 Remove deprecated `RedirectURIValidator`, `WildcardSet` per #1345; `validate_logout_request` per #1274
2323

2424
### Fixed
25+
* #1443 Query strings with invalid hex values now raise a SuspiciousOperation exception (in DRF extension) instead of raising a 500 ValueError: Invalid hex encoding in query string.
2526
### Security
2627

2728
## [2.4.0] - 2024-05-13

oauth2_provider/contrib/rest_framework/authentication.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict
22

3+
from django.core.exceptions import SuspiciousOperation
34
from rest_framework.authentication import BaseAuthentication
45

56
from ...oauth2_backends import get_oauthlib_core
@@ -23,10 +24,18 @@ def authenticate(self, request):
2324
Returns two-tuple of (user, token) if authentication succeeds,
2425
or None otherwise.
2526
"""
27+
if request is None:
28+
return None
2629
oauthlib_core = get_oauthlib_core()
27-
valid, r = oauthlib_core.verify_request(request, scopes=[])
28-
if valid:
29-
return r.user, r.access_token
30+
try:
31+
valid, r = oauthlib_core.verify_request(request, scopes=[])
32+
except ValueError as error:
33+
if str(error) == "Invalid hex encoding in query string.":
34+
raise SuspiciousOperation(error)
35+
raise
36+
else:
37+
if valid:
38+
return r.user, r.access_token
3039
request.oauth2_error = getattr(r, "oauth2_error", {})
3140
return None
3241

tests/test_rest_framework.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,9 @@ def test_authentication_none(self):
415415
auth = self._create_authorization_header(self.access_token.token)
416416
response = self.client.get("/oauth2-authentication-none/", HTTP_AUTHORIZATION=auth)
417417
self.assertEqual(response.status_code, 401)
418+
419+
def test_invalid_hex_string_in_query(self):
420+
auth = self._create_authorization_header(self.access_token.token)
421+
response = self.client.get("/oauth2-test/?q=73%%20of%20Arkansans", HTTP_AUTHORIZATION=auth)
422+
# Should respond with a 400 rather than raise a ValueError
423+
self.assertEqual(response.status_code, 400)

0 commit comments

Comments
 (0)