Skip to content

Add Configurable secure flag in CookieCsrfTokenRepository #8749

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

Merged
merged 1 commit into from
Jun 25, 2020
Merged
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 @@ -18,6 +18,7 @@

import java.util.UUID;

import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -53,6 +54,8 @@ public final class CookieCsrfTokenRepository implements CsrfTokenRepository {

private String cookieDomain;

private Boolean secure;

public CookieCsrfTokenRepository() {
}

Expand All @@ -67,7 +70,12 @@ public void saveToken(CsrfToken token, HttpServletRequest request,
HttpServletResponse response) {
String tokenValue = token == null ? "" : token.getToken();
Cookie cookie = new Cookie(this.cookieName, tokenValue);
cookie.setSecure(request.isSecure());
if (secure == null) {
cookie.setSecure(request.isSecure());
} else {
cookie.setSecure(secure);
}

if (this.cookiePath != null && !this.cookiePath.isEmpty()) {
cookie.setPath(this.cookiePath);
} else {
Expand Down Expand Up @@ -195,4 +203,17 @@ public void setCookieDomain(String cookieDomain) {
this.cookieDomain = cookieDomain;
}

/**
* Sets secure flag of the cookie that the expected CSRF token is saved to and read from.
* By default secure flag depends on {@link ServletRequest#isSecure()}
*
* @since 5.4
* @param secure the secure flag of the cookie that the expected CSRF token is saved to
* and read from
*/
public void setSecure(Boolean secure) {
this.secure = secure;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ public void saveTokenSecure() {
assertThat(tokenCookie.getSecure()).isTrue();
}

@Test
public void saveTokenSecureFlagTrue() {
this.request.setSecure(false);
this.repository.setSecure(Boolean.TRUE);
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.getSecure()).isTrue();
}

@Test
public void saveTokenSecureFlagFalse() {
this.request.setSecure(true);
this.repository.setSecure(Boolean.FALSE);
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.getSecure()).isFalse();
}


@Test
public void saveTokenNull() {
this.request.setSecure(true);
Expand Down