Skip to content

Handle invalid hex values in query strings in DRF extension #1444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Kristian Rune Larsen
Lazaros Toumanidis
Ludwig Hähne
Łukasz Skarżyński
Madison Swain-Bowden
Marcus Sonestedt
Matias Seniquiel
Michael Howitz
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* #1425 Remove deprecated `RedirectURIValidator`, `WildcardSet` per #1345; `validate_logout_request` per #1274

### Fixed
* #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.
### Security

## [2.4.0] - 2024-05-13
Expand Down
15 changes: 12 additions & 3 deletions oauth2_provider/contrib/rest_framework/authentication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import OrderedDict

from django.core.exceptions import SuspiciousOperation
from rest_framework.authentication import BaseAuthentication

from ...oauth2_backends import get_oauthlib_core
Expand All @@ -23,10 +24,18 @@ def authenticate(self, request):
Returns two-tuple of (user, token) if authentication succeeds,
or None otherwise.
"""
if request is None:
return None
oauthlib_core = get_oauthlib_core()
valid, r = oauthlib_core.verify_request(request, scopes=[])
if valid:
return r.user, r.access_token
try:
valid, r = oauthlib_core.verify_request(request, scopes=[])
except ValueError as error:
if str(error) == "Invalid hex encoding in query string.":
raise SuspiciousOperation(error)
raise
else:
if valid:
return r.user, r.access_token
request.oauth2_error = getattr(r, "oauth2_error", {})
return None

Expand Down
6 changes: 6 additions & 0 deletions tests/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,9 @@ def test_authentication_none(self):
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-authentication-none/", HTTP_AUTHORIZATION=auth)
self.assertEqual(response.status_code, 401)

def test_invalid_hex_string_in_query(self):
auth = self._create_authorization_header(self.access_token.token)
response = self.client.get("/oauth2-test/?q=73%%20of%20Arkansans", HTTP_AUTHORIZATION=auth)
# Should respond with a 400 rather than raise a ValueError
self.assertEqual(response.status_code, 400)
Loading