Skip to content

Allow setting SameSite on the API session cookie - #612

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

justin-fritz wants to merge 3 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.

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

Labels

None yet

3 participants