From 73befe8c4016e81e06a1d26fc3515dd5d6f20261 Mon Sep 17 00:00:00 2001 From: justin-fritz Date: Mon, 30 Nov 2026 16:58:12 +0100 Subject: [PATCH 1/5] Allow setting SameSite on the API session cookie Added a method to create a session handler for the REST API with SameSite cookie attribute support. Signed-off-by: justin-fritz --- .../mirth/connect/server/MirthWebServer.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java index b2cbf8d8f..5359cc4ba 100644 --- a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java +++ b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java @@ -52,6 +52,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configurator; +import org.eclipse.jetty.http.HttpCookie; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.MimeTypes; @@ -449,7 +450,7 @@ private ServletContextHandler createApiServletContextHandler(String contextPath, // Create the servlet handler for the API ServletContextHandler apiServletContextHandler = new ServletContextHandler(); apiServletContextHandler.setMaxFormContentSize(0); - apiServletContextHandler.setSessionHandler(new SessionHandler()); + apiServletContextHandler.setSessionHandler(createApiSessionHandler(mirthProperties)); apiServletContextHandler.setContextPath(contextPath + baseAPI + apiPath); apiServletContextHandler.addFilter(new FilterHolder(new ApiOriginFilter(mirthProperties)), "/*", EnumSet.of(DispatcherType.REQUEST)); apiServletContextHandler.addFilter(new FilterHolder(new ClickjackingFilter(mirthProperties)), "/*", EnumSet.of(DispatcherType.REQUEST)); @@ -462,7 +463,29 @@ private ServletContextHandler createApiServletContextHandler(String contextPath, return apiServletContextHandler; } - + /** + * Creates the session handler for the REST API. The SameSite attribute of the session cookie + * can be set with "server.api.sessioncookie.samesite" (None, Lax or Strict). If the property is + * blank, the cookie is sent without a SameSite attribute, as before. + * + * SameSite=None is required when a web client using the API is embedded in an iframe of + * another site (allowed via server.api.contentsecuritypolicy). Browsers then reject the cookie + * unless it is also Secure, which is the case for HTTPS connectors. + */ + private SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProperties) { + SessionHandler sessionHandler = new SessionHandler(); + String sameSite = StringUtils.trimToEmpty(mirthProperties.getString("server.api.sessioncookie.samesite", "")); + + if (!sameSite.isEmpty()) { + try { + sessionHandler.setSameSite(HttpCookie.SameSite.valueOf(sameSite.toUpperCase())); + } catch (IllegalArgumentException e) { + logger.warn("Invalid value \"" + sameSite + "\" for server.api.sessioncookie.samesite, expected None, Lax or Strict. The SameSite attribute will not be set."); + } + } + + return sessionHandler; + } private void addApiServlets(HandlerList handlers, ServletContextHandler apiServletContextHandler, String contextPath, String baseAPI, boolean apiAllowHTTP, Version version, PropertiesConfiguration mirthProperties) { Version apiVersion = version; if (apiVersion == null) { From eb2db9b8fab83581ad88df197a0dacf418fc44a9 Mon Sep 17 00:00:00 2001 From: justin-fritz Date: Mon, 30 Nov 2026 17:00:40 +0100 Subject: [PATCH 2/5] Document SameSite attribute for API session cookie Added comments for session cookie SameSite attribute. Signed-off-by: justin-fritz --- server/conf/mirth.properties | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/conf/mirth.properties b/server/conf/mirth.properties index 4c9894277..0d0f852ec 100644 --- a/server/conf/mirth.properties +++ b/server/conf/mirth.properties @@ -54,6 +54,10 @@ server.api.accesscontrolallowheaders = Content-Type server.api.accesscontrolexposeheaders = server.api.accesscontrolmaxage = +# SameSite attribute of the API session cookie (None, Lax or Strict). Leave blank to omit it. +# Set to None to embed a web client in an iframe of another site (see server.api.contentsecuritypolicy). +server.api.sessioncookie.samesite = + # Password for the admin user, only applied when the database is first created. # If blank, a random password is generated and written to the log once on first startup. server.initialadminpassword = From 8fb4c66a42729f64b0cc2b7119cdde6b42632dea Mon Sep 17 00:00:00 2001 From: justin-fritz Date: Mon, 30 Nov 2026 17:03:39 +0100 Subject: [PATCH 3/5] Document session cookie SameSite attribute handling Added documentation for session cookie SameSite attribute configuration. Signed-off-by: justin-fritz --- .../src/main/java/com/mirth/connect/server/MirthWebServer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java index 5359cc4ba..e105ee442 100644 --- a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java +++ b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java @@ -463,6 +463,7 @@ private ServletContextHandler createApiServletContextHandler(String contextPath, return apiServletContextHandler; } + /** * Creates the session handler for the REST API. The SameSite attribute of the session cookie * can be set with "server.api.sessioncookie.samesite" (None, Lax or Strict). If the property is @@ -486,6 +487,7 @@ private SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProp return sessionHandler; } + private void addApiServlets(HandlerList handlers, ServletContextHandler apiServletContextHandler, String contextPath, String baseAPI, boolean apiAllowHTTP, Version version, PropertiesConfiguration mirthProperties) { Version apiVersion = version; if (apiVersion == null) { From 039feb32b78426bba92f99bf662b9be0590ae64d Mon Sep 17 00:00:00 2001 From: justin-fritz Date: Tue, 1 Dec 2026 10:45:39 +0100 Subject: [PATCH 4/5] Force Secure for SameSite=None and address review feedback - Always mark the API session cookie Secure when SameSite is None, also behind a TLS-terminating proxy in front of the HTTP connector - Log an invalid value as ERROR so it shows with the default log4j2 config - Use Locale.ROOT when parsing the value - Document the HTTPS requirement and the CSRF considerations in mirth.properties - Fix the Javadoc indentation Signed-off-by: justin-fritz Co-Authored-By: Claude Opus 5.5 --- server/conf/mirth.properties | 5 ++++- .../mirth/connect/server/MirthWebServer.java | 18 +++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/server/conf/mirth.properties b/server/conf/mirth.properties index 0d0f852ec..1ee25e577 100644 --- a/server/conf/mirth.properties +++ b/server/conf/mirth.properties @@ -55,7 +55,10 @@ server.api.accesscontrolexposeheaders = server.api.accesscontrolmaxage = # SameSite attribute of the API session cookie (None, Lax or Strict). Leave blank to omit it. -# Set to None to embed a web client in an iframe of another site (see server.api.contentsecuritypolicy). +# None allows a web client in an iframe of another site (see server.api.contentsecuritypolicy). +# None requires HTTPS between the browser and the engine or its TLS proxy; the cookie is then always Secure. +# With None the cookie is also sent with form posts from other sites, so keep +# server.api.require-requested-with enabled and list only the embedding sites in frame-ancestors. server.api.sessioncookie.samesite = # Password for the admin user, only applied when the database is first created. diff --git a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java index e105ee442..71aa0b4c3 100644 --- a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java +++ b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java @@ -27,6 +27,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import java.util.zip.GZIPOutputStream; @@ -463,15 +464,14 @@ private ServletContextHandler createApiServletContextHandler(String contextPath, return apiServletContextHandler; } - - /** + + /** * Creates the session handler for the REST API. The SameSite attribute of the session cookie * can be set with "server.api.sessioncookie.samesite" (None, Lax or Strict). If the property is * blank, the cookie is sent without a SameSite attribute, as before. * - * SameSite=None is required when a web client using the API is embedded in an iframe of - * another site (allowed via server.api.contentsecuritypolicy). Browsers then reject the cookie - * unless it is also Secure, which is the case for HTTPS connectors. + * Browsers only accept SameSite=None together with Secure, so the cookie is always marked + * Secure in that case, also when TLS is terminated by a proxy in front of an HTTP connector. */ private SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProperties) { SessionHandler sessionHandler = new SessionHandler(); @@ -479,9 +479,13 @@ private SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProp if (!sameSite.isEmpty()) { try { - sessionHandler.setSameSite(HttpCookie.SameSite.valueOf(sameSite.toUpperCase())); + HttpCookie.SameSite value = HttpCookie.SameSite.valueOf(sameSite.toUpperCase(Locale.ROOT)); + sessionHandler.setSameSite(value); + if (value == HttpCookie.SameSite.NONE) { + sessionHandler.getSessionCookieConfig().setSecure(true); + } } catch (IllegalArgumentException e) { - logger.warn("Invalid value \"" + sameSite + "\" for server.api.sessioncookie.samesite, expected None, Lax or Strict. The SameSite attribute will not be set."); + logger.error("Invalid value \"" + sameSite + "\" for server.api.sessioncookie.samesite, expected None, Lax or Strict. The SameSite attribute will not be set."); } } From d9a2ffe6175c0aa2f416e813ff3473717474fffe Mon Sep 17 00:00:00 2001 From: justin-fritz Date: Wed, 2 Dec 2026 09:34:00 +0100 Subject: [PATCH 5/5] Add unit test for the API session cookie SameSite setting Cover a blank or unset value, mixed case, Strict with a Turkish default locale, None forcing Secure, Lax and Strict not forcing Secure, and an invalid value leaving the attribute unset. createApiSessionHandler is now package-private and static so it can be tested without starting the web server. Signed-off-by: justin-fritz Co-Authored-By: Claude Opus 5.5 --- .../mirth/connect/server/MirthWebServer.java | 6 +- .../MirthWebServerSessionCookieTest.java | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 server/src/test/java/com/mirth/connect/server/MirthWebServerSessionCookieTest.java diff --git a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java index 71aa0b4c3..1354c8a90 100644 --- a/server/src/main/java/com/mirth/connect/server/MirthWebServer.java +++ b/server/src/main/java/com/mirth/connect/server/MirthWebServer.java @@ -472,8 +472,10 @@ private ServletContextHandler createApiServletContextHandler(String contextPath, * * Browsers only accept SameSite=None together with Secure, so the cookie is always marked * Secure in that case, also when TLS is terminated by a proxy in front of an HTTP connector. + * + * Package-private and static so it can be unit tested without starting the web server. */ - private SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProperties) { + static SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProperties) { SessionHandler sessionHandler = new SessionHandler(); String sameSite = StringUtils.trimToEmpty(mirthProperties.getString("server.api.sessioncookie.samesite", "")); @@ -485,7 +487,7 @@ private SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProp sessionHandler.getSessionCookieConfig().setSecure(true); } } catch (IllegalArgumentException e) { - logger.error("Invalid value \"" + sameSite + "\" for server.api.sessioncookie.samesite, expected None, Lax or Strict. The SameSite attribute will not be set."); + LogManager.getLogger(MirthWebServer.class).error("Invalid value \"" + sameSite + "\" for server.api.sessioncookie.samesite, expected None, Lax or Strict. The SameSite attribute will not be set."); } } diff --git a/server/src/test/java/com/mirth/connect/server/MirthWebServerSessionCookieTest.java b/server/src/test/java/com/mirth/connect/server/MirthWebServerSessionCookieTest.java new file mode 100644 index 000000000..f371d3abd --- /dev/null +++ b/server/src/test/java/com/mirth/connect/server/MirthWebServerSessionCookieTest.java @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Justin Fritz + +package com.mirth.connect.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Locale; + +import org.apache.commons.configuration2.PropertiesConfiguration; +import org.eclipse.jetty.http.HttpCookie; +import org.eclipse.jetty.server.session.SessionHandler; +import org.junit.Test; + +import com.mirth.connect.client.core.PropertiesConfigurationUtil; + +public class MirthWebServerSessionCookieTest { + + private static final String PROPERTY = "server.api.sessioncookie.samesite"; + + private static SessionHandler createSessionHandler(String sameSite) { + PropertiesConfiguration mirthProperties = PropertiesConfigurationUtil.create(); + if (sameSite != null) { + mirthProperties.setProperty(PROPERTY, sameSite); + } + return MirthWebServer.createApiSessionHandler(mirthProperties); + } + + @Test + // assert that a missing or blank property keeps the previous behavior + public void testUnsetOrBlankLeavesAttributeUnset() { + for (String sameSite : new String[] { null, "", " " }) { + SessionHandler sessionHandler = createSessionHandler(sameSite); + assertNull(sessionHandler.getSameSite()); + assertFalse(sessionHandler.getSessionCookieConfig().isSecure()); + } + } + + @Test + // assert that the value is case-insensitive and surrounding whitespace is ignored + public void testMixedCase() { + assertEquals(HttpCookie.SameSite.LAX, createSessionHandler("lAx").getSameSite()); + assertEquals(HttpCookie.SameSite.STRICT, createSessionHandler(" STRICT ").getSameSite()); + assertEquals(HttpCookie.SameSite.NONE, createSessionHandler("none").getSameSite()); + } + + @Test + // assert that "Strict" is parsed with a Turkish default locale, where "i".toUpperCase() is not "I" + public void testStrictWithTurkishDefaultLocale() { + Locale defaultLocale = Locale.getDefault(); + try { + Locale.setDefault(Locale.forLanguageTag("tr-TR")); + assertEquals(HttpCookie.SameSite.STRICT, createSessionHandler("Strict").getSameSite()); + } finally { + Locale.setDefault(defaultLocale); + } + } + + @Test + // assert that None always marks the cookie Secure, because browsers reject SameSite=None without it + public void testNoneForcesSecure() { + SessionHandler sessionHandler = createSessionHandler("None"); + assertEquals(HttpCookie.SameSite.NONE, sessionHandler.getSameSite()); + assertTrue(sessionHandler.getSessionCookieConfig().isSecure()); + } + + @Test + // assert that Lax and Strict do not change the Secure flag + public void testLaxAndStrictDoNotForceSecure() { + assertFalse(createSessionHandler("Lax").getSessionCookieConfig().isSecure()); + assertFalse(createSessionHandler("Strict").getSessionCookieConfig().isSecure()); + } + + @Test + // assert that an invalid value leaves the attribute unset instead of failing + public void testBadValueLeavesAttributeUnset() { + SessionHandler sessionHandler = createSessionHandler("bogus"); + assertNull(sessionHandler.getSameSite()); + assertFalse(sessionHandler.getSessionCookieConfig().isSecure()); + } +}