Skip to content
Open
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 @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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> 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();
}
}
Loading