Skip to content

Commit fa59ac1

Browse files
committed
WIP: feat: redirect uri wildcards
1 parent 631291d commit fa59ac1

File tree

5 files changed

+137
-22
lines changed

5 files changed

+137
-22
lines changed

docs/settings.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@ assigned ports.
6363
Note that you may override ``Application.get_allowed_schemes()`` to set this on
6464
a per-application basis.
6565

66+
ALLOW_REDIRECT_URI_WILDCARDS
67+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68+
69+
Default: ``False``
70+
71+
SECURITY WARNING: Enabling this setting can introduce security vulnerabilities. Only enable
72+
this setting if you understand the risks. https://datatracker.ietf.org/doc/html/rfc6749#section-3.1.2
73+
states "The redirection endpoint URI MUST be an absolute URI as defined by [RFC3986] Section 4.3." The
74+
intent of the URI restrictions is to prevent open redirects and phishing attacks. If you do enable this
75+
ensure that the wildcards restrict URIs to resources under your control. You are strongly encouragd not
76+
to use this feature in production.
77+
78+
When set to ``True``, the server will allow wildcard characters in the domains
79+
and paths for redirect_uris and post_logout_redirect_uris.
80+
81+
``*`` is the only wildcard character allowed.
82+
83+
``*`` can only be used as a prefix to a domain, must be the first character in
84+
the domain, and cannot be in the top or second level domain. Matching is done using an
85+
endsWith check.
86+
87+
For example,
88+
``https://*.example.com`` is allowed,
89+
``https://*-myproject.example.com`` is allowed,
90+
``https://*.sub.example.com`` is not allowed,
91+
``https://*.com`` is not allowed, and
92+
``https://example.*.com`` is not allowed.
93+
94+
``*`` can also be used as a suffix to a path, must be the last character in the path.
95+
Matching is done using a startsWith check.
96+
97+
For example,
98+
``https://example.com/*`` is allowed, and
99+
``https://example.com/path/*`` is allowed.
100+
101+
This feature is useful for working with CI service such as cloudflare, netlify, and vercel that offer branch
102+
deployments for development previews and user acceptance testing.
103+
66104
ALLOWED_SCHEMES
67105
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68106

oauth2_provider/models.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ def clean(self):
213213

214214
if redirect_uris:
215215
validator = AllowedURIValidator(
216-
allowed_schemes, name="redirect uri", allow_path=True, allow_query=True
216+
allowed_schemes,
217+
name="redirect uri",
218+
allow_path=True,
219+
allow_query=True,
220+
allow_hostname_wildcard=oauth2_settings.ALLOW_REDIRECT_URI_WILDCARDS,
221+
allow_path_wildcard=oauth2_settings.ALLOW_REDIRECT_URI_WILDCARDS,
217222
)
218223
for uri in redirect_uris:
219224
validator(uri)
@@ -782,35 +787,43 @@ def redirect_to_uri_allowed(uri, allowed_uris):
782787
for allowed_uri in allowed_uris:
783788
parsed_allowed_uri = urlparse(allowed_uri)
784789

790+
if parsed_allowed_uri.scheme != parsed_uri.scheme:
791+
# match failed, continue
792+
continue
793+
794+
""" check hostname """
795+
if oauth2_settings.ALLOW_REDIRECT_URI_WILDCARDS and parsed_allowed_uri.hostname.startswith("*"):
796+
""" wildcard hostname """
797+
if not parsed_uri.hostname.endswith(parsed_allowed_uri.hostname[1:]):
798+
continue
799+
elif parsed_allowed_uri.hostname != parsed_uri.hostname:
800+
continue
801+
785802
# From RFC 8252 (Section 7.3)
803+
# https://datatracker.ietf.org/doc/html/rfc8252#section-7.3
786804
#
787805
# Loopback redirect URIs use the "http" scheme
788806
# [...]
789807
# The authorization server MUST allow any port to be specified at the
790808
# time of the request for loopback IP redirect URIs, to accommodate
791809
# clients that obtain an available ephemeral port from the operating
792810
# system at the time of the request.
811+
allowed_uri_is_loopback = parsed_allowed_uri.scheme == "http" and parsed_allowed_uri.hostname in [
812+
"127.0.0.1",
813+
"::1",
814+
]
815+
""" check port """
816+
if not allowed_uri_is_loopback and parsed_allowed_uri.port != parsed_uri.port:
817+
continue
793818

794-
allowed_uri_is_loopback = (
795-
parsed_allowed_uri.scheme == "http"
796-
and parsed_allowed_uri.hostname in ["127.0.0.1", "::1"]
797-
and parsed_allowed_uri.port is None
798-
)
799-
if (
800-
allowed_uri_is_loopback
801-
and parsed_allowed_uri.scheme == parsed_uri.scheme
802-
and parsed_allowed_uri.hostname == parsed_uri.hostname
803-
and parsed_allowed_uri.path == parsed_uri.path
804-
) or (
805-
parsed_allowed_uri.scheme == parsed_uri.scheme
806-
and parsed_allowed_uri.netloc == parsed_uri.netloc
807-
and parsed_allowed_uri.path == parsed_uri.path
808-
):
809-
aqs_set = set(parse_qsl(parsed_allowed_uri.query))
810-
if aqs_set.issubset(uqs_set):
811-
return True
819+
""" check path """
820+
if parsed_allowed_uri.path != parsed_uri.path:
821+
continue
812822

813-
return False
823+
""" check querystring """
824+
aqs_set = set(parse_qsl(parsed_allowed_uri.query))
825+
if not aqs_set.issubset(uqs_set):
826+
continue # circuit break
814827

815828

816829
def is_origin_allowed(origin, allowed_origins):
@@ -833,4 +846,5 @@ def is_origin_allowed(origin, allowed_origins):
833846
and parsed_allowed_origin.netloc == parsed_origin.netloc
834847
):
835848
return True
849+
836850
return False

oauth2_provider/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"REQUEST_APPROVAL_PROMPT": "force",
7272
"ALLOWED_REDIRECT_URI_SCHEMES": ["http", "https"],
7373
"ALLOWED_SCHEMES": ["https"],
74+
"ALLOW_REDIRECT_URI_WILDCARDS": False,
7475
"OIDC_ENABLED": False,
7576
"OIDC_ISS_ENDPOINT": "",
7677
"OIDC_USERINFO_ENDPOINT": "",

oauth2_provider/validators.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from django.core.validators import URLValidator
66
from django.utils.encoding import force_str
77

8+
from .settings import oauth2_settings
9+
810

911
class URIValidator(URLValidator):
1012
scheme_re = r"^(?:[a-z][a-z0-9\.\-\+]*)://"
@@ -21,7 +23,16 @@ class URIValidator(URLValidator):
2123
class AllowedURIValidator(URIValidator):
2224
# TODO: find a way to get these associated with their form fields in place of passing name
2325
# TODO: submit PR to get `cause` included in the parent class ValidationError params`
24-
def __init__(self, schemes, name, allow_path=False, allow_query=False, allow_fragments=False):
26+
def __init__(
27+
self,
28+
schemes,
29+
name,
30+
allow_path=False,
31+
allow_query=False,
32+
allow_fragments=False,
33+
allow_hostname_wildcard=False,
34+
allow_path_wildcard=False,
35+
):
2536
"""
2637
:param schemes: List of allowed schemes. E.g.: ["https"]
2738
:param name: Name of the validated URI. It is required for validation message. E.g.: "Origin"
@@ -34,6 +45,7 @@ def __init__(self, schemes, name, allow_path=False, allow_query=False, allow_fra
3445
self.allow_path = allow_path
3546
self.allow_query = allow_query
3647
self.allow_fragments = allow_fragments
48+
self.allow_hostname_wildcard = allow_hostname_wildcard or True
3749

3850
def __call__(self, value):
3951
value = force_str(value)
@@ -68,8 +80,56 @@ def __call__(self, value):
6880
params={"name": self.name, "value": value, "cause": "path not allowed"},
6981
)
7082

83+
if (
84+
oauth2_settings.ALLOW_REDIRECT_URI_WILDCARDS
85+
and self.allow_hostname_wildcard
86+
and "*" in netloc
87+
):
88+
domain_parts = netloc.split(".")
89+
if netloc.count("*") > 1:
90+
raise ValidationError(
91+
"%(name)s URI validation error. %(cause)s: %(value)s",
92+
params={
93+
"name": self.name,
94+
"value": value,
95+
"cause": "only one wildcard is allowed in the hostname",
96+
},
97+
)
98+
if not netloc.startswith("*"):
99+
raise ValidationError(
100+
"%(name)s URI validation error. %(cause)s: %(value)s",
101+
params={
102+
"name": self.name,
103+
"value": value,
104+
"cause": "wildcards must be at the beginning of the hostname",
105+
},
106+
)
107+
if len(domain_parts) < 3:
108+
raise ValidationError(
109+
"%(name)s URI validation error. %(cause)s: %(value)s",
110+
params={
111+
"name": self.name,
112+
"value": value,
113+
"cause": "wildcards cannot be in the top level or second level domain",
114+
},
115+
)
116+
117+
# strip the wildcard from the netloc, we'll reassamble the value later to pass to URI Validator
118+
if netloc.startswith("*."):
119+
netloc = netloc[2:]
120+
else:
121+
netloc = netloc[1:]
122+
123+
# we stripped the wildcard from the netloc and path if they were allowed and present since they would
124+
# fail validation we'll reassamble the URI to pass to the URIValidator
125+
reassambled_uri = f"{scheme}://{netloc}{path}"
126+
if query:
127+
reassambled_uri += f"?{query}"
128+
if fragment:
129+
reassambled_uri += f"#{fragment}"
130+
71131
try:
72-
super().__call__(value)
132+
super().__call__(reassambled_uri)
73133
except ValidationError as e:
74134
raise ValidationError(
75135
"%(name)s URI validation error. %(cause)s: %(value)s",

tests/test_oidc_views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ def test_validate_logout_request(oidc_tokens, public_application, rp_settings):
299299
post_logout_redirect_uri="http://other.org",
300300
)
301301

302+
# TODO: test wildcards
303+
302304

303305
@pytest.mark.django_db(databases=retrieve_current_databases())
304306
@pytest.mark.parametrize("ALWAYS_PROMPT", [True, False])

0 commit comments

Comments
 (0)