Skip to content

reverseproxy: fix X-Forwarded-* headers for Unix socket requests#7463

Merged
mholt merged 1 commit intocaddyserver:masterfrom
XYenon:fix/reverse-proxy-unix-socket-xff
Feb 10, 2026
Merged

reverseproxy: fix X-Forwarded-* headers for Unix socket requests#7463
mholt merged 1 commit intocaddyserver:masterfrom
XYenon:fix/reverse-proxy-unix-socket-xff

Conversation

@XYenon
Copy link
Contributor

@XYenon XYenon commented Feb 9, 2026

This is a follow-up improvement to #7265, which added trusted_proxies_unix support at the server layer.

Problem

While #7265 correctly determines trust status for Unix domain socket connections in determineTrustedProxy, the reverse_proxy module's addForwardedHeaders function does not account for this case. When RemoteAddr is "@" (Unix socket), net.SplitHostPort fails, causing all X-Forwarded-For, X-Forwarded-Proto, and X-Forwarded-Host headers to be unconditionally stripped — even when the connection is trusted via trusted_proxies_unix.

This means upstream applications behind a reverse_proxy directive never receive forwarded headers when Caddy itself listens on a Unix socket.

Fix

Check for Unix socket connections (RemoteAddr == "@") before attempting SplitHostPort:

  • If the connection is trusted (already determined by the server layer via trusted_proxies_unix), preserve existing X-Forwarded-* headers from trusted proxies.
  • If the connection is not trusted, strip the headers as before for security.

Tests

Added three test cases in headers_test.go:

  • TestAddForwardedHeaders_UnixSocketTrusted — trusted Unix socket with existing headers are preserved
  • TestAddForwardedHeaders_UnixSocketUntrusted — untrusted Unix socket has headers stripped
  • TestAddForwardedHeaders_UnixSocketTrustedNoExistingHeaders — trusted Unix socket with no prior headers falls back to defaults

Assistance Disclosure

Amp (Claude) identified the bug, generated the fix and tests. I reviewed and verified the code is correct.

@XYenon XYenon force-pushed the fix/reverse-proxy-unix-socket-xff branch from 9838a97 to 9e9c49d Compare February 9, 2026 13:17
When a request arrives via a Unix domain socket (RemoteAddr == "@"),
net.SplitHostPort fails, causing addForwardedHeaders to strip all
X-Forwarded-* headers even when the connection is trusted via
trusted_proxies_unix.

Handle Unix socket connections before parsing RemoteAddr: if untrusted,
strip headers for security; if trusted, let clientIP remain empty (no
peer IP for a Unix socket hop) and fall through to the shared header
logic, preserving the existing XFF chain without appending a spurious
entry.

Amp-Thread-ID: https://ampcode.com/threads/T-019c4225-a0ad-7283-ac56-e2c01eae1103
Co-authored-by: Amp <amp@ampcode.com>
@XYenon XYenon force-pushed the fix/reverse-proxy-unix-socket-xff branch from 9e9c49d to 636350d Compare February 9, 2026 13:23
XYenon added a commit to XYenon/nur-packages that referenced this pull request Feb 9, 2026
@XYenon XYenon marked this pull request as ready for review February 9, 2026 13:52
@francislavoie
Copy link
Member

FYI @cseufert

Copy link
Contributor

@cseufert cseufert left a comment

Choose a reason for hiding this comment

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

Yer, this looks good. I wonder how i missed this, although I think fast-cgi/php uses the client ip not the XFF header

LGTM

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thank you for the patch!

@mholt mholt merged commit 03e6e43 into caddyserver:master Feb 10, 2026
27 checks passed
@francislavoie francislavoie added this to the v2.11.0 milestone Feb 10, 2026
@francislavoie francislavoie added the bug 🐞 Something isn't working label Feb 10, 2026
@XYenon XYenon deleted the fix/reverse-proxy-unix-socket-xff branch February 11, 2026 01:32
This was referenced Feb 20, 2026
@francislavoie francislavoie mentioned this pull request Feb 20, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants