From c2287292fca8c1ddd94aca31de1ad00a9e90a5cc Mon Sep 17 00:00:00 2001 From: AndrewG10i <16522951+AndrewG10i@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:26:13 +0100 Subject: [PATCH 1/3] [#2962] Honor the Jakarta EE secure session cookie opt-out - Group the Faces stage checks in EnvironmentLoaderListener so the explicit opt-out applies in Production. Previously, operator precedence let the Production check bypass the opt-out. - Add ShiroSessionCookieIT to verify that the container-managed JSESSIONID cookie carries Secure by default and omits it when the opt-out is enabled. --- .../jakarta/ee/CreateSessionServlet.java | 31 ++++++ .../shiro/testing/jakarta/ee/Deployments.java | 12 +++ .../jakarta/ee/ShiroSessionCookieIT.java | 97 +++++++++++++++++++ .../listeners/EnvironmentLoaderListener.java | 2 +- 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java create mode 100644 integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java new file mode 100644 index 0000000000..971138129a --- /dev/null +++ b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java @@ -0,0 +1,31 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.shiro.testing.jakarta.ee; + +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +@WebServlet(CreateSessionServlet.PATH) +public class CreateSessionServlet extends HttpServlet { + static final String PATH = "/create-container-session"; + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + request.getSession(true); + response.setStatus(HttpServletResponse.SC_NO_CONTENT); + } +} diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java index c62300580a..beed772db9 100644 --- a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java +++ b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java @@ -23,6 +23,8 @@ import static org.apache.shiro.testing.cdi.ComponentInjectionIT.TESTABLE_MODE; import static org.apache.shiro.testing.jakarta.ee.ShiroAuthFormsIT.DEPLOYMENT_DEV_MODE; import static org.apache.shiro.testing.jakarta.ee.ShiroAuthFormsIT.DEPLOYMENT_PROD_MODE; +import static org.apache.shiro.testing.jakarta.ee.ShiroSessionCookieIT.SESSION_COOKIE_DEFAULT; +import static org.apache.shiro.testing.jakarta.ee.ShiroSessionCookieIT.SESSION_COOKIE_OPT_OUT; import org.eu.ingwar.tools.arquillian.extension.suite.annotations.ArquillianSuiteDeployment; import org.jboss.arquillian.container.test.api.Deployment; @@ -59,6 +61,16 @@ public static WebArchive createDeploymentProd() { return ShiroAuthFormsIT.createDeploymentProd(); } + @Deployment(testable = false, name = SESSION_COOKIE_DEFAULT) + public static WebArchive createSessionCookieDefaultDeployment() { + return ShiroSessionCookieIT.createDeployment(SESSION_COOKIE_DEFAULT, false); + } + + @Deployment(testable = false, name = SESSION_COOKIE_OPT_OUT) + public static WebArchive createSessionCookieOptOutDeployment() { + return ShiroSessionCookieIT.createDeployment(SESSION_COOKIE_OPT_OUT, true); + } + @Deployment(name = TESTABLE_MODE) public static WebArchive createNonUIDeployment() { return ComponentInjectionIT.createDeployment("ShiroSuiteTest"); diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java new file mode 100644 index 0000000000..4c651a3b3f --- /dev/null +++ b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java @@ -0,0 +1,97 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.shiro.testing.jakarta.ee; + +import static com.flowlogix.util.ShrinkWrapManipulator.getContextParamValue; +import static org.assertj.core.api.Assertions.assertThat; + +import com.flowlogix.util.ShrinkWrapManipulator; +import com.flowlogix.util.ShrinkWrapManipulator.Action; +import jakarta.servlet.http.HttpServletResponse; +import java.net.URL; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.jboss.arquillian.container.test.api.OperateOnDeployment; +import org.jboss.arquillian.junit5.ArquillianExtension; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith(ArquillianExtension.class) +public class ShiroSessionCookieIT { + static final String SESSION_COOKIE_DEFAULT = "session-cookie-default"; + static final String SESSION_COOKIE_OPT_OUT = "session-cookie-opt-out"; + private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(15); + + @ArquillianResource + private URL baseUrl; + + @Test + @OperateOnDeployment(SESSION_COOKIE_DEFAULT) + void productionSessionCookieIsSecure() throws Exception { + assertThat(sessionCookieIsSecure(baseUrl)).as("JSESSIONID has the Secure attribute by default").isTrue(); + } + + @Test + @OperateOnDeployment(SESSION_COOKIE_OPT_OUT) + void productionSessionCookieHonorsOptOut() throws Exception { + assertThat(sessionCookieIsSecure(baseUrl)).as("JSESSIONID has the Secure attribute after opt-out").isFalse(); + } + + static WebArchive createDeployment(String name, boolean optOut) { + var archive = ShiroAuthFormsIT.createDeploymentProd(name).addClass(CreateSessionServlet.class); + // Keep these deployments on container sessions even in the suite's native session mode. + var actions = new ArrayList<>(List.of( + new Action(getContextParamValue("shiroConfigLocations"), + node -> node.setTextContent("classpath:META-INF/shiro.ini")), + new Action(getContextParamValue("org.apache.shiro.ee.disabled"), + node -> node.setTextContent("false")))); + if (optOut) { + actions.add(new Action("/web-app", webApp -> { + var document = webApp.getOwnerDocument(); + var contextParam = document.createElement("context-param"); + var paramName = document.createElement("param-name"); + paramName.setTextContent("org.apache.shiro.ee.secure-session-cookie.disabled"); + var paramValue = document.createElement("param-value"); + paramValue.setTextContent("true"); + contextParam.appendChild(paramName); + contextParam.appendChild(paramValue); + webApp.appendChild(contextParam); + })); + } + new ShrinkWrapManipulator().webXmlXPath(archive, actions); + return archive; + } + + private static boolean sessionCookieIsSecure(URL baseUrl) throws Exception { + var request = HttpRequest.newBuilder(baseUrl.toURI().resolve(CreateSessionServlet.PATH.substring(1))) + .timeout(REQUEST_TIMEOUT) + .build(); + var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.discarding()); + assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_NO_CONTENT); + var cookies = response.headers().allValues("Set-Cookie").stream() + .filter(cookie -> cookie.startsWith("JSESSIONID=")) + .toList(); + assertThat(cookies).hasSize(1); + return Arrays.stream(cookies.get(0).split(";")).skip(1) + .map(String::trim) + .anyMatch("Secure"::equalsIgnoreCase); + } +} diff --git a/support/jakarta-ee/src/main/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListener.java b/support/jakarta-ee/src/main/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListener.java index 5370930e3b..de4ae4e9d7 100644 --- a/support/jakarta-ee/src/main/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListener.java +++ b/support/jakarta-ee/src/main/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListener.java @@ -177,7 +177,7 @@ private static void modifySessionTrackingConfiguration(ServletContextEvent sce) private void modifySecureSessionConfiguration(ServletContextEvent sce) { if (!Boolean.parseBoolean(sce.getServletContext().getInitParameter(SHIRO_EE_DISABLE_SECURE_SESSION_COOKIE_PARAM)) - && !hasFacesContext() || !Faces.isDevelopment()) { + && (!hasFacesContext() || !Faces.isDevelopment())) { sce.getServletContext().getSessionCookieConfig().setSecure(true); } } From 74e4b622f317ace03f1fb5136391655497fb4231 Mon Sep 17 00:00:00 2001 From: AndrewG10i <16522951+AndrewG10i@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:45:24 +0100 Subject: [PATCH 2/3] Add missing Javadoc --- .../jakarta/ee/CreateSessionServlet.java | 9 +++++++ .../jakarta/ee/ShiroSessionCookieIT.java | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java index 971138129a..8ccdef845f 100644 --- a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java +++ b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java @@ -18,11 +18,20 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +/** + * Test fixture that creates a container-managed session so the client can inspect its session cookie. + */ @WebServlet(CreateSessionServlet.PATH) public class CreateSessionServlet extends HttpServlet { static final String PATH = "/create-container-session"; private static final long serialVersionUID = 1L; + /** + * Creates a session if needed and returns HTTP 204. + * + * @param request the request whose session is created or reused + * @param response the response that carries the session cookie when a new session is created + */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { request.getSession(true); diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java index 4c651a3b3f..917c43e79c 100644 --- a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java +++ b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java @@ -34,6 +34,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +/** + * Session Cookies Tests + */ @ExtendWith(ArquillianExtension.class) public class ShiroSessionCookieIT { static final String SESSION_COOKIE_DEFAULT = "session-cookie-default"; @@ -43,18 +46,35 @@ public class ShiroSessionCookieIT { @ArquillianResource private URL baseUrl; + /** + * Verifies that the container's session cookie carries {@code Secure} by default. + * + * @throws Exception if the session endpoint cannot be requested + */ @Test @OperateOnDeployment(SESSION_COOKIE_DEFAULT) void productionSessionCookieIsSecure() throws Exception { assertThat(sessionCookieIsSecure(baseUrl)).as("JSESSIONID has the Secure attribute by default").isTrue(); } + /** + * Verifies that the container's session cookie omits {@code Secure} when the Shiro opt-out is enabled. + * + * @throws Exception if the session endpoint cannot be requested + */ @Test @OperateOnDeployment(SESSION_COOKIE_OPT_OUT) void productionSessionCookieHonorsOptOut() throws Exception { assertThat(sessionCookieIsSecure(baseUrl)).as("JSESSIONID has the Secure attribute after opt-out").isFalse(); } + /** + * Builds a Production deployment with Shiro EE enabled and container-managed sessions. + * + * @param name the deployment name + * @param optOut whether to disable Shiro's enforcement of the session cookie's {@code Secure} attribute + * @return the configured web archive + */ static WebArchive createDeployment(String name, boolean optOut) { var archive = ShiroAuthFormsIT.createDeploymentProd(name).addClass(CreateSessionServlet.class); // Keep these deployments on container sessions even in the suite's native session mode. @@ -80,6 +100,13 @@ static WebArchive createDeployment(String name, boolean optOut) { return archive; } + /** + * Requests a new session and checks the raw {@code Set-Cookie} header for the {@code Secure} attribute. + * + * @param baseUrl the deployment's base URL + * @return whether the single {@code JSESSIONID} cookie carries {@code Secure} + * @throws Exception if the session endpoint URI is invalid or the request fails + */ private static boolean sessionCookieIsSecure(URL baseUrl) throws Exception { var request = HttpRequest.newBuilder(baseUrl.toURI().resolve(CreateSessionServlet.PATH.substring(1))) .timeout(REQUEST_TIMEOUT) From 346080d1def6261b0e499fe710391c07e4eda6f9 Mon Sep 17 00:00:00 2001 From: lprimak Date: Fri, 20 Nov 2026 15:41:01 -0600 Subject: [PATCH 3/3] tests: replace slow IT tests with fast unit tests --- .../jakarta/ee/CreateSessionServlet.java | 40 ------ .../shiro/testing/jakarta/ee/Deployments.java | 12 -- .../jakarta/ee/ShiroSessionCookieIT.java | 124 ------------------ .../EnvironmentLoaderListenerTest.java | 98 ++++++++++++++ 4 files changed, 98 insertions(+), 176 deletions(-) delete mode 100644 integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java delete mode 100644 integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java create mode 100644 support/jakarta-ee/src/test/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListenerTest.java diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java deleted file mode 100644 index 8ccdef845f..0000000000 --- a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/CreateSessionServlet.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.shiro.testing.jakarta.ee; - -import jakarta.servlet.annotation.WebServlet; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -/** - * Test fixture that creates a container-managed session so the client can inspect its session cookie. - */ -@WebServlet(CreateSessionServlet.PATH) -public class CreateSessionServlet extends HttpServlet { - static final String PATH = "/create-container-session"; - private static final long serialVersionUID = 1L; - - /** - * Creates a session if needed and returns HTTP 204. - * - * @param request the request whose session is created or reused - * @param response the response that carries the session cookie when a new session is created - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) { - request.getSession(true); - response.setStatus(HttpServletResponse.SC_NO_CONTENT); - } -} diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java index beed772db9..c62300580a 100644 --- a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java +++ b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/Deployments.java @@ -23,8 +23,6 @@ import static org.apache.shiro.testing.cdi.ComponentInjectionIT.TESTABLE_MODE; import static org.apache.shiro.testing.jakarta.ee.ShiroAuthFormsIT.DEPLOYMENT_DEV_MODE; import static org.apache.shiro.testing.jakarta.ee.ShiroAuthFormsIT.DEPLOYMENT_PROD_MODE; -import static org.apache.shiro.testing.jakarta.ee.ShiroSessionCookieIT.SESSION_COOKIE_DEFAULT; -import static org.apache.shiro.testing.jakarta.ee.ShiroSessionCookieIT.SESSION_COOKIE_OPT_OUT; import org.eu.ingwar.tools.arquillian.extension.suite.annotations.ArquillianSuiteDeployment; import org.jboss.arquillian.container.test.api.Deployment; @@ -61,16 +59,6 @@ public static WebArchive createDeploymentProd() { return ShiroAuthFormsIT.createDeploymentProd(); } - @Deployment(testable = false, name = SESSION_COOKIE_DEFAULT) - public static WebArchive createSessionCookieDefaultDeployment() { - return ShiroSessionCookieIT.createDeployment(SESSION_COOKIE_DEFAULT, false); - } - - @Deployment(testable = false, name = SESSION_COOKIE_OPT_OUT) - public static WebArchive createSessionCookieOptOutDeployment() { - return ShiroSessionCookieIT.createDeployment(SESSION_COOKIE_OPT_OUT, true); - } - @Deployment(name = TESTABLE_MODE) public static WebArchive createNonUIDeployment() { return ComponentInjectionIT.createDeployment("ShiroSuiteTest"); diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java deleted file mode 100644 index 917c43e79c..0000000000 --- a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/jakarta/ee/ShiroSessionCookieIT.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.shiro.testing.jakarta.ee; - -import static com.flowlogix.util.ShrinkWrapManipulator.getContextParamValue; -import static org.assertj.core.api.Assertions.assertThat; - -import com.flowlogix.util.ShrinkWrapManipulator; -import com.flowlogix.util.ShrinkWrapManipulator.Action; -import jakarta.servlet.http.HttpServletResponse; -import java.net.URL; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.time.Duration; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.jboss.arquillian.container.test.api.OperateOnDeployment; -import org.jboss.arquillian.junit5.ArquillianExtension; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.jboss.shrinkwrap.api.spec.WebArchive; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; - -/** - * Session Cookies Tests - */ -@ExtendWith(ArquillianExtension.class) -public class ShiroSessionCookieIT { - static final String SESSION_COOKIE_DEFAULT = "session-cookie-default"; - static final String SESSION_COOKIE_OPT_OUT = "session-cookie-opt-out"; - private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(15); - - @ArquillianResource - private URL baseUrl; - - /** - * Verifies that the container's session cookie carries {@code Secure} by default. - * - * @throws Exception if the session endpoint cannot be requested - */ - @Test - @OperateOnDeployment(SESSION_COOKIE_DEFAULT) - void productionSessionCookieIsSecure() throws Exception { - assertThat(sessionCookieIsSecure(baseUrl)).as("JSESSIONID has the Secure attribute by default").isTrue(); - } - - /** - * Verifies that the container's session cookie omits {@code Secure} when the Shiro opt-out is enabled. - * - * @throws Exception if the session endpoint cannot be requested - */ - @Test - @OperateOnDeployment(SESSION_COOKIE_OPT_OUT) - void productionSessionCookieHonorsOptOut() throws Exception { - assertThat(sessionCookieIsSecure(baseUrl)).as("JSESSIONID has the Secure attribute after opt-out").isFalse(); - } - - /** - * Builds a Production deployment with Shiro EE enabled and container-managed sessions. - * - * @param name the deployment name - * @param optOut whether to disable Shiro's enforcement of the session cookie's {@code Secure} attribute - * @return the configured web archive - */ - static WebArchive createDeployment(String name, boolean optOut) { - var archive = ShiroAuthFormsIT.createDeploymentProd(name).addClass(CreateSessionServlet.class); - // Keep these deployments on container sessions even in the suite's native session mode. - var actions = new ArrayList<>(List.of( - new Action(getContextParamValue("shiroConfigLocations"), - node -> node.setTextContent("classpath:META-INF/shiro.ini")), - new Action(getContextParamValue("org.apache.shiro.ee.disabled"), - node -> node.setTextContent("false")))); - if (optOut) { - actions.add(new Action("/web-app", webApp -> { - var document = webApp.getOwnerDocument(); - var contextParam = document.createElement("context-param"); - var paramName = document.createElement("param-name"); - paramName.setTextContent("org.apache.shiro.ee.secure-session-cookie.disabled"); - var paramValue = document.createElement("param-value"); - paramValue.setTextContent("true"); - contextParam.appendChild(paramName); - contextParam.appendChild(paramValue); - webApp.appendChild(contextParam); - })); - } - new ShrinkWrapManipulator().webXmlXPath(archive, actions); - return archive; - } - - /** - * Requests a new session and checks the raw {@code Set-Cookie} header for the {@code Secure} attribute. - * - * @param baseUrl the deployment's base URL - * @return whether the single {@code JSESSIONID} cookie carries {@code Secure} - * @throws Exception if the session endpoint URI is invalid or the request fails - */ - private static boolean sessionCookieIsSecure(URL baseUrl) throws Exception { - var request = HttpRequest.newBuilder(baseUrl.toURI().resolve(CreateSessionServlet.PATH.substring(1))) - .timeout(REQUEST_TIMEOUT) - .build(); - var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.discarding()); - assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_NO_CONTENT); - var cookies = response.headers().allValues("Set-Cookie").stream() - .filter(cookie -> cookie.startsWith("JSESSIONID=")) - .toList(); - assertThat(cookies).hasSize(1); - return Arrays.stream(cookies.get(0).split(";")).skip(1) - .map(String::trim) - .anyMatch("Secure"::equalsIgnoreCase); - } -} diff --git a/support/jakarta-ee/src/test/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListenerTest.java b/support/jakarta-ee/src/test/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListenerTest.java new file mode 100644 index 0000000000..2fff216e23 --- /dev/null +++ b/support/jakarta-ee/src/test/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListenerTest.java @@ -0,0 +1,98 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.shiro.ee.listeners; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletContextEvent; +import jakarta.servlet.SessionCookieConfig; +import org.apache.shiro.web.env.WebEnvironment; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.junit.jupiter.MockitoExtension; +import org.omnifaces.util.Faces; + +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class EnvironmentLoaderListenerTest { + private static final String OPT_OUT = "org.apache.shiro.ee.secure-session-cookie.disabled"; + private static final String TRACKING_DISABLED = "org.apache.shiro.ee.session-tracking-configuration.disabled"; + + @Mock + private ServletContext context; + @Mock + private SessionCookieConfig cookieConfig; + @Mock + private MockedStatic faces; + + private final EnvironmentLoaderListener listener = new EnvironmentLoaderListener() { + @Override + public WebEnvironment initEnvironment(ServletContext servletContext) { + return null; + } + }; + + @BeforeEach + void setUp() { + lenient().when(context.getInitParameter(TRACKING_DISABLED)).thenReturn("true"); + } + + @Test + void productionEnforcesSecureCookieByDefault() { + faces.when(Faces::hasContext).thenReturn(true); + faces.when(Faces::isDevelopment).thenReturn(false); + assertSecureCookieEnforced(); + } + + @Test + void developmentDoesNotEnforceSecureCookieByDefault() { + faces.when(Faces::hasContext).thenReturn(true); + faces.when(Faces::isDevelopment).thenReturn(true); + + assertSecureCookieNotEnforced(); + } + + @Test + void noFacesContextEnforcesSecureCookieByDefault() { + faces.when(Faces::hasContext).thenReturn(false); + assertSecureCookieEnforced(); + faces.verify(Faces::isDevelopment, never()); + } + + @Test + void productionHonorsOptOut() { + lenient().when(context.getInitParameter(OPT_OUT)).thenReturn("true"); + faces.when(Faces::isDevelopment).thenReturn(false); + + assertSecureCookieNotEnforced(); + faces.verifyNoInteractions(); + } + + private void assertSecureCookieEnforced() { + when(context.getSessionCookieConfig()).thenReturn(cookieConfig); + listener.contextInitialized(new ServletContextEvent(context)); + verify(cookieConfig).setSecure(true); + } + + private void assertSecureCookieNotEnforced() { + listener.contextInitialized(new ServletContextEvent(context)); + verify(context, never()).getSessionCookieConfig(); + } +}