| 1 |
import sys |
| 2 |
from http import HTTPStatus |
| 3 |
|
| 4 |
from django.core.exceptions import PermissionDenied |
| 5 |
from django.http import HttpResponseRedirect |
| 6 |
from django.urls import reverse |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from allauth.account.adapter import DefaultAccountAdapter |
| 11 |
from allauth.core.exceptions import ImmediateHttpResponse |
| 12 |
|
| 13 |
|
| 14 |
class PreLoginRedirectAccountAdapter(DefaultAccountAdapter): |
| 15 |
def pre_login(self, *args, **kwargs): |
| 16 |
raise ImmediateHttpResponse(HttpResponseRedirect("/foo")) |
| 17 |
|
| 18 |
|
| 19 |
def test_adapter_pre_login(settings, user, user_password, client): |
| 20 |
settings.ACCOUNT_ADAPTER = ( |
| 21 |
"tests.apps.account.test_adapter.PreLoginRedirectAccountAdapter" |
| 22 |
) |
| 23 |
resp = client.post( |
| 24 |
reverse("account_login"), |
| 25 |
{"login": user.username, "password": user_password}, |
| 26 |
) |
| 27 |
assert resp.status_code == HTTPStatus.FOUND |
| 28 |
assert resp["location"] == "/foo" |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize( |
| 32 |
"x_forwarded_for,expected_ip", |
| 33 |
[ |
| 34 |
("", None), |
| 35 |
("192.168.1.1", "192.168.1.1"), |
| 36 |
(" 192.168.1.1", "192.168.1.1"), |
| 37 |
("192.168.1.1 ", "192.168.1.1"), |
| 38 |
(" 192.168.1.1:12345 ", "192.168.1.1"), |
| 39 |
(" [2001:db8::1:0]:12345 ", "2001:db8::1:0"), |
| 40 |
("192.168.1.1:12345", "192.168.1.1"), |
| 41 |
( |
| 42 |
"2001:0db8:85a3:0000:0000:8a2e:0370:7334", |
| 43 |
"2001:db8:85a3::8a2e:370:7334", |
| 44 |
), |
| 45 |
( |
| 46 |
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:12345", |
| 47 |
"2001:db8:85a3::8a2e:370:7334", |
| 48 |
), |
| 49 |
("2001:db8::1:0", "2001:db8::1:0"), |
| 50 |
("[2001:db8::1:0]:12345", "2001:db8::1:0"), |
| 51 |
("::abc:7:def", "::abc:7:def"), |
| 52 |
("::1", "::1"), |
| 53 |
("[::1]:12345", "::1"), |
| 54 |
] |
| 55 |
+ ( |
| 56 |
[ |
| 57 |
# not supported by ipaddress py3.8 |
| 58 |
("fe80::1234%1", "fe80::1234%1"), |
| 59 |
] |
| 60 |
if sys.version_info >= (3, 9) |
| 61 |
else [] |
| 62 |
), |
| 63 |
) |
| 64 |
def test_get_client_ip_vs_x_forwarded_for(rf, x_forwarded_for, expected_ip, settings): |
| 65 |
settings.ALLAUTH_TRUSTED_PROXY_COUNT = 1 |
| 66 |
request = rf.get("/", HTTP_X_FORWARDED_FOR=x_forwarded_for) |
| 67 |
ip = DefaultAccountAdapter(request=request).get_client_ip(request) |
| 68 |
assert ip == (expected_ip if expected_ip else request.META["REMOTE_ADDR"]) |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.parametrize( |
| 72 |
"x_forwarded_for", |
| 73 |
[ |
| 74 |
" ", |
| 75 |
"999.999.999.999", |
| 76 |
"[999.999.999.999]", |
| 77 |
"999.999.999.999:12345", |
| 78 |
"[999.999.999.999]:12345", |
| 79 |
"[192.0.0.2]", |
| 80 |
"[192.0.0.2]:12345", |
| 81 |
"192.[0].0.2", |
| 82 |
"192.[0].0.2:12345", |
| 83 |
"192.[0.0].2:12345", |
| 84 |
"[192.[0.0].2:12345", |
| 85 |
"2001:db8::1:0:12345", |
| 86 |
"[2001:db8::1:0:12345]", |
| 87 |
"2001:0db8:85a3:0000:0000:8a2e:0370:12345", |
| 88 |
"[2001:0db8:85a3:0000:0000:8a2e:0370]:7334:12345", |
| 89 |
"XwDxNk4JQhUft')) OR 18=(SELECT 18 FROM PG_SLEEP(15))--", |
| 90 |
], |
| 91 |
) |
| 92 |
def test_get_client_ip_vs_invalid_x_forwarded_for(rf, x_forwarded_for, settings): |
| 93 |
settings.ALLAUTH_TRUSTED_PROXY_COUNT = 1 |
| 94 |
request = rf.get("/", HTTP_X_FORWARDED_FOR=x_forwarded_for) |
| 95 |
with pytest.raises(PermissionDenied): |
| 96 |
DefaultAccountAdapter(request=request).get_client_ip(request) |