| 1 |
from django.core.exceptions import ValidationError |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from allauth.idp.oidc.internal.clientkit import ( |
| 6 |
_wildcard_to_regex, |
| 7 |
is_origin_allowed, |
| 8 |
is_redirect_uri_allowed, |
| 9 |
) |
| 10 |
from allauth.idp.oidc.models import Client |
| 11 |
|
| 12 |
|
| 13 |
URI_TEST_CASES = [ |
| 14 |
(False, "https://example.com", True), |
| 15 |
(False, "https://*.example.com", False), |
| 16 |
(True, "https://example.com", True), |
| 17 |
(True, "https://*.projectname.pages.dev/", True), |
| 18 |
(True, "https://*--projectname-com.netlify.app", True), |
| 19 |
(True, "http://*.localhost:3000", True), |
| 20 |
(True, "https://example.com/callback", True), |
| 21 |
(True, "https://*.*.example.com", False), |
| 22 |
(True, "http*://example.com", False), |
| 23 |
(True, "https://example.com/callback/*", False), |
| 24 |
] |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize( |
| 28 |
("allow_wildcards", "uri", "should_pass"), |
| 29 |
URI_TEST_CASES, |
| 30 |
) |
| 31 |
def test_redirect_uri_wildcard_validation(allow_wildcards, uri, should_pass): |
| 32 |
client = Client( |
| 33 |
id="test-client", |
| 34 |
name="Test Client", |
| 35 |
allow_uri_wildcards=allow_wildcards, |
| 36 |
redirect_uris=uri, |
| 37 |
) |
| 38 |
|
| 39 |
if should_pass: |
| 40 |
client.clean_redirect_uris() |
| 41 |
else: |
| 42 |
with pytest.raises(ValidationError): |
| 43 |
client.clean_redirect_uris() |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.parametrize( |
| 47 |
("allow_wildcards", "origin", "should_pass"), |
| 48 |
URI_TEST_CASES, |
| 49 |
) |
| 50 |
def test_cors_origin_wildcard_validation(allow_wildcards, origin, should_pass): |
| 51 |
client = Client( |
| 52 |
id="test-client", |
| 53 |
name="Test Client", |
| 54 |
allow_uri_wildcards=allow_wildcards, |
| 55 |
cors_origins=origin, |
| 56 |
) |
| 57 |
|
| 58 |
if should_pass: |
| 59 |
client.clean_cors_origins() |
| 60 |
else: |
| 61 |
with pytest.raises(ValidationError): |
| 62 |
client.clean_cors_origins() |
| 63 |
|
| 64 |
|
| 65 |
def test_wildcards_disabled_by_default(): |
| 66 |
client = Client( |
| 67 |
id="test-client", |
| 68 |
name="Test Client", |
| 69 |
redirect_uris="https://example.com", |
| 70 |
) |
| 71 |
|
| 72 |
assert client.allow_uri_wildcards is False |
| 73 |
|
| 74 |
client.redirect_uris = "https://*.example.com" |
| 75 |
with pytest.raises(ValidationError): |
| 76 |
client.clean_redirect_uris() |
| 77 |
|
| 78 |
|
| 79 |
def test_wildcard_matching_enforcement(): |
| 80 |
client = Client( |
| 81 |
id="test-client", |
| 82 |
name="Test Client", |
| 83 |
allow_uri_wildcards=True, |
| 84 |
redirect_uris="https://*.example.com/callback", |
| 85 |
cors_origins="https://*.example.com", |
| 86 |
) |
| 87 |
|
| 88 |
assert is_redirect_uri_allowed( |
| 89 |
"https://subdomain.example.com/callback", |
| 90 |
client.get_redirect_uris(), |
| 91 |
client.allow_uri_wildcards, |
| 92 |
) |
| 93 |
assert not is_redirect_uri_allowed( |
| 94 |
"https://evil-example.com/callback", |
| 95 |
client.get_redirect_uris(), |
| 96 |
client.allow_uri_wildcards, |
| 97 |
) |
| 98 |
|
| 99 |
assert is_origin_allowed( |
| 100 |
"https://subdomain.example.com", |
| 101 |
client.get_cors_origins(), |
| 102 |
client.allow_uri_wildcards, |
| 103 |
) |
| 104 |
|
| 105 |
assert not is_origin_allowed( |
| 106 |
"https://evil-example.com", |
| 107 |
client.get_cors_origins(), |
| 108 |
client.allow_uri_wildcards, |
| 109 |
) |
| 110 |
|
| 111 |
assert not is_origin_allowed( |
| 112 |
"https://user:[email protected]", |
| 113 |
client.get_cors_origins(), |
| 114 |
client.allow_uri_wildcards, |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
def test_wildcard_to_regex(): |
| 119 |
pattern = _wildcard_to_regex("*.example.com") |
| 120 |
assert pattern.pattern == r"^[^.]+\.example\.com$" |
| 121 |
|
| 122 |
assert pattern.match("api.example.com") |
| 123 |
assert not pattern.match("foo.api.example.com") # * should not match dots |
| 124 |
assert not pattern.match("evil-example.com") |
| 125 |
|
| 126 |
# verify dots aren't treated as wildcards |
| 127 |
assert not pattern.match("api$example$com") |