Skip to content

Commit 1f6381d

Browse files
okohubeleftherias
authored andcommitted
Set secure on cookie when logging out
Mark cookie secure flag to ensure cookie identity is the same
1 parent 8f1d0cf commit 1f6381d

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

web/src/main/java/org/springframework/security/web/authentication/logout/CookieClearingLogoutHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
* - A given list of Cookies
3333
*
3434
* @author Luke Taylor
35+
* @author Onur Kagan Ozcan
3536
* @since 3.1
3637
*/
3738
public final class CookieClearingLogoutHandler implements LogoutHandler {
@@ -46,6 +47,7 @@ public CookieClearingLogoutHandler(String... cookiesToClear) {
4647
String cookiePath = request.getContextPath() + "/";
4748
cookie.setPath(cookiePath);
4849
cookie.setMaxAge(0);
50+
cookie.setSecure(request.isSecure());
4951
return cookie;
5052
};
5153
cookieList.add(f);

web/src/test/java/org/springframework/security/web/authentication/logout/CookieClearingLogoutHandlerTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @author Luke Taylor
30+
* @author Onur Kagan Ozcan
3031
*/
3132
public class CookieClearingLogoutHandlerTests {
3233

@@ -61,6 +62,30 @@ public void configuredCookiesAreCleared() {
6162
}
6263
}
6364

65+
@Test
66+
public void configuredCookieIsSecure() {
67+
MockHttpServletResponse response = new MockHttpServletResponse();
68+
MockHttpServletRequest request = new MockHttpServletRequest();
69+
request.setSecure(true);
70+
request.setContextPath("/app");
71+
CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler("my_cookie");
72+
handler.logout(request, response, mock(Authentication.class));
73+
assertThat(response.getCookies()).hasSize(1);
74+
assertThat(response.getCookies()[0].getSecure()).isTrue();
75+
}
76+
77+
@Test
78+
public void configuredCookieIsNotSecure() {
79+
MockHttpServletResponse response = new MockHttpServletResponse();
80+
MockHttpServletRequest request = new MockHttpServletRequest();
81+
request.setSecure(false);
82+
request.setContextPath("/app");
83+
CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler("my_cookie");
84+
handler.logout(request, response, mock(Authentication.class));
85+
assertThat(response.getCookies()).hasSize(1);
86+
assertThat(response.getCookies()[0].getSecure()).isFalse();
87+
}
88+
6489
@Test
6590
public void passedInCookiesAreCleared() {
6691
MockHttpServletResponse response = new MockHttpServletResponse();

0 commit comments

Comments
 (0)