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

Add cookiePath to CookieCsrfTokenRepository #4062

Closed
wants to merge 6 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -53,6 +53,8 @@ public final class CookieCsrfTokenRepository implements CsrfTokenRepository {

private boolean cookieHttpOnly;

private String cookiePath;

public CookieCsrfTokenRepository() {
this.setHttpOnlyMethod = ReflectionUtils.findMethod(Cookie.class, "setHttpOnly", boolean.class);
if (this.setHttpOnlyMethod != null) {
Expand All @@ -72,7 +74,11 @@ public void saveToken(CsrfToken token, HttpServletRequest request,
String tokenValue = token == null ? "" : token.getToken();
Cookie cookie = new Cookie(this.cookieName, tokenValue);
cookie.setSecure(request.isSecure());
cookie.setPath(getCookiePath(request));
if (this.cookiePath != null && !this.cookiePath.isEmpty()) {
cookie.setPath(this.cookiePath);
} else {
cookie.setPath(this.getRequestContext(request));
}
if (token == null) {
cookie.setMaxAge(0);
}
Expand Down Expand Up @@ -148,7 +154,7 @@ public void setCookieHttpOnly(boolean cookieHttpOnly) {
this.cookieHttpOnly = cookieHttpOnly;
}

private String getCookiePath(HttpServletRequest request) {
private String getRequestContext(HttpServletRequest request) {
String contextPath = request.getContextPath();
return contextPath.length() > 0 ? contextPath : "/";
}
Expand All @@ -169,4 +175,23 @@ public static CookieCsrfTokenRepository withHttpOnlyFalse() {
private String createNewToken() {
return UUID.randomUUID().toString();
}
}

/**
* Set the path that the Cookie will be created with. This will will override the default functionality which uses the
* request context as the path.
*
* @param path the path to use
*/
public void setCookiePath(String path) {
this.cookiePath = path;
}

/**
* Get the path that the CSRF cookie will be set to.
*
* @return the path to be used.
*/
public String getCookiePath() {
return this.cookiePath;
}
}
Expand Up @@ -150,6 +150,45 @@ public void saveTokenWithHttpOnlyFalse() {
assertThat(tokenCookie.isHttpOnly()).isFalse();
}

@Test
public void saveTokenCustomPath() {
String customPath = "/custompath";
this.repository.setCookiePath(customPath);
CsrfToken token = this.repository.generateToken(this.request);
this.repository.saveToken(token, this.request, this.response);

Cookie tokenCookie = this.response
.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);

assertThat(tokenCookie.getPath()).isEqualTo(this.repository.getCookiePath());
}

@Test
public void saveTokenEmptyCustomPath() {
String customPath = "";
this.repository.setCookiePath(customPath);
CsrfToken token = this.repository.generateToken(this.request);
this.repository.saveToken(token, this.request, this.response);

Cookie tokenCookie = this.response
.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);

assertThat(tokenCookie.getPath()).isEqualTo(this.request.getContextPath());
}

@Test
public void saveTokenNullCustomPath() {
String customPath = null;
this.repository.setCookiePath(customPath);
CsrfToken token = this.repository.generateToken(this.request);
this.repository.saveToken(token, this.request, this.response);

Cookie tokenCookie = this.response
.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);

assertThat(tokenCookie.getPath()).isEqualTo(this.request.getContextPath());
}

@Test
public void loadTokenNoCookiesNull() {
assertThat(this.repository.loadToken(this.request)).isNull();
Expand Down