Skip to content

Commit e113bd3

Browse files
michalrwinch
michal
authored andcommitted
issue 5414 - configurable secure flag in CookieCsrfTokenRepository
While using the request's "isSecure" flag is a reasonable default, when webapps sit behind firewalls, sometimes the firewall does the SSL, and the traffic between the firewall and the app is plain HTTP (not HTTPS). In this case the "isSecure" flag on the request is always false, but we still want th XSRF-TOKEN cookie to be secure (the firewall forwards all cookies to the app, and the browser sends the secure cookie to the firewall). It would be nice if we could configure the desired value for the secure flag of the cookie, just like we can configure the value for the httpOnly flag of the cookie.
1 parent 565fd28 commit e113bd3

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.UUID;
2020

21+
import javax.servlet.ServletRequest;
2122
import javax.servlet.http.Cookie;
2223
import javax.servlet.http.HttpServletRequest;
2324
import javax.servlet.http.HttpServletResponse;
@@ -53,6 +54,8 @@ public final class CookieCsrfTokenRepository implements CsrfTokenRepository {
5354

5455
private String cookieDomain;
5556

57+
private Boolean secure;
58+
5659
public CookieCsrfTokenRepository() {
5760
}
5861

@@ -67,7 +70,12 @@ public void saveToken(CsrfToken token, HttpServletRequest request,
6770
HttpServletResponse response) {
6871
String tokenValue = token == null ? "" : token.getToken();
6972
Cookie cookie = new Cookie(this.cookieName, tokenValue);
70-
cookie.setSecure(request.isSecure());
73+
if (secure == null) {
74+
cookie.setSecure(request.isSecure());
75+
} else {
76+
cookie.setSecure(secure);
77+
}
78+
7179
if (this.cookiePath != null && !this.cookiePath.isEmpty()) {
7280
cookie.setPath(this.cookiePath);
7381
} else {
@@ -195,4 +203,17 @@ public void setCookieDomain(String cookieDomain) {
195203
this.cookieDomain = cookieDomain;
196204
}
197205

206+
/**
207+
* Sets secure flag of the cookie that the expected CSRF token is saved to and read from.
208+
* By default secure flag depends on {@link ServletRequest#isSecure()}
209+
*
210+
* @since 5.4
211+
* @param secure the secure flag of the cookie that the expected CSRF token is saved to
212+
* and read from
213+
*/
214+
public void setSecure(Boolean secure) {
215+
this.secure = secure;
216+
}
217+
218+
198219
}

web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryTests.java

+27
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,33 @@ public void saveTokenSecure() {
9898
assertThat(tokenCookie.getSecure()).isTrue();
9999
}
100100

101+
@Test
102+
public void saveTokenSecureFlagTrue() {
103+
this.request.setSecure(false);
104+
this.repository.setSecure(Boolean.TRUE);
105+
CsrfToken token = this.repository.generateToken(this.request);
106+
this.repository.saveToken(token, this.request, this.response);
107+
108+
Cookie tokenCookie = this.response
109+
.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
110+
111+
assertThat(tokenCookie.getSecure()).isTrue();
112+
}
113+
114+
@Test
115+
public void saveTokenSecureFlagFalse() {
116+
this.request.setSecure(true);
117+
this.repository.setSecure(Boolean.FALSE);
118+
CsrfToken token = this.repository.generateToken(this.request);
119+
this.repository.saveToken(token, this.request, this.response);
120+
121+
Cookie tokenCookie = this.response
122+
.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
123+
124+
assertThat(tokenCookie.getSecure()).isFalse();
125+
}
126+
127+
101128
@Test
102129
public void saveTokenNull() {
103130
this.request.setSecure(true);

0 commit comments

Comments
 (0)