middleware: Redirect non-canonical realm domain names.

If a host is in REALM_HOSTS, it has its own domain name.  Redirect
access from other domain names to that name.
This commit is contained in:
Alex Vandiver 2023-05-16 21:09:33 +00:00 committed by Tim Abbott
parent 724de9cd49
commit 24c3e25f86
1 changed files with 9 additions and 1 deletions

View File

@ -4,7 +4,7 @@ import tempfile
import time import time
import traceback import traceback
from typing import Any, AnyStr, Callable, Dict, Iterable, List, MutableMapping, Optional, Tuple from typing import Any, AnyStr, Callable, Dict, Iterable, List, MutableMapping, Optional, Tuple
from urllib.parse import urlencode from urllib.parse import urlencode, urljoin
from django.conf import settings from django.conf import settings
from django.conf.urls.i18n import is_language_prefix_patterns_used from django.conf.urls.i18n import is_language_prefix_patterns_used
@ -579,6 +579,14 @@ class HostDomainMiddleware(MiddlewareMixin):
return render(request, "zerver/invalid_realm.html", status=404) return render(request, "zerver/invalid_realm.html", status=404)
# Check that we're not using the non-canonical form of a REALM_HOSTS subdomain
if subdomain in settings.REALM_HOSTS:
host = request.get_host().lower()
formal_host = request_notes.realm.host
if host != formal_host and not host.startswith(formal_host + ":"):
return HttpResponseRedirect(
urljoin(request_notes.realm.uri, request.get_full_path())
)
return None return None