Skip to content

Commit 29fbe66

Browse files
authored
Merge pull request #729 from azmeuk/714-redirect-uri-fragments
Forbid URL fragments in redirect_uris
2 parents 2f1f971 + c68daba commit 29fbe66

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

authlib/common/urls.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def extract_params(raw):
139139
return None
140140

141141

142-
def is_valid_url(url):
142+
def is_valid_url(url: str, fragments_allowed=True):
143143
parsed = urlparse.urlparse(url)
144-
return parsed.scheme and parsed.hostname
144+
return (
145+
parsed.scheme and parsed.hostname and (fragments_allowed or not parsed.fragment)
146+
)

authlib/oauth2/rfc7591/claims.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def validate_software_version(self):
217217
def _validate_uri(self, key, uri=None):
218218
if uri is None:
219219
uri = self.get(key)
220-
if uri and not is_valid_url(uri):
220+
if uri and not is_valid_url(uri, fragments_allowed=False):
221221
raise InvalidClaimError(key)
222222

223223
@classmethod

docs/changelog.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ Version 1.5.2
1212

1313
**Unreleased**
1414

15+
- Forbid fragments in ``redirect_uris``. :issue:`714`
1516
- Fix invalid characters in ``error_description``. :issue:`720`
16-
- Add ``claims_cls``` parameter for client's ``parse_id_token`` method.
17+
- Add ``claims_cls``` parameter for client's ``parse_id_token`` method. :issue:`725`
1718

1819

1920
Version 1.5.1

tests/flask/test_oauth2/test_client_registration_endpoint.py

+31
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,34 @@ def test_require_auth_time(self):
654654
rv = self.client.post("/create_client", json=body, headers=self.headers)
655655
resp = json.loads(rv.data)
656656
self.assertIn(resp["error"], "invalid_client_metadata")
657+
658+
def test_redirect_uri(self):
659+
"""RFC6749 indicate that fragments are forbidden in redirect_uri.
660+
661+
The redirection endpoint URI MUST be an absolute URI as defined by
662+
[RFC3986] Section 4.3. [...] The endpoint URI MUST NOT include a
663+
fragment component.
664+
665+
https://www.rfc-editor.org/rfc/rfc6749#section-3.1.2
666+
"""
667+
self.prepare_data()
668+
669+
# Nominal case
670+
body = {
671+
"redirect_uris": ["https://client.test"],
672+
"client_name": "Authlib",
673+
}
674+
rv = self.client.post("/create_client", json=body, headers=self.headers)
675+
resp = json.loads(rv.data)
676+
self.assertIn("client_id", resp)
677+
self.assertEqual(resp["client_name"], "Authlib")
678+
self.assertEqual(resp["redirect_uris"], ["https://client.test"])
679+
680+
# Error case
681+
body = {
682+
"redirect_uris": ["https://client.test#fragment"],
683+
"client_name": "Authlib",
684+
}
685+
rv = self.client.post("/create_client", json=body, headers=self.headers)
686+
resp = json.loads(rv.data)
687+
self.assertIn(resp["error"], "invalid_client_metadata")

0 commit comments

Comments
 (0)