Skip to content

🐛 [Bug]: Strip all TrustedProxy IPs from ProxyHeader (X-Forwarded-For) #4391

@gueuselambix

Description

@gueuselambix

Bug Description

Fiber only strips of the first TrustedProxy IP from the ProxyHeader (typically X-Forwarded-For).

It should keep stripping off TrustedProxy IPs until it reaches the first non-trusted IP.

The doc now says:

Behavior note: X-Forwarded-For often contains a comma-separated chain of IP addresses. With the default EnableIPValidation = false, c.IP() will return the raw header value (the whole chain) rather than a single parsed client IP. With EnableIPValidation = true, c.IP() parses the header and returns the first syntactically valid IP address it finds; it does not walk the chain to find the first non-proxy hop. For a reliable client IP, configure your reverse proxy to overwrite or sanitize this header and/or to provide a single-IP header such as "X-Real-IP" or a provider-specific header like "CF-Connecting-IP".

Why does the code not walk the chain to find the first non-proxy hop?

How to Reproduce

Steps to reproduce the behavior:

  1. Configure Fiber with TrustedProxy subnets: "2001:db8::/32", "192.168.0.0/24".
  2. Send a X-Forwarded-For request header with 1.2.3.4, 2001:db8::10, 192.168.0.10
  3. c.IP() now returns 1.2.3.4, 2001:db8::10

Expected Behavior

As mentioned in https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Forwarded-For
The header can contain N-proxies:

X-Forwarded-For: <client>, <proxy>
X-Forwarded-For: <client>, <proxy>, …, <proxyN>

The server should strip all trusted proxy addresses from the list.

Fiber Version

v2.52.13

Code Snippet (optional)

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions