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
7 changes: 7 additions & 0 deletions server/conf/mirth.properties
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ 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.
# 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.
# If blank, a random password is generated and written to the log once on first startup.
server.initialadminpassword =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -52,6 +53,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;
Expand Down Expand Up @@ -449,7 +451,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));
Expand All @@ -462,6 +464,35 @@ 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.
*
* 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.
*/
static SessionHandler createApiSessionHandler(PropertiesConfiguration mirthProperties) {
SessionHandler sessionHandler = new SessionHandler();
String sameSite = StringUtils.trimToEmpty(mirthProperties.getString("server.api.sessioncookie.samesite", ""));

if (!sameSite.isEmpty()) {
try {
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) {
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.");
}
}

return sessionHandler;
}

private void addApiServlets(HandlerList handlers, ServletContextHandler apiServletContextHandler, String contextPath, String baseAPI, boolean apiAllowHTTP, Version version, PropertiesConfiguration mirthProperties) {
Version apiVersion = version;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Justin Fritz <justin.fritz@amc-gmbh.com>

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());
}
}