fix: Failed to switch languages

pull/15272/head
wangruidong 2025-07-29 18:05:25 +08:00 committed by 老广
parent e46aa95980
commit 1406437d4e
1 changed files with 10 additions and 4 deletions

View File

@ -14,7 +14,6 @@ from django.shortcuts import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.utils import timezone
from django.utils.http import is_same_domain
from .utils import set_current_request
@ -163,9 +162,16 @@ class SafeRedirectMiddleware:
target_host = parsed.netloc
if target_host in [*settings.ALLOWED_HOSTS]:
return response
origin = request.get_host()
target_origin = target_host
if not is_same_domain(origin, target_origin):
target_host, target_port = self._split_host_port(parsed.netloc)
origin_host, origin_port = self._split_host_port(request.get_host())
if target_host != origin_host:
safe_redirect_url = '%s?%s' % (reverse('redirect-confirm'), f'next={quote(location)}')
return redirect(safe_redirect_url)
return response
@staticmethod
def _split_host_port(netloc):
if ':' in netloc:
host, port = netloc.split(':', 1)
return host, port
return netloc, '80'