Skip to content

Commit ea18897

Browse files
committed
Handle invalid hex values in query strings in DRF extension
1 parent 102c851 commit ea18897

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)
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
fromcollectionsimportOrderedDict
22

3+
fromdjango.core.exceptionsimportSuspiciousOperation
34
fromrest_framework.authenticationimportBaseAuthentication
45

56
from ...oauth2_backendsimportget_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+
ifrequestisNone:
28+
returnNone
2629
oauthlib_core=get_oauthlib_core()
27-
valid, r=oauthlib_core.verify_request(request, scopes=[])
28-
ifvalid:
29-
returnr.user, r.access_token
30+
try:
31+
valid, r=oauthlib_core.verify_request(request, scopes=[])
32+
exceptValueErroraserror:
33+
ifstr(error) =="Invalid hex encoding in query string.":
34+
raiseSuspiciousOperation(error)
35+
raise
36+
else:
37+
ifvalid:
38+
returnr.user, r.access_token
3039
request.oauth2_error=getattr(r, "oauth2_error",{})
3140
returnNone
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+
deftest_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)