π₯π₯π₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.
TypeScript boardsbug-trackerdjangodockerganttissue-trackerjirajira-alternativekanbanlinearpostgresqlproduct-managementproject-managementproject-planningpythonreactredistypesciptvitework-management
[WEB-4943] refactor: enhance URL validation and redirection logic in authentication views (#7815)
Nikhil3d0618972327
diff --git a/apps/api/plane/authentication/views/space/email.py b/apps/api/plane/authentication/views/space/email.pyindex 2fb1c2c5ea..b25e2d015c 100644--- a/apps/api/plane/authentication/views/space/email.py+++ b/apps/api/plane/authentication/views/space/email.py@@ -15,8 +15,7 @@ from plane.authentication.adapter.error import (AUTHENTICATION_ERROR_CODES,AuthenticationException,)-from plane.utils.path_validator import get_safe_redirect_url, validate_next_path-+from plane.utils.path_validator import get_safe_redirect_url, validate_next_path, get_allowed_hostsclass SignInAuthSpaceEndpoint(View):def post(self, request):@@ -200,7 +199,7 @@ class SignUpAuthSpaceEndpoint(View):user_login(request=request, user=user, is_space=True)# redirect to referer pathnext_path = validate_next_path(next_path=next_path)- url = f"{base_host(request=request, is_space=True).rstrip("/")}{next_path}"+ url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):return HttpResponseRedirect(url)else:diff --git a/apps/api/plane/authentication/views/space/github.py b/apps/api/plane/authentication/views/space/github.pyindex dd148b8c10..acc956cf3b 100644--- a/apps/api/plane/authentication/views/space/github.py+++ b/apps/api/plane/authentication/views/space/github.py@@ -4,6 +4,7 @@ import uuid# Django importfrom django.http import HttpResponseRedirectfrom django.views import View+from django.utils.http import url_has_allowed_host_and_scheme# Module importsfrom plane.authentication.provider.oauth.github import GitHubOAuthProvider@@ -14,7 +15,7 @@ from plane.authentication.adapter.error import (AUTHENTICATION_ERROR_CODES,AuthenticationException,)-from plane.utils.path_validator import get_safe_redirect_url, validate_next_path+from plane.utils.path_validator import get_safe_redirect_url, validate_next_path, get_allowed_hostsclass GitHubOauthInitiateSpaceEndpoint(View):@@ -94,8 +95,11 @@ class GitHubCallbackSpaceEndpoint(View):# Process workspace and project invitations# redirect to referer pathnext_path = validate_next_path(next_path=next_path)- url = f"{base_host(request=request, is_space=True).rstrip("/")}{next_path}"- return HttpResponseRedirect(url)+ url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"+ if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):+ return HttpResponseRedirect(url)+ else:+ return HttpResponseRedirect(base_host(request=request, is_space=True))except AuthenticationException as e:params = e.get_error_dict()url = get_safe_redirect_url(diff --git a/apps/api/plane/authentication/views/space/gitlab.py b/apps/api/plane/authentication/views/space/gitlab.pyindex 77a10a9147..493cd823e2 100644--- a/apps/api/plane/authentication/views/space/gitlab.py+++ b/apps/api/plane/authentication/views/space/gitlab.py@@ -4,6 +4,7 @@ import uuid# Django importfrom django.http import HttpResponseRedirectfrom django.views import View+from django.utils.http import url_has_allowed_host_and_scheme# Module importsfrom plane.authentication.provider.oauth.gitlab import GitLabOAuthProvider@@ -14,7 +15,7 @@ from plane.authentication.adapter.error import (AUTHENTICATION_ERROR_CODES,AuthenticationException,)-from plane.utils.path_validator import get_safe_redirect_url, validate_next_path+from plane.utils.path_validator import get_safe_redirect_url, get_allowed_hosts, validate_next_pathclass GitLabOauthInitiateSpaceEndpoint(View):@@ -95,8 +96,11 @@ class GitLabCallbackSpaceEndpoint(View):# Process workspace and project invitations# redirect to referer pathnext_path = validate_next_path(next_path=next_path)- url = f"{base_host(request=request, is_space=True).rstrip("/")}{next_path}"- return HttpResponseRedirect(url)+ url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"+ if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):+ return HttpResponseRedirect(url)+ else:+ return HttpResponseRedirect(base_host(request=request, is_space=True))except AuthenticationException as e:params = e.get_error_dict()url = get_safe_redirect_url(diff --git a/apps/api/plane/authentication/views/space/google.py b/apps/api/plane/authentication/views/space/google.pyindex d8fef9da45..c7c8337716 100644--- a/apps/api/plane/authentication/views/space/google.py+++ b/apps/api/plane/authentication/views/space/google.py@@ -4,6 +4,7 @@ import uuid# Django importfrom django.http import HttpResponseRedirectfrom django.views import View+from django.utils.http import url_has_allowed_host_and_scheme# Module importsfrom plane.authentication.provider.oauth.google import GoogleOAuthProvider@@ -14,7 +15,7 @@ from plane.authentication.adapter.error import (AuthenticationException,AUTHENTICATION_ERROR_CODES,)-from plane.utils.path_validator import get_safe_redirect_url, validate_next_path+from plane.utils.path_validator import get_safe_redirect_url, validate_next_path, get_allowed_hostsclass GoogleOauthInitiateSpaceEndpoint(View):@@ -91,8 +92,11 @@ class GoogleCallbackSpaceEndpoint(View):user_login(request=request, user=user, is_space=True)# redirect to referer pathnext_path = validate_next_path(next_path=next_path)- url = f"{base_host(request=request, is_space=True).rstrip("/")}{next_path}"- return HttpResponseRedirect(url)+ url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"+ if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):+ return HttpResponseRedirect(url)+ else:+ return HttpResponseRedirect(base_host(request=request, is_space=True))except AuthenticationException as e:params = e.get_error_dict()url = get_safe_redirect_url(diff --git a/apps/api/plane/authentication/views/space/magic.py b/apps/api/plane/authentication/views/space/magic.pyindex 85e3a185cf..5052d40c0b 100644--- a/apps/api/plane/authentication/views/space/magic.py+++ b/apps/api/plane/authentication/views/space/magic.py@@ -96,7 +96,7 @@ class MagicSignInSpaceEndpoint(View):user_login(request=request, user=user, is_space=True)# redirect to referer pathnext_path = validate_next_path(next_path=next_path)- url = f"{base_host(request=request, is_space=True).rstrip("/")}{next_path}"+ url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):return HttpResponseRedirect(url)else:@@ -158,7 +158,7 @@ class MagicSignUpSpaceEndpoint(View):user_login(request=request, user=user, is_space=True)# redirect to referer pathnext_path = validate_next_path(next_path=next_path)- url = f"{base_host(request=request, is_space=True).rstrip("/")}{next_path}"+ url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):return HttpResponseRedirect(url)else:diff --git a/apps/api/plane/utils/path_validator.py b/apps/api/plane/utils/path_validator.pyindex a89c8b9698..ccb67d868b 100644--- a/apps/api/plane/utils/path_validator.py+++ b/apps/api/plane/utils/path_validator.py@@ -46,7 +46,11 @@ def _contains_suspicious_patterns(path: str) -> bool:def get_allowed_hosts() -> list[str]:"""Get the allowed hosts from the settings."""base_origin = settings.WEB_URL or settings.APP_BASE_URL- allowed_hosts = [base_origin]++ allowed_hosts = []+ if base_origin:+ host = urlparse(base_origin).netloc+ allowed_hosts.append(host)if settings.ADMIN_BASE_URL:# Get only the hosthost = urlparse(settings.ADMIN_BASE_URL).netloc@@ -107,19 +111,33 @@ def get_safe_redirect_url(base_url: str, next_path: str = "", params: dict = {})# Validate the next pathvalidated_path = validate_next_path(next_path)-+# Add the next path to the parametersbase_url = base_url.rstrip('/')++ # Prepare the query parameters+ query_parts = []+ encoded_params = ""++ # Add the next path to the parameters+ if validated_path:+ query_parts.append(f"next_path={validated_path}")++ # Add additional parametersif params:encoded_params = urlencode(params)- url = f"{base_url}/?next_path={validated_path}&{encoded_params}"+ query_parts.append(encoded_params)++ # Construct the url query string+ if query_parts:+ query_string = "&".join(query_parts)+ url = f"{base_url}/?{query_string}"else:- url = f"{base_url}/?next_path={validated_path}"+ url = base_url# Check if the URL is allowedif url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):return url# Return the base URL if the URL is not allowed- return base_url-\ No newline at end of file+ return base_url + (f"?{encoded_params}" if encoded_params else "")\ No newline at end of file