This is an automated email from the ASF dual-hosted git repository.

mridulpathak pushed a commit to branch release24.09
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release24.09 by this push:
     new 3278c2530d Fixed: Keep the redirection of a path alias to its aliasTo 
on the web site (OFBIZ-13645)
3278c2530d is described below

commit 3278c2530d90c7d44ac5801ef40bd46c73b51c17
Author: Mridul Pathak <[email protected]>
AuthorDate: Sun Oct 4 14:32:10 2026 +0530

    Fixed: Keep the redirection of a path alias to its aliasTo on the web site 
(OFBIZ-13645)
    
    When a WebSitePathAlias with an aliasTo is requested, CmsEvents.cms 
redirects to a location built from the context path, the servlet path, the 
hosted path alias, the request URI that the client sent without its last 
segment, and the aliasTo. That request URI is not normalized, so when the web 
application is mounted on the root a request whose URI starts with two slashes 
gave a location that starts with two slashes, which a browser follows to 
another host.
    
    The location is now built in CmsEvents.getAliasRedirectLocation, which 
always returns a path on the web site, with a single leading slash, and without 
any control character, such as a line break, that would end the header. The 
locations of the redirections that were already working are unchanged.
    
    (cherry picked from commit 669ed9f3e0e9b2ed61ca116a04895542c8ccc2c3)
---
 .../org/apache/ofbiz/content/cms/CmsEvents.java    | 37 +++++++++++++++++-----
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git 
a/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java
 
b/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java
index 2012f2d5cc..b313aa6c07 100644
--- 
a/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java
+++ 
b/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java
@@ -61,6 +61,31 @@ public final class CmsEvents {
     private CmsEvents() {
     }
 
+    /**
+     * Builds the location a path alias is redirected to: the context path, 
the servlet path, the hosted path alias if there is
+     * one, the part of the request URI before the last segment, and the alias 
the path is redirected to.
+     * The request URI is the one the client sent and it is not normalized, so 
it can start with two slashes, for example
+     * //example.com/.. when the web application is mounted on the root, and a 
browser takes a location that starts with two
+     * slashes as another host. The location is therefore always a path on 
this site, and it never contains a control character,
+     * such as a line break, which would end the header.
+     * @param context the context path of the request
+     * @param servletPath the servlet path of the request
+     * @param hostedPathAlias the hosted path alias of the web site, or null
+     * @param requestUri the request URI
+     * @param alias the path to redirect to, starting with a slash
+     * @return the location to redirect to
+     */
+    static String getAliasRedirectLocation(String context, String servletPath, 
String hostedPathAlias, String requestUri,
+            String alias) {
+        String location = context + servletPath;
+        if (hostedPathAlias != null) {
+            location += "/" + hostedPathAlias;
+        }
+        String uri = requestUri.substring(context.length());
+        uri = uri.substring(0, uri.lastIndexOf('/'));
+        return "/" + (location + uri + alias).replaceAll("\\p{Cntrl}", 
"").replaceFirst("^[/\\\\]+", "");
+    }
+
     public static String cms(HttpServletRequest request, HttpServletResponse 
response) {
         Delegator delegator = (Delegator) request.getAttribute("delegator");
         LocalDispatcher dispatcher = (LocalDispatcher) 
request.getAttribute("dispatcher");
@@ -181,19 +206,15 @@ public final class CmsEvents {
                             alias = "/" + alias;
                         }
 
-                        String context = request.getContextPath();
-                        String location = context + request.getServletPath();
                         GenericValue webSite = 
WebSiteWorker.getWebSite(request);
+                        String hostedPathAlias = null;
                         if (webSite != null && 
webSite.getString("hostedPathAlias") != null && !"ROOT".equals(pathInfo)) {
-                            location += "/" + 
webSite.getString("hostedPathAlias");
+                            hostedPathAlias = 
webSite.getString("hostedPathAlias");
                         }
 
-                        String uriWithContext = request.getRequestURI();
-                        String uri = 
uriWithContext.substring(context.length());
-                        uri = uri.substring(0, uri.lastIndexOf('/'));
-
                         
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
-                        response.setHeader("Location", location + uri + alias);
+                        response.setHeader("Location", 
getAliasRedirectLocation(request.getContextPath(), request.getServletPath(),
+                                hostedPathAlias, request.getRequestURI(), 
alias));
                         response.setHeader("Connection", "close");
 
                         return null; // null to not process any views

Reply via email to