Skip to content

Commit

Permalink
#26640 include in 23.10.24
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Jan 26, 2024
1 parent 658605d commit 48305da
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
3 changes: 2 additions & 1 deletion dotCMS/hotfix_tracking.md
Expand Up @@ -36,4 +36,5 @@ This maintenance release includes the following code fixes:
30. https://github.com/dotCMS/core/issues/26706 : Change Default PUBSUB Provider #26706
31. https://github.com/dotCMS/core/issues/26825 : Change order of config precidence #26825
32. https://github.com/dotCMS/core/issues/26439 : Relationship fields not respecting the order identifiers are sent via the workflow API #26439
33. https://github.com/dotCMS/core/issues/26693 : Edit Permissions Individually stuck when editing folder with legacy ID #26693
33. https://github.com/dotCMS/core/issues/26693 : Edit Permissions Individually stuck when editing folder with legacy ID #26693
34. https://github.com/dotCMS/core/issues/26640 : Cyrillic URLs encoding issue when configured by Vanity URL Redirect #26640
Expand Up @@ -227,28 +227,29 @@ public void test_that_vanity_url_filter_handles_redirects() throws Exception {


String title = "VanityURL" + System.currentTimeMillis();
String site = defaultHost.getIdentifier();
String uri = "/testing_301" + System.currentTimeMillis();
String forwardTo = "https://dotcms.com";
String baseURI = "/testing_301" + System.currentTimeMillis();
String vanityURIPattern = baseURI + "/(.*)";
String forwardBaseURI = "https://dotcms.com/redirected_301";
String forwardTo = forwardBaseURI + "/$1";
int action = 301;
int order = 1;

Contentlet contentlet1 = filtersUtil.createVanityUrl(title, defaultHost, uri,
Contentlet contentlet1 = filtersUtil.createVanityUrl(title, defaultHost, vanityURIPattern,
forwardTo, action, order, defaultLanguage.getId());
filtersUtil.publishVanityUrl(contentlet1);


final HttpServletRequest request = new MockHttpRequestIntegrationTest(defaultHost.getHostname(), uri).request();
final String testURI = baseURI + "/test redirect 301";
final HttpServletRequest request = new MockHttpRequestIntegrationTest(defaultHost.getHostname(), testURI).request();
final HttpServletResponse response = new MockHttpStatusResponse(new MockHeaderResponse(new MockHttpResponse().response()).response()).response();


VanityURLFilter filter = new VanityURLFilter();

filter.doFilter(request, response, null);

Collection<String> list= response.getHeaderNames();
assert(response.getHeader("Location").equals(forwardTo));
assert(response.getStatus()==301);
final String expectedLocation = forwardBaseURI + "/test%20redirect%20301";
Assert.assertEquals(expectedLocation, response.getHeader("Location"));
Assert.assertEquals(301,response.getStatus());
Assert.assertNotNull(response.getHeader("X-DOT-VanityUrl"));

}
Expand Down
@@ -1,5 +1,6 @@
package com.dotcms.vanityurl.business;

import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
Expand All @@ -8,6 +9,8 @@
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.dotmarketing.util.URLUtils;
import org.apache.http.HttpStatus;
import com.dotcms.api.web.HttpServletRequestThreadLocal;
import com.dotcms.business.CloseDBIfOpened;
Expand Down Expand Up @@ -42,6 +45,7 @@

import com.liferay.util.StringPool;
import io.vavr.control.Try;
import org.apache.http.client.utils.URIBuilder;

/**
* Implementation class for the {@link VanityUrlAPI}.
Expand Down Expand Up @@ -375,7 +379,7 @@ public boolean handleVanityURLRedirects(final VanityUrlRequestWrapper request, f
final String newUrl = uri + (queryString != null ? StringPool.QUESTION + queryString : StringPool.BLANK);
if (responseCode == 301 || responseCode == 302) {
response.setStatus(responseCode);
response.setHeader("Location", newUrl);
response.setHeader("Location", encodeRedirectURL(newUrl));
return true;
}

Expand All @@ -388,6 +392,61 @@ public boolean handleVanityURLRedirects(final VanityUrlRequestWrapper request, f
return false;
}

/**
* Encodes the redirect URL with the parameters from the request.
*
* @param uri The URI to redirect to.
* @return The encoded redirect URL.
*/
private String encodeRedirectURL(final String uri) {
try {
boolean hasProtocol = true;
String redirectURI = uri;
if (uri.startsWith("//")) {
hasProtocol = false;
redirectURI = "none:" + uri;
}
final URLUtils.ParsedURL urlToEncode = URLUtils.parseURL(redirectURI);
if (urlToEncode == null) {
throw new DotRuntimeException("Could not parse redirect URL: " + uri);
}
final URIBuilder uriBuilder = new URIBuilder();
if (UtilMethods.isSet(urlToEncode.getProtocol()) && hasProtocol) {
uriBuilder.setScheme(urlToEncode.getProtocol());
}
if (UtilMethods.isSet(urlToEncode.getHost())) {
final String hostWithUserInfo = urlToEncode.getHost();
String host = hostWithUserInfo;
String userInfo = "";
if (hostWithUserInfo.contains("@")) {
userInfo = hostWithUserInfo.substring(0, hostWithUserInfo.indexOf("@"));
host = hostWithUserInfo.substring(hostWithUserInfo.indexOf("@") + 1);
}
if (UtilMethods.isSet(userInfo)) {
uriBuilder.setUserInfo(userInfo);
}
uriBuilder.setHost(host);
}
if (urlToEncode.getPort() > 0) {
uriBuilder.setPort(urlToEncode.getPort());
}
if (UtilMethods.isSet(urlToEncode.getURI())) {
uriBuilder.setPath(urlToEncode.getURI());
}
final Map<String, String[]> paramMap = urlToEncode.getParameters();
if (paramMap != null) {
for (final Map.Entry<String, String[]> entry : paramMap.entrySet()) {
for (final String value : entry.getValue()) {
uriBuilder.addParameter(entry.getKey(), value);
}
}
}
return uriBuilder.build().toASCIIString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

@Override
@CloseDBIfOpened
public List<CachedVanityUrl> findByForward(final Host host, final Language language, final String forward,int action) {
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void testExecuteAction() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);

when(request.getAttribute(CMS_FILTER_URI_OVERRIDE)).thenReturn("/index");
String url = "//foo";
String url = "/foo";
ParameterModel paramURL = new ParameterModel(URL_KEY, url);
ParameterModel paramRedirectMethod = new ParameterModel(INPUT_REDIRECT_METHOD, REDIRECT_METHOD.MOVED_PERM.name());
Map<String, ParameterModel> params = new HashMap<>();
Expand Down

0 comments on commit 48305da

Please sign in to comment.