Skip to content

Commit 2d8df05

Browse files
committed
Generate warning of invalid configuration of cookies
1 parent 87cee6e commit 2d8df05

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/ContainerProxyApplication.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public class ContainerProxyApplication {
8989
private static final String PROP_SERVER_SECURE_COOKIES = "server.secure-cookies";
9090
private static final Boolean SECURE_COOKIES_DEFAULT_VALUE = false;
9191

92+
public static Boolean secureCookiesEnabled;
93+
public static String sameSiteCookiePolicy;
94+
9295
public static void main(String[] args) {
9396
SpringApplication app = new SpringApplication(ContainerProxyApplication.class);
9497

@@ -115,10 +118,16 @@ public void init() {
115118
log.warn("WARNING: Using server.use-forward-headers will not work in this ShinyProxy release, you need to change your configuration to use another property. See https://shinyproxy.io/documentation/security/#forward-headers on how to change your configuration.");
116119
}
117120

118-
String sameSiteCookie = environment.getProperty(PROP_PROXY_SAME_SITE_COOKIE, SAME_SITE_COOKIE_DEFAULT_VALUE);
119-
log.debug("Setting sameSiteCookie policy to {}" , sameSiteCookie);
120-
defaultCookieSerializer.setSameSite(sameSiteCookie);
121-
defaultCookieSerializer.setUseSecureCookie(environment.getProperty(PROP_SERVER_SECURE_COOKIES, Boolean.class, SECURE_COOKIES_DEFAULT_VALUE));
121+
sameSiteCookiePolicy = environment.getProperty(PROP_PROXY_SAME_SITE_COOKIE, SAME_SITE_COOKIE_DEFAULT_VALUE);
122+
secureCookiesEnabled = environment.getProperty(PROP_SERVER_SECURE_COOKIES, Boolean.class, SECURE_COOKIES_DEFAULT_VALUE);
123+
124+
log.debug("Setting sameSiteCookie policy to {}" , sameSiteCookiePolicy);
125+
defaultCookieSerializer.setSameSite(sameSiteCookiePolicy);
126+
defaultCookieSerializer.setUseSecureCookie(secureCookiesEnabled);
127+
128+
if (sameSiteCookiePolicy.equalsIgnoreCase("none") && !secureCookiesEnabled) {
129+
log.warn("WARNING: Invalid configuration detected: same-site-cookie policy is set to None, but secure-cookies are not enabled. Secure cookies must be enabled when using None as same-site-cookie policy ");
130+
}
122131
}
123132

124133
@Autowired(required = false)
@@ -134,13 +143,12 @@ public UndertowServletWebServerFactory servletContainer() {
134143
}
135144
info.addInnerHandlerChainWrapper(defaultHandler -> mappingManager.createHttpHandler(defaultHandler));
136145

137-
String sameSiteCookie = environment.getProperty(PROP_PROXY_SAME_SITE_COOKIE, SAME_SITE_COOKIE_DEFAULT_VALUE);
138-
log.debug("Setting sameSiteCookie policy for session cookies to {}" , sameSiteCookie);
139-
info.addOuterHandlerChainWrapper(defaultHandler -> new SameSiteCookieHandler(defaultHandler, sameSiteCookie, null, true, true, false));
146+
log.debug("Setting sameSiteCookie policy for session cookies to {}" , sameSiteCookiePolicy);
147+
info.addOuterHandlerChainWrapper(defaultHandler -> new SameSiteCookieHandler(defaultHandler, sameSiteCookiePolicy, null, true, true, false));
140148

141149
ServletSessionConfig sessionConfig = new ServletSessionConfig();
142150
sessionConfig.setHttpOnly(true);
143-
sessionConfig.setSecure(environment.getProperty(PROP_SERVER_SECURE_COOKIES, Boolean.class, SECURE_COOKIES_DEFAULT_VALUE));
151+
sessionConfig.setSecure(secureCookiesEnabled);
144152
info.setServletSessionConfig(sessionConfig);
145153
if (sessionManagerFactory != null) {
146154
info.setSessionManagerFactory(sessionManagerFactory);

src/main/java/eu/openanalytics/containerproxy/security/WebSecurityConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
*/
2121
package eu.openanalytics.containerproxy.security;
2222

23+
import eu.openanalytics.containerproxy.ContainerProxyApplication;
2324
import eu.openanalytics.containerproxy.auth.IAuthenticationBackend;
2425
import eu.openanalytics.containerproxy.auth.UserLogoutHandler;
2526
import eu.openanalytics.containerproxy.util.AppRecoveryFilter;
27+
import org.apache.logging.log4j.LogManager;
28+
import org.apache.logging.log4j.Logger;
2629
import org.springframework.beans.factory.annotation.Autowired;
2730
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
2831
import org.springframework.context.annotation.Bean;
@@ -58,6 +61,8 @@
5861
@EnableWebSecurity
5962
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
6063

64+
private final Logger logger = LogManager.getLogger(getClass());
65+
6166
@Inject
6267
private UserLogoutHandler logoutHandler;
6368

@@ -99,6 +104,13 @@ public void configure(WebSecurity web) {
99104
}
100105
}
101106

107+
private void checkForIncorrectConfiguration(HttpServletRequest request) {
108+
if (request.getScheme().equals("http") && ContainerProxyApplication.secureCookiesEnabled) {
109+
logger.warn("WARNING: Invalid configuration detected: ShinyProxy is accessed over HTTP but secure-cookies is enabled. Secure-cookies only work when accessing ShinyProxy over HTTPS. "
110+
+ "Ensure that ShinyProxy is accessed over HTTPS or disable secure-cookies");
111+
}
112+
}
113+
102114
@Override
103115
protected void configure(HttpSecurity http) throws Exception {
104116
// App Recovery Filter
@@ -112,6 +124,8 @@ protected void configure(HttpSecurity http) throws Exception {
112124
final AccessDeniedHandler defaultAccessDeniedHandler = new AccessDeniedHandlerImpl();
113125
@Override
114126
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
127+
checkForIncorrectConfiguration(request);
128+
115129
if (matcher.matcher(request).isMatch() && accessDeniedException instanceof MissingCsrfTokenException) {
116130
response.sendRedirect(ServletUriComponentsBuilder.fromCurrentContextPath().path("/login").queryParam("error", "expired").build().toUriString());
117131
} else {

0 commit comments

Comments
 (0)