Skip to content

Trust forwarded headers only from configured proxies - #620

Open
MichaelLeeHobbs wants to merge 2 commits into
OpenIntegrationEngine:mainfrom
MichaelLeeHobbs:feature/api-trusted-proxies
Open

MichaelLeeHobbs wants to merge 2 commits into
OpenIntegrationEngine:mainfrom
MichaelLeeHobbs:feature/api-trusted-proxies

Conversation

@MichaelLeeHobbs

Copy link
Copy Markdown
Contributor

Closes #619

Adds server.api.trustedproxies, a comma-separated list of addresses, CIDR blocks or ranges. When it's set, the HTTP and HTTPS connectors apply Jetty's ForwardedRequestCustomizer only to requests whose connection comes from a listed address. So behind a TLS-terminating proxy, X-Forwarded-Proto: https marks the request secure (the session cookie gets Secure) and X-Forwarded-For sets the remote address. Forwarded headers from any other client are ignored. Blank, the default, keeps the current behavior.

Jetty takes the first X-Forwarded-For entry, so the property comment says the proxy must replace that header rather than append to it.

The audit-IP part depends on #600: on main, getRequestIpAddress() still reads X-Forwarded-For directly.

How to verify

./gradlew :server:test --tests "*MirthWebServerTrustedProxyTest" starts a Jetty connector and sends X-Forwarded-For: 10.0.0.5 and X-Forwarded-Proto: https from 127.0.0.1. With the property blank, or listing only 10.0.0.0/8, the request reports 127.0.0.1 and not secure. Listing 127.0.0.1 or 127.0.0.0/8 makes it report 10.0.0.5 and secure. I ran this test; I did not run the full suite or test behind a real proxy.

🤖 Generated with Claude Code

Add server.api.trustedproxies. When set, both connectors apply Jetty's
ForwardedRequestCustomizer to requests whose peer address is in the list,
so a TLS-terminating proxy's X-Forwarded-Proto marks the request secure and
its X-Forwarded-For sets the remote address. Headers from other clients are
ignored. Blank, the default, keeps the current behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Michael Hobbs <michael.lee.hobbs@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are identified for the opt-in trusted-proxy change.

No open issues 🎉

What changed in this PR

Adds opt-in trusted-proxy handling to Jetty, allowing requests behind TLS-terminating proxies to report the original client address and secure scheme. Audit-IP support depends on #600.

Changes:

  • Applies forwarded headers only for configured proxy peers on HTTP and HTTPS connectors.
  • Documents server.api.trustedproxies, preserving existing behavior when blank.
  • Adds connector tests for blank, trusted, and untrusted proxy configurations.
File Description
server/​src/​test/​java/​com/​mirth/​connect/​server/​MirthWebServerTrustedProxyTest.java Tests forwarded-header handling against a live Jetty connector.
server/​src/​main/​java/​com/​mirth/​connect/​server/​MirthWebServer.java Adds peer-address validation before applying forwarded headers.
server/​conf/​mirth.properties Documents trusted-proxy configuration and header-replacement requirements.

Review effort: Balanced

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Test Results

  152 files  + 2    152 suites  +2   4m 38s ⏱️ +31s
1 041 tests +25  1 041 ✅ +25  0 💤 ±0  0 ❌ ±0 
1 419 runs  +25  1 413 ✅ +25  6 💤 ±0  0 ❌ ±0 

Results for commit 8739881. ± Comparison against base commit 7aa5e8b.

♻️ This comment has been updated with latest results.

@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.

One fix before merge. A bad value in server.api.trustedproxies takes the whole admin API down. InetAddressSet.add throws IllegalArgumentException: Bad pattern on anything that isn't an IP, CIDR or range, a hostname, or a plain typo like 172.30.0.10x, and TrustedProxyCustomizer adds each entry with no guard, so it throws out of the MirthWebServer constructor. Both connectors then fail to start, HTTP and HTTPS both refuse connections and a fronting proxy gets 502, but the engine logs "server successfully started" and keeps running: channels deploy and process messages fine. So one typo in this new property leaves the engine moving traffic while nobody can reach the API, the Administrator or the web admin, with only a bare stack trace in the log, not an ERROR.

Measured on a PR build behind an nginx TLS proxy: 172.30.0.10 works (cookie gets Secure through the proxy, forged headers from untrusted clients ignored, direct access unchanged); 172.30.0.10x or a hostname downs both connectors with the engine still reporting started.

Please validate each entry and either skip the bad one or fail fast, with a clear ERROR naming the value, rather than letting it kill the web server. Hostnames aren't supported (InetAddressSet doesn't resolve them), which matches the IP/CIDR/range comment, so rejecting a non-IP value with that message would read better than a raw "Bad pattern".

Co-authored with Claude Code.

@pacmano1

pacmano1 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

One more: add a test for a bad value. The three tests only feed valid IPs and CIDR, so the unparseable path, the one that throws and takes the web server down, has no coverage. A case that feeds a bad entry and asserts the server still starts (or the value is rejected cleanly) would lock it in.

An entry InetAddressSet can't parse (a typo or a hostname) threw out of
the MirthWebServer constructor, so neither connector started. Log it as
an ERROR and skip it; the valid entries still apply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Michael Hobbs <michael.lee.hobbs@gmail.com>
@MichaelLeeHobbs

Copy link
Copy Markdown
Contributor Author

@pacmano1 Thanks for testing it behind nginx. Fixed in 8739881: an entry InetAddressSet can't parse is now skipped with an ERROR naming the value and saying an IP address, CIDR block or range is expected. The valid entries still apply, and the connectors start either way. A skipped proxy's headers are just ignored, as with a blank property.

testInvalidEntryIsSkipped covers 172.30.0.10x and a hostname on their own (server starts, headers ignored) and 172.30.0.10x, 127.0.0.1 (the valid entry still applies). I checked that it fails without the fix.

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.

Requests through a TLS-terminating proxy are seen as plain HTTP from the proxy's address

3 participants