Skip to content

Set secure on cookie when logging out #7764

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
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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,7 @@
* - A given list of Cookies
*
* @author Luke Taylor
* @author Onur Kagan Ozcan
* @since 3.1
*/
public final class CookieClearingLogoutHandler implements LogoutHandler {
Expand All @@ -46,6 +47,7 @@ public CookieClearingLogoutHandler(String... cookiesToClear) {
String cookiePath = request.getContextPath() + "/";
cookie.setPath(cookiePath);
cookie.setMaxAge(0);
cookie.setSecure(request.isSecure());
return cookie;
};
cookieList.add(f);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@

/**
* @author Luke Taylor
* @author Onur Kagan Ozcan
*/
public class CookieClearingLogoutHandlerTests {

Expand Down Expand Up @@ -61,6 +62,30 @@ public void configuredCookiesAreCleared() {
}
}

@Test
public void configuredCookieIsSecure() {
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSecure(true);
request.setContextPath("/app");
CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler("my_cookie");
handler.logout(request, response, mock(Authentication.class));
assertThat(response.getCookies()).hasSize(1);
assertThat(response.getCookies()[0].getSecure()).isTrue();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test checking that when the request is not secure, then the cookie is not secure.

@Test
public void configuredCookieIsNotSecure() {
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSecure(false);
request.setContextPath("/app");
CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler("my_cookie");
handler.logout(request, response, mock(Authentication.class));
assertThat(response.getCookies()).hasSize(1);
assertThat(response.getCookies()[0].getSecure()).isFalse();
}

@Test
public void passedInCookiesAreCleared() {
MockHttpServletResponse response = new MockHttpServletResponse();
Expand Down