fix: better redirect handling

This commit is contained in:
Ayush Chaudhari 2025-09-17 16:58:26 +05:30
parent 4cf7a94db6
commit 4a830b49e8

View file

@ -2,7 +2,7 @@
# License: MIT. See LICENSE
from urllib.parse import urlparse
from urllib.parse import urljoin, urlparse
import frappe
import frappe.utils
@ -202,17 +202,21 @@ def sanitize_redirect(redirect: str | None) -> str | None:
Allowed redirects:
- Same host e.g. https://frappe.localhost/path
- Just path e.g. /app
- Just path e.g. /app gets converted to https://frappe.localhost/app
"""
if not redirect:
return redirect
parsed_redirect = urlparse(redirect)
if not parsed_redirect.netloc:
return redirect
parsed_request_host = urlparse(frappe.local.request.url)
if parsed_request_host.netloc == parsed_redirect.netloc:
return redirect
output_parsed_url = parsed_redirect._replace(
netloc=parsed_request_host.netloc, scheme=parsed_request_host.scheme
)
if parsed_redirect.netloc:
if parsed_request_host.netloc != parsed_redirect.netloc:
output_parsed_url = output_parsed_url._replace(path="/app")
else:
output_parsed_url = output_parsed_url._replace(path=parsed_redirect.path)
return None
return output_parsed_url.geturl()