Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultRedirectStrategy Host-less Redirect #7358

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@
package org.springframework.security.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.security.web.util.UrlUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

/**
* Simple implementation of <tt>RedirectStrategy</tt> which is the default used throughout
* the framework.
*
* @author Luke Taylor
* @author Josh Cummings
* @since 3.0
*/
public class DefaultRedirectStrategy implements RedirectStrategy {

protected final Log logger = LogFactory.getLog(getClass());

private boolean contextRelative;
private boolean hostRelative = true;

/**
* Redirects the response to the supplied URL.
Expand Down Expand Up @@ -68,25 +72,40 @@ protected String calculateRedirectUrl(String contextPath, String url) {
}

// Full URL, including http(s)://
boolean hostRelative = this.hostRelative;
boolean contextRelative = isContextRelative();

if (!isContextRelative()) {
if (!hostRelative && !contextRelative) {
return url;
}

// Calculate the relative URL from the fully qualified URL, minus the last
// occurrence of the scheme and base context.
url = url.substring(url.lastIndexOf("://") + 3); // strip off scheme
url = url.substring(url.indexOf(contextPath) + contextPath.length());
UriComponents components = UriComponentsBuilder
.fromHttpUrl(url).build();

if (url.length() > 1 && url.charAt(0) == '/') {
url = url.substring(1);
String path = components.getPath();
if (contextRelative) {
path = path.substring(path.indexOf(contextPath) + contextPath.length());
if (path.length() > 1 && path.charAt(0) == '/') {
path = path.substring(1);
}
}

return url;
return UriComponentsBuilder
.fromPath(path)
.query(components.getQuery())
.build().toString();
}

/**
* If <tt>true</tt>, causes any redirection URLs to be calculated minus the authority
* (defaults to <tt>true</tt>).
*/
public void setHostRelative(boolean hostRelative) {
this.hostRelative = hostRelative;
}

/**
* If <tt>true</tt>, causes any redirection URLs to be calculated minus the protocol
* If <tt>true</tt>, causes any redirection URLs to be calculated minus the authority
* and context path (defaults to <tt>false</tt>).
*/
public void setContextRelative(boolean useRelativeContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/
package org.springframework.security.web;

import static org.assertj.core.api.Assertions.*;

import org.junit.Test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import static org.assertj.core.api.Assertions.assertThat;

/**
*
* @author Luke Taylor
Expand Down Expand Up @@ -56,4 +57,34 @@ public void contextRelativeUrlWithMultipleSchemesInHostnameIsHandledCorrectly()

assertThat(response.getRedirectedUrl()).isEqualTo("remainder");
}

// gh-7273
@Test
public void sendRedirectWhenUsingDefaultsThenRemovesHost()
throws Exception {
DefaultRedirectStrategy rds = new DefaultRedirectStrategy();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath("/context");
MockHttpServletResponse response = new MockHttpServletResponse();

rds.sendRedirect(request, response,
"https://context.blah.com/context/remainder");
assertThat(response.getRedirectedUrl()).isEqualTo("/context/remainder");
}

// gh-7273
@Test
public void sendRedirectWhenHostRelativeFalseThenKeepsHost()
throws Exception {
DefaultRedirectStrategy rds = new DefaultRedirectStrategy();
rds.setHostRelative(false);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath("/context");
MockHttpServletResponse response = new MockHttpServletResponse();

rds.sendRedirect(request, response,
"https://context.blah.com/context/remainder");

assertThat(response.getRedirectedUrl()).isEqualTo("https://context.blah.com/context/remainder");
}
}