Skip to content

Allow setting SameSite on the API session cookie - #612

Open
justin-fritz wants to merge 5 commits into
OpenIntegrationEngine:mainfrom
justin-fritz:justin-fritz-patch-1
Open

justin-fritz wants to merge 5 commits into
OpenIntegrationEngine:mainfrom
justin-fritz:justin-fritz-patch-1

Conversation

@justin-fritz

Copy link
Copy Markdown

Adds server.api.sessioncookie.samesite to configure the SameSite attribute of the REST API session cookie. Blank (default) keeps the current behavior. With None, the Web Admin works inside an iframe of a different site allowed by server.api.contentsecuritypolicy. Invalid values are logged and ignored.

Verified with Jetty 9.4.57: Set-Cookie: JSESSIONID=…; Path=/api; Secure; SameSite=None.
Closes #611

Added a method to create a session handler for the REST API with SameSite cookie attribute support.

Signed-off-by: justin-fritz <justin.fritz@amc-gmbh.com>
Added comments for session cookie SameSite attribute.

Signed-off-by: justin-fritz <justin.fritz@amc-gmbh.com>
Added documentation for session cookie SameSite attribute configuration.

Signed-off-by: justin-fritz <justin.fritz@amc-gmbh.com>

@pacmano1 pacmano1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None needs Secure even when the engine sees HTTP. Behind a proxy that terminates TLS and forwards to the HTTP port (server.api.allowhttp), the cookie comes back SameSite=None without Secure. Chromium, Firefox and WebKit all drop it, so login returns 200 and the next call 401. Marking the session cookie Secure whenever None is configured fixes it in all three (tried it). Plain HTTP between the browser and the engine can't work with None either way. The OIE web client's Node server (1.3.0) on plain HTTP also still breaks, because it strips Secure. That one is on its side, and I'll raise it there.

Log bad values at ERROR. With the shipped log4j2.properties (root at ERROR) the "Invalid value" warning shows in neither mirth.log nor the console. Logged at ERROR it shows in both.

Uppercase with Locale.ROOT. On a JVM with -Duser.language=tr -Duser.country=TR, Strict comes back without the attribute. With Locale.ROOT it's set.

The mirth.properties comment should say that None needs HTTPS to the engine or its TLS proxy, and that with None the cookie goes along on other sites' form posts. So server.api.require-requested-with should stay on, and frame-ancestors should list only the sites that embed it.

Tested with the web admin WAR framed by another site: None fixes it in Chromium, Firefox already works with blank, and WebKit still fails with None.

Co-authored with Claude Code.

@MichaelLeeHobbs

Copy link
Copy Markdown
Contributor

Great testing on this, especially running it in all three browser engines. One thought from someone who runs web servers behind TLS proxies every day:

Forcing Secure when SameSite=None is set fixes the symptom. The cause is that the engine can't tell a request came through a TLS proxy: only the HTTPS connector has a SecureRequestCustomizer, and nothing reads X-Forwarded-Proto/Forwarded. So behind a TLS-terminating proxy with server.api.allowhttp, Jetty sees plain HTTP. The session cookie is just the first thing to break; request.isSecure(), redirects and absolute URLs all get the wrong scheme too.

It's the same root cause as #578. #600 stops trusting a client-supplied X-Forwarded-For for audit IPs, which is right, but it means proxied deployments now record the proxy's address. #578 proposed a trusted-proxy setting for exactly this.

Jetty 9.4's ForwardedRequestCustomizer handles both headers but trusts whoever connects. Wrapping it in a check against a configured proxy list fixes that and stays small:

class TrustedProxyCustomizer implements HttpConfiguration.Customizer {
    private final InetAddressSet trusted = new InetAddressSet();
    private final ForwardedRequestCustomizer forwarded = new ForwardedRequestCustomizer();

    TrustedProxyCustomizer(String[] proxies) {
        for (String proxy : proxies) {
            trusted.add(proxy.trim());
        }
    }

    @Override
    public void customize(Connector connector, HttpConfiguration config, Request request) {
        InetSocketAddress peer = request.getHttpChannel().getEndPoint().getRemoteAddress();
        if (peer != null && trusted.test(peer.getAddress())) {
            forwarded.customize(connector, config, request);
        }
    }
}
String[] proxies = mirthProperties.getStringArray("server.api.trustedproxies");
if (proxies.length > 0) {
    httpConfig.addCustomizer(new TrustedProxyCustomizer(proxies));
}

InetAddressSet ships with Jetty 9.4 (it backs InetAccessHandler) and accepts single addresses, CIDR blocks and ranges, so server.api.trustedproxies=10.0.0.0/8,192.168.1.5 just works. Blank, the default, keeps today's behavior.

With a proxy listed:

  • X-Forwarded-Proto: https marks the request secure, so the session cookie gets Secure on its own for every SameSite value. No special case ties the two settings together.
  • X-Forwarded-For sets getRemoteAddr(), so Record the socket peer address in audit events #600's audit events record the real client again, and only when the request came through a listed proxy.
  • Headers from anyone else are ignored, so direct clients can't fake their scheme or IP.

One thing for the docs: the proxy should overwrite X-Forwarded-For rather than append to it (nginx proxy_set_header X-Forwarded-For $remote_addr;, not $proxy_add_x_forwarded_for). Otherwise the client's own value can come first in the list.

One property closes #578, fixes the proxy case for #600, and fixes this cookie without a workaround. Happy to help test it behind nginx.

- 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 <justin.fritz@amc-gmbh.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@justin-fritz

Copy link
Copy Markdown
Author

@pacmano1 Thanks for the thorough review, all four points are addressed in 039feb3:

  1. SameSite=None now always marks the session cookie Secure, so it also works behind a TLS-terminating proxy on the HTTP port.
  2. An invalid value is logged as ERROR.
  3. The value is upper-cased with Locale.ROOT.
  4. The mirth.properties comment now covers the HTTPS requirement, keeping server.api.require-requested-with enabled and listing only the embedding sites in frame-ancestors.

The previous red build was a 429 (Too Many Requests) from Maven Central while resolving the dependency-check plugin, not a compile error. The build for the new commit is waiting for maintainer approval.

@MichaelLeeHobbs Thanks, a TrustedProxyCustomizer sounds like the right fix for #578 and the proxy part of #600. I'd like to keep #612 small and handle that in a separate PR. Forcing Secure for SameSite=None doesn't conflict with it.

@justin-fritz
justin-fritz requested a review from pacmano1 December 1, 2026 10:06
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Test Results

  147 files  ±0    147 suites  ±0   4m 40s ⏱️ + 1m 24s
1 008 tests ±0  1 008 ✅ ±0  0 💤 ±0  0 ❌ ±0 
1 332 runs  ±0  1 326 ✅ ±0  6 💤 ±0  0 ❌ ±0 

Results for commit 039feb3. ± Comparison against base commit 75c52f6.

@pacmano1

pacmano1 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

I should have asked for this in the last review, sorry: please add a unit test. Nothing in CI sets server.api.sessioncookie.samesite, so none of this round's fixes is covered. RequestedWithFilterTest does the same kind of thing for server.api.require-requested-with. Cases worth covering: mixed case, Strict with a Turkish default locale, None forcing Secure, and a bad value leaving the attribute unset.

Co-authored with Claude Code.

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 <justin.fritz@amc-gmbh.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@justin-fritz

Copy link
Copy Markdown
Author

@pacmano1 Added MirthWebServerSessionCookieTest in d9a2ffe. It covers a blank value, mixed case, Strict with a tr-TR default locale, None forcing Secure, Lax and Strict not forcing Secure, and a bad value leaving the attribute unset. To test it without starting the server, createApiSessionHandler is now package-private and static. I also checked that the Turkish locale test fails without Locale.ROOT.

@MichaelLeeHobbs

Copy link
Copy Markdown
Contributor

Opened #620 for the trusted-proxy setting (#619). With a proxy listed in server.api.trustedproxies, X-Forwarded-Proto: https marks the request secure, so the session cookie gets Secure behind a TLS-terminating proxy for any SameSite value. Forcing Secure for None here doesn't conflict with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow setting SameSite on the API session cookie

3 participants