Skip to content

Commit 5c18e77

Browse files
committed
use secure url construction
1 parent 80dc111 commit 5c18e77

2 files changed

Lines changed: 8 additions & 20 deletions

File tree

hypha/core/middleware/htmx.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from urllib.parse import urlparse
2+
from urllib.parse import parse_qs, urlencode, urlparse
33

44
from django.contrib.messages import get_messages
55
from django.http import HttpRequest, HttpResponse
@@ -105,22 +105,10 @@ def __call__(self, request):
105105
# Set response status code to 204 for HTMX to process the redirect
106106
response.status_code = 204
107107

108-
# Create the redirect URL manually to avoid encoding issues
109-
if redirect_url.query:
110-
# Extract the query parameters
111-
if "next=" in redirect_url.query:
112-
# Replace the existing next parameter
113-
import re
114-
115-
new_query = re.sub(
116-
r"next=[^&]*", f"next={next_path}", redirect_url.query
117-
)
118-
else:
119-
# Append a new next parameter
120-
new_query = f"{redirect_url.query}&next={next_path}"
121-
else:
122-
# No existing query parameters
123-
new_query = f"next={next_path}"
108+
# Update the "?next" query parameter
109+
query_params = parse_qs(redirect_url.query)
110+
query_params["next"] = [next_path]
111+
new_query = urlencode(query_params, doseq=True)
124112

125113
# Set the new HX-Redirect header
126114
response.headers["HX-Redirect"] = f"{redirect_url.path}?{new_query}"

hypha/core/middleware/tests/test_htmx_auth_redirect.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_htmx_auth_redirect(request_factory, middleware, settings_with_login_url
8282

8383
# Check that the response is modified with HX-Redirect
8484
assert response.status_code == 204
85-
assert response.headers["HX-Redirect"] == "/accounts/login/?next=/private/"
85+
assert response.headers["HX-Redirect"] == "/accounts/login/?next=%2Fprivate%2F"
8686

8787

8888
def test_htmx_auth_redirect_with_referer(
@@ -102,7 +102,7 @@ def test_htmx_auth_redirect_with_referer(
102102

103103
# Check that the response uses the Referer's path in the next parameter
104104
assert response.status_code == 204
105-
assert response.headers["HX-Redirect"] == "/accounts/login/?next=/some/page/"
105+
assert response.headers["HX-Redirect"] == "/accounts/login/?next=%2Fsome%2Fpage%2F"
106106

107107

108108
def test_non_htmx_request_not_redirected(
@@ -137,7 +137,7 @@ def test_htmx_non_auth_redirect_not_affected(
137137

138138
# Assert that the middleware handles the redirect with HX-Redirect
139139
assert response.status_code == 204
140-
assert response.headers["HX-Redirect"] == "/other-page/?next=/redirect/"
140+
assert response.headers["HX-Redirect"] == "/other-page/?next=%2Fredirect%2F"
141141

142142

143143
def test_htmx_normal_request(request_factory, middleware, settings_with_login_url):

0 commit comments

Comments
 (0)