Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
middleware: Fix URL encoding of next parameter.
Anders Kaseorgcce142c61a7d
diff --git a/zerver/middleware.py b/zerver/middleware.pyindex bb21467b260..3f5a6845fc0 100644--- a/zerver/middleware.py+++ b/zerver/middleware.py@@ -14,6 +14,7 @@ from typing import (Optional,Tuple,)+from urllib.parse import urlencodefrom django.conf import settingsfrom django.conf.urls.i18n import is_language_prefix_patterns_used@@ -460,7 +461,9 @@ class JsonErrorHandler(MiddlewareMixin):# execute the likely intent for intentionally visiting# an API endpoint without authentication in a browser,# but that's an unlikely to be done intentionally often.- return HttpResponseRedirect(f"{settings.HOME_NOT_LOGGED_IN}?next={request.path}")+ return HttpResponseRedirect(+ f"{settings.HOME_NOT_LOGGED_IN}?{urlencode({'next': request.path})}"+ )if request.path.startswith("/api"):# For API routes, ask for HTTP basic auth (email:apiKey).return json_unauthorized()diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 5c29892993f..37fe7d86faf 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -1942,7 +1942,7 @@ class RestAPITest(ZulipTestCase):def test_http_accept_redirect(self) -> None:result = self.client_get("/json/users", HTTP_ACCEPT="text/html")self.assertEqual(result.status_code, 302)- self.assertTrue(result["Location"].endswith("/login/?next=/json/users"))+ self.assertTrue(result["Location"].endswith("/login/?next=%2Fjson%2Fusers"))class CacheTestCase(ZulipTestCase):diff --git a/zerver/tests/test_docs.py b/zerver/tests/test_docs.pyindex 0697570d053..c9416e1be4d 100644--- a/zerver/tests/test_docs.py+++ b/zerver/tests/test_docs.py@@ -401,7 +401,7 @@ class PlansPageTest(ZulipTestCase):result = self.client_get("/plans/", subdomain=root_domain)self.assert_in_success_response(["Self-host Zulip"], result)self.assert_not_in_success_response(["/upgrade#sponsorship"], result)- self.assert_in_success_response(["/accounts/go/?next=/upgrade%23sponsorship"], result)+ self.assert_in_success_response(["/accounts/go/?next=%2Fupgrade%23sponsorship"], result)non_existent_domain = "moo"result = self.client_get("/plans/", subdomain=non_existent_domain)@@ -425,7 +425,7 @@ class PlansPageTest(ZulipTestCase):result = self.client_get("/plans/", subdomain="zulip")self.assert_in_success_response(["Current plan"], result)self.assert_in_success_response(["/upgrade#sponsorship"], result)- self.assert_not_in_success_response(["/accounts/go/?next=/upgrade%23sponsorship"], result)+ self.assert_not_in_success_response(["/accounts/go/?next=%2Fupgrade%23sponsorship"], result)# Test root domain, with login on different domainresult = self.client_get("/plans/", subdomain="")diff --git a/zerver/views/portico.py b/zerver/views/portico.pyindex eaab2fbd390..757f5392284 100644--- a/zerver/views/portico.py+++ b/zerver/views/portico.py@@ -1,5 +1,5 @@-import urllib.parsefrom typing import Optional+from urllib.parse import urlencodeimport orjsonfrom django.conf import settings@@ -40,7 +40,7 @@ def plans_view(request: HttpRequest) -> HttpResponse:sponsorship_url = "/upgrade#sponsorship"if is_subdomain_root_or_alias(request):# If we're on the root domain, we make this link first ask you which organization.- sponsorship_url = f"/accounts/go/?next={urllib.parse.quote(sponsorship_url)}"+ sponsorship_url = f"/accounts/go/?{urlencode({'next': sponsorship_url})}"realm_on_free_trial = Falseif realm is not None: