Skip to content

Commit 9babc13

Browse files
committed
fix: redirecting to unvalidated redirect_uri on InvalidScopeError in OIDC grants
1 parent 0dc0e5b commit 9babc13

5 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎authlib/oidc/core/grants/hybrid.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,20 @@ def save_authorization_code(self, code, request):
4949
raise NotImplementedError()
5050

5151
def validate_authorization_request(self):
52-
if not is_openid_scope(self.request.payload.scope):
53-
raise InvalidScopeError(
54-
"Missing 'openid' scope",
55-
redirect_uri=self.request.payload.redirect_uri,
56-
redirect_fragment=True,
57-
)
5852
self.register_hook(
5953
"after_validate_authorization_request_payload",
6054
lambda grant, redirect_uri: validate_nonce(
6155
grant.request, grant.exists_nonce, required=True
6256
),
6357
)
64-
return validate_code_authorization_request(self)
58+
redirect_uri = validate_code_authorization_request(self)
59+
if not is_openid_scope(self.request.payload.scope):
60+
raise InvalidScopeError(
61+
"Missing 'openid' scope",
62+
redirect_uri=redirect_uri,
63+
redirect_fragment=True,
64+
)
65+
return redirect_uri
6566

6667
def create_granted_params(self, grant_user):
6768
self.request.user = grant_user

‎authlib/oidc/core/grants/implicit.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ def get_audiences(self, request):
8282
return [client.get_client_id()]
8383

8484
def validate_authorization_request(self):
85+
redirect_uri = super().validate_authorization_request()
8586
if not is_openid_scope(self.request.payload.scope):
8687
raise InvalidScopeError(
8788
"Missing 'openid' scope",
88-
redirect_uri=self.request.payload.redirect_uri,
89+
redirect_uri=redirect_uri,
8990
redirect_fragment=True,
9091
)
91-
redirect_uri = super().validate_authorization_request()
9292
try:
9393
validate_nonce(self.request, self.exists_nonce, required=True)
9494
except OAuth2Error as error:

‎docs/changelog.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ Changelog
66

77
Here you can see the full list of changes between each Authlib release.
88

9+
Version 1.6.12
10+
--------------
11+
12+
**Unreleased**
13+
14+
- Fix redirecting to unvalidated ``redirect_uri`` on ``InvalidScopeError``
15+
in ``OpenIDImplicitGrant`` and ``OpenIDHybridGrant``.
16+
917
Version 1.6.11
1018
--------------
1119

‎tests/flask/test_oauth2/test_openid_hybrid_grant.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ def test_invalid_scope(test_client):
160160
assert "error=invalid_scope" in rv.location
161161

162162

163+
def test_missing_openid_in_scope_does_not_redirect_to_unregistered_uri(test_client):
164+
"""An unregistered redirect_uri must not be followed even when openid scope is missing."""
165+
rv = test_client.post(
166+
"/oauth/authorize",
167+
data={
168+
"client_id": "client-id",
169+
"response_type": "code id_token",
170+
"state": "s",
171+
"nonce": "n",
172+
"scope": "profile",
173+
"redirect_uri": "https://evil.example.com/phish",
174+
},
175+
)
176+
assert rv.status_code == 400
177+
assert rv.headers.get("Location") is None
178+
179+
163180
def test_access_denied(test_client):
164181
rv = test_client.post(
165182
"/oauth/authorize",

‎tests/flask/test_oauth2/test_openid_implict_grant.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ def test_missing_openid_in_scope(test_client):
107107
assert "error=invalid_scope" in rv.location
108108

109109

110+
def test_missing_openid_in_scope_does_not_redirect_to_unregistered_uri(test_client):
111+
"""An unregistered redirect_uri must not be followed even when openid scope is missing."""
112+
rv = test_client.post(
113+
"/oauth/authorize",
114+
data={
115+
"response_type": "id_token token",
116+
"client_id": "client-id",
117+
"scope": "profile",
118+
"state": "s",
119+
"nonce": "n",
120+
"redirect_uri": "https://evil.example.com/phish",
121+
},
122+
)
123+
assert rv.status_code == 400
124+
assert rv.headers.get("Location") is None
125+
126+
110127
def test_denied(test_client):
111128
rv = test_client.post(
112129
"/oauth/authorize",

0 commit comments

Comments
 (0)