Self hosted FLOSS fitness/workout, nutrition and weight tracker
Properly check request with url_has_allowed_host_and_scheme before redirecting
Roland Geidercfa27a7fc78c
diff --git a/wger/core/middleware.py b/wger/core/middleware.pyindex fb069cdb1..a6f2c44c2 100644--- a/wger/core/middleware.py+++ b/wger/core/middleware.py@@ -27,6 +27,7 @@ from django.contrib.auth import (from django.shortcuts import redirectfrom django.urls import reversefrom django.utils.deprecation import MiddlewareMixin+from django.utils.http import url_has_allowed_host_and_scheme# wgerfrom wger.utils.helpers import remove_language_code@@ -140,7 +141,13 @@ class AuthProxyHeaderMiddleware(MiddlewareMixin):f"AuthProxyMiddleware: User '{username}' authenticated via header from "f"trusted IP '{client_ip}'.")- next_url = request.GET.get('next', reverse('core:dashboard'))+ next_url = request.GET.get('next')+ if not next_url or not url_has_allowed_host_and_scheme(+ next_url,+ allowed_hosts={request.get_host()},+ require_https=request.is_secure(),+ ):+ next_url = reverse('core:dashboard')if request.user.is_authenticated:return redirect(next_url)else:diff --git a/wger/core/tests/test_auth_proxy_middleware.py b/wger/core/tests/test_auth_proxy_middleware.pyindex 6f9b27887..de4f1e0cd 100644--- a/wger/core/tests/test_auth_proxy_middleware.py+++ b/wger/core/tests/test_auth_proxy_middleware.py@@ -220,3 +220,36 @@ class AuthProxyMiddlewareTests(TestCase):response_exact = self.make_request(TRUSTED_IP, USERNAME)self.assertEqual(response_exact.status_code, 200)self.assertEqual(int(self.client.session.get('_auth_user_id', 0)), self.existing_user.pk)++ @override_settings(+ AUTH_PROXY_TRUSTED_IPS=[TRUSTED_IP],+ AUTH_PROXY_HEADER=PROXY_HEADER_KEY,+ WGER_SETTINGS={'ALLOW_GUEST_USERS': False},+ )+ def test_external_next_is_not_followed(self):+ """An external ``next`` URL must not be honored after proxy auth."""+ response = self.client.get(+ self.login_url,+ {'next': 'https://evil.example.com/'},+ REMOTE_ADDR=TRUSTED_IP,+ **{PROXY_HEADER_KEY: USERNAME},+ )+ self.assertEqual(response.status_code, 302)+ self.assertEqual(response['Location'], self.protected_url)++ @override_settings(+ AUTH_PROXY_TRUSTED_IPS=[TRUSTED_IP],+ AUTH_PROXY_HEADER=PROXY_HEADER_KEY,+ WGER_SETTINGS={'ALLOW_GUEST_USERS': False},+ )+ def test_safe_local_next_is_followed(self):+ """A same-host ``next`` URL is still honored after proxy auth."""+ safe_next = f'http://testserver{self.protected_url}'+ response = self.client.get(+ self.login_url,+ {'next': safe_next},+ REMOTE_ADDR=TRUSTED_IP,+ **{PROXY_HEADER_KEY: USERNAME},+ )+ self.assertEqual(response.status_code, 302)+ self.assertEqual(response['Location'], safe_next)