Pass the browser's address to the mail server, and trust IPv4 proxies on a dual-stack socket #115

Merged
jcoffey-dev merged 2 commits from feat/forward-client-ip into main 2026-11-19 03:00:29 +00:00
Owner

K-W3 (#105), plus a bug it turned up.

1. Trust IPv4 proxies on a dual-stack socket. This bug affects current releases. The image listens on ::, where Node reports an IPv4 peer as ::ffff:10.0.0.5. No IPv4 range matches that form, so a proxy reaching the webmail over IPv4 was never trusted. Its X-Forwarded-For was ignored, and the proxy itself became the client. Behind Caddy, nginx or an ingress, that means:

  • In password mode, everyone shares one sign-in limit (LOGIN_RATE_LIMIT, 10 per 15 minutes by default). One person guessing locks everyone out.
  • The logs and the session list name the proxy instead of the user.

The peer is now read in its plain IPv4 form, as the forwarded chain already was. Verified in Node: before the fix, resolveClientIp("::ffff:10.244.1.5", {forwardedFor: "203.0.113.7"}) returned the proxy.

2. Tell the mail server who each request is for. Requests to the mail server now carry X-Forwarded-For with the client address the webmail resolved, replacing anything the browser sent. This goes through upstreamFetch in clientcontext.ts, using AsyncLocalStorage set by a middleware. Without it, the server's limits and bans count all of a replica's users as one address. Fetches for remote images and calendars use a separate path and never carry the header. Requests outside a browser request, such as push, carry none.

3. README: "Several replicas". The webmail's limits count per replica and are a first line only. The server's limits hold across replicas, and need the server to believe the header (INBUXA_HTTP_USE_FORWARDED, inbuxa-server PR).

Tested

  • 4 new unit tests, including one through the real app with a stand-in server. Typecheck, the full suite (316) and the build pass.
  • On kind, two replicas behind port-forwards acting as the proxy (inbuxa-k8s-lab ratelimit-test.sh):
    • Replica A refuses one client at attempt 201.
    • Replica B still starts that client's sign-in.
    • Replica A still starts a second client's sign-in, which shows it counts the browser and not the proxy.
  • frontends-test.sh passes.

Should ship in 27.1-rc.7. Since part 1 affects running deployments, it might also be worth a note in the release, or an advisory if you want one.

K-W3 (#105), plus a bug it turned up. **1. Trust IPv4 proxies on a dual-stack socket. This bug affects current releases.** The image listens on `::`, where Node reports an IPv4 peer as `::ffff:10.0.0.5`. No IPv4 range matches that form, so a proxy reaching the webmail over IPv4 was never trusted. Its `X-Forwarded-For` was ignored, and the proxy itself became the client. Behind Caddy, nginx or an ingress, that means: - In password mode, everyone shares one sign-in limit (`LOGIN_RATE_LIMIT`, 10 per 15 minutes by default). One person guessing locks everyone out. - The logs and the session list name the proxy instead of the user. The peer is now read in its plain IPv4 form, as the forwarded chain already was. Verified in Node: before the fix, `resolveClientIp("::ffff:10.244.1.5", {forwardedFor: "203.0.113.7"})` returned the proxy. **2. Tell the mail server who each request is for.** Requests to the mail server now carry `X-Forwarded-For` with the client address the webmail resolved, replacing anything the browser sent. This goes through `upstreamFetch` in `clientcontext.ts`, using AsyncLocalStorage set by a middleware. Without it, the server's limits and bans count all of a replica's users as one address. Fetches for remote images and calendars use a separate path and never carry the header. Requests outside a browser request, such as push, carry none. **3. README: "Several replicas".** The webmail's limits count per replica and are a first line only. The server's limits hold across replicas, and need the server to believe the header (`INBUXA_HTTP_USE_FORWARDED`, inbuxa-server PR). **Tested** - 4 new unit tests, including one through the real app with a stand-in server. Typecheck, the full suite (316) and the build pass. - On kind, two replicas behind port-forwards acting as the proxy (inbuxa-k8s-lab `ratelimit-test.sh`): - Replica A refuses one client at attempt 201. - Replica B still starts that client's sign-in. - Replica A still starts a second client's sign-in, which shows it counts the browser and not the proxy. - `frontends-test.sh` passes. Should ship in 27.1-rc.7. Since part 1 affects running deployments, it might also be worth a note in the release, or an advisory if you want one.
jcoffey-dev added 2 commits 2026-11-19 02:46:59 +00:00
Every request the webmail makes comes from the webmail's own address, so
the server's limits and bans counted all of a replica's users as one
client: a hundred wrong passwords in a day between them, or a hundred new
tokens in a minute, locked everyone on that replica out.

Requests to the mail server now carry X-Forwarded-For with the client
address the webmail resolved (TRUSTED_PROXIES), replacing any the browser
sent. Fetches for remote images and calendars never carry it.

README: how the limits behave with several replicas (K-W3).
Trust IPv4 proxies on a dual-stack socket
ci / node (pull_request) Skipped
ci / version (pull_request) Skipped
ci / docker-build (pull_request) Skipped
ci / publish (pull_request) Skipped
github/ci (branch) GitHub Actions
ci / github (pull_request) Successful in 1m45s
ci / announce (pull_request) Skipped
774a2de094
The image listens on ::, where Node reports an IPv4 peer as
::ffff:10.0.0.5. No IPv4 range matches that form, so every proxy reaching
the webmail over IPv4 looked untrusted: its X-Forwarded-For was ignored and
the proxy itself became the client. Behind Caddy, nginx or an ingress, all
users then shared one sign-in limit and one address in the logs.

The peer is now read in its plain IPv4 form, as the forwarded chain
already was.
jcoffey-dev merged commit fb756a8f68 into main 2026-11-19 03:00:29 +00:00
jcoffey-dev deleted branch feat/forward-client-ip 2026-11-19 03:00:30 +00:00
Sign in to join this conversation.