Skip to content

Commit ec4c7a1

Browse files
authored
Merge commit from fork
1 parent d74dc90 commit ec4c7a1

6 files changed

Lines changed: 138 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from .implicit import OpenIDImplicitGrant
1010
from .util import is_openid_scope
11+
from .util import validate_form_post_redirect_uri
1112
from .util import validate_nonce
1213

1314
log = logging.getLogger(__name__)
@@ -56,6 +57,7 @@ def validate_authorization_request(self):
5657
),
5758
)
5859
redirect_uri = validate_code_authorization_request(self)
60+
validate_form_post_redirect_uri(self, redirect_uri)
5961
if not is_openid_scope(self.request.scope):
6062
raise InvalidScopeError(
6163
"Missing 'openid' scope",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .util import create_half_hash
1616
from .util import create_response_mode_response
1717
from .util import is_openid_scope
18+
from .util import validate_form_post_redirect_uri
1819
from .util import validate_nonce
1920
from .util import validate_request_prompt
2021

@@ -69,6 +70,7 @@ def get_audiences(self, request):
6970

7071
def validate_authorization_request(self):
7172
redirect_uri = super().validate_authorization_request()
73+
validate_form_post_redirect_uri(self, redirect_uri)
7274
if not is_openid_scope(self.request.scope):
7375
raise InvalidScopeError(
7476
"Missing 'openid' scope",

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from authlib.common.encoding import to_native
77
from authlib.common.urls import add_params_to_uri
88
from authlib.common.urls import quote_url
9+
from authlib.common.urls import urlparse
910
from authlib.oauth2.rfc6749 import InvalidRequestError
1011
from authlib.oauth2.rfc6749 import scope_to_list
1112

@@ -51,6 +52,28 @@ def validate_request_prompt(grant, redirect_uri, redirect_fragment=False):
5152
return grant
5253

5354

55+
def validate_form_post_redirect_uri(grant, redirect_uri):
56+
"""Reject the ``form_post`` response mode for a non-HTTP redirect URI.
57+
58+
With ``form_post``, the user-agent submits an HTML form to the redirect
59+
URI. A ``javascript:`` redirect URI would then run a script in the
60+
origin of the authorization server.
61+
62+
:param grant: the grant that validates the authorization request
63+
:param redirect_uri: the redirect URI of the authorization request
64+
:raises InvalidRequestError: if the redirect URI scheme is not http or https
65+
"""
66+
response_mode = grant.request.payload.data.get(
67+
"response_mode", grant.DEFAULT_RESPONSE_MODE
68+
)
69+
if response_mode != "form_post":
70+
return
71+
if urlparse.urlparse(redirect_uri).scheme not in ("http", "https"):
72+
raise InvalidRequestError(
73+
"The 'form_post' response mode requires an http or https redirect URI."
74+
)
75+
76+
5477
def validate_nonce(request, exists_nonce, required=False):
5578
nonce = request.payload.data.get("nonce")
5679
if not nonce:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Security
2+
~~~~~~~~
3+
4+
- The OpenID Connect implicit and hybrid grants refuse ``response_mode=form_post``
5+
when the redirect URI does not use the ``http`` or ``https`` scheme. They
6+
return an ``invalid_request`` error and do not redirect. A client with a
7+
``javascript:`` redirect URI could run a script in the origin of the
8+
authorization server, even when the user denied the authorization.
9+
`GHSA-gp2m-377p-m4j3 <https://github.com/authlib/authlib/security/advisories/GHSA-gp2m-377p-m4j3>`_

‎tests/flask/test_oauth2/test_openid_hybrid_grant.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,54 @@ def test_response_mode_form_post(test_client):
405405
assert b'name="code"' in rv.data
406406
assert b'name="id_token"' in rv.data
407407
assert b'name="access_token"' in rv.data
408+
409+
410+
@pytest.mark.parametrize(
411+
"redirect_uri",
412+
[
413+
"javascript://client.test/%0Aalert(1)",
414+
"JavaScript://client.test/\nalert(1)",
415+
"com.example.app://client.test/callback",
416+
],
417+
)
418+
@pytest.mark.parametrize("user_id", ["1", None])
419+
@pytest.mark.parametrize("default_response_mode", [False, True])
420+
def test_response_mode_form_post_non_http_redirect_uri(
421+
test_client, db, client, monkeypatch, redirect_uri, user_id, default_response_mode
422+
):
423+
"""A form_post response must not target a non-HTTP redirect URI.
424+
425+
The request is refused before any form is rendered, whether the user
426+
approves or denies the authorization.
427+
"""
428+
client.set_client_metadata(
429+
{
430+
"redirect_uris": [redirect_uri],
431+
"scope": "openid profile address",
432+
"response_types": ["code id_token", "code token", "code id_token token"],
433+
"grant_types": ["authorization_code"],
434+
"id_token_signed_response_alg": "HS256",
435+
}
436+
)
437+
db.session.add(client)
438+
db.session.commit()
439+
440+
data = {
441+
"client_id": "client-id",
442+
"response_type": "code id_token token",
443+
"response_mode": "form_post",
444+
"state": "bar",
445+
"nonce": "abc",
446+
"scope": "openid profile",
447+
"redirect_uri": redirect_uri,
448+
}
449+
if user_id:
450+
data["user_id"] = user_id
451+
if default_response_mode:
452+
monkeypatch.setattr(_OpenIDHybridGrant, "DEFAULT_RESPONSE_MODE", "form_post")
453+
del data["response_mode"]
454+
rv = test_client.post("/oauth/authorize", data=data)
455+
assert rv.status_code == 400
456+
assert rv.json["error"] == "invalid_request"
457+
assert rv.location is None
458+
assert b"<form" not in rv.data

‎tests/flask/test_oauth2/test_openid_implict_grant.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,57 @@ def test_response_mode_form_post(test_client):
269269
assert b'name="state"' in rv.data
270270

271271

272+
@pytest.mark.parametrize(
273+
"redirect_uri",
274+
[
275+
"javascript://client.test/%0Aalert(1)",
276+
"JavaScript://client.test/\nalert(1)",
277+
"com.example.app://client.test/callback",
278+
],
279+
)
280+
@pytest.mark.parametrize("user_id", ["1", None])
281+
@pytest.mark.parametrize("default_response_mode", [False, True])
282+
def test_response_mode_form_post_non_http_redirect_uri(
283+
test_client, db, client, monkeypatch, redirect_uri, user_id, default_response_mode
284+
):
285+
"""A form_post response must not target a non-HTTP redirect URI.
286+
287+
The request is refused before any form is rendered, whether the user
288+
approves or denies the authorization.
289+
"""
290+
client.set_client_metadata(
291+
{
292+
"redirect_uris": [redirect_uri],
293+
"scope": "openid profile",
294+
"token_endpoint_auth_method": "none",
295+
"response_types": ["id_token", "id_token token"],
296+
"id_token_signed_response_alg": "HS256",
297+
}
298+
)
299+
db.session.add(client)
300+
db.session.commit()
301+
302+
data = {
303+
"response_type": "id_token",
304+
"response_mode": "form_post",
305+
"client_id": "client-id",
306+
"scope": "openid profile",
307+
"state": "bar",
308+
"nonce": "abc",
309+
"redirect_uri": redirect_uri,
310+
}
311+
if user_id:
312+
data["user_id"] = user_id
313+
if default_response_mode:
314+
monkeypatch.setattr(_OpenIDImplicitGrant, "DEFAULT_RESPONSE_MODE", "form_post")
315+
del data["response_mode"]
316+
rv = test_client.post("/oauth/authorize", data=data)
317+
assert rv.status_code == 400
318+
assert rv.json["error"] == "invalid_request"
319+
assert rv.location is None
320+
assert b"<form" not in rv.data
321+
322+
272323
def test_client_metadata_custom_alg(test_client, app, db, client):
273324
"""If the client metadata 'id_token_signed_response_alg' is defined,
274325
it should be used to sign id_tokens."""

0 commit comments

Comments
 (0)