Firewall icon indicating copy to clipboard operation
Firewall copied to clipboard

X-Forwarded-For HTTP Header isn't used

Open nickntg opened this issue 2 years ago • 7 comments
trafficstars

I think that the value of X-Forwarded-For is not set in the RemoteIpAddress as stated in the doc. A custom rule is required for load balancers forwarding this header.

nickntg avatar May 25 '23 18:05 nickntg

Proposing PR #12 for this issue.

nickntg avatar Jun 11 '23 08:06 nickntg

@nickntg Do you know of a temporary workaround for this while we wait for the fix to be merged?

Crownpack07 avatar Feb 23 '24 14:02 Crownpack07

The RemoteIpAddress should be set automatically by the corresponding ASP.NET Core middleware if you have configured it correctly. Obviously you must plug the forwarded for middleware before the firewall middleware for it to be picked up by the firewall as well. If the RemoteIpAddress is not set correctly then the forwarded for middleware is misconfigured. I don't think there is a bug in the firewall here. Maybe show me some code of your middlewares and I can help diagnose the issue.

dustinmoris avatar Feb 26 '24 17:02 dustinmoris

A common error I see if that the Forwarded header middleware only trusts localhost proxies by default. The RemoteIpAddress etc will not be set if the proxy if not running on locahost. See https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-8.0.

The following shows how to bypass that for testing, for prod you should specify the proxy - see example

       // Sort out headers so works in front of a proxy
        var config = new ForwardedHeadersOptions()
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto,
            ForwardLimit = 100
        };
        // https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-8.0
        // By default the code only trusts localhost proxies, which doesn't help us in Docker or K8s.
        config.KnownProxies.Clear();
        config.KnownNetworks.Clear(); ;
        app.UseForwardedHeaders(config);

davidglassborow avatar Feb 26 '24 18:02 davidglassborow

The RemoteIpAddress should be set automatically by the corresponding ASP.NET Core middleware if you have configured it correctly. Obviously you must plug the forwarded for middleware before the firewall middleware for it to be picked up by the firewall as well. If the RemoteIpAddress is not set correctly then the forwarded for middleware is misconfigured. I don't think there is a bug in the firewall here. Maybe show me some code of your middlewares and I can help diagnose the issue.

So with this, I have added the configuration as it is highlighted in the docs, see the screenshot below (we are using this along with Yarp), and it seems to work on my local machine but does not map the remote IP address correctly in our Kubernetes environment where the traffic is controlled using an nginx controller that plays a load balancer role, it keeps the Nginx pod's IP address as the remote IP address.

image This is just a snippet of where the firewall is configured with some debug logs when the access denied delegate is triggered.

Could the problem be the fact that I am configuring the firewall on the Yarp pipeline?

Crownpack07 avatar Feb 27 '24 07:02 Crownpack07

The RemoteIpAddress should be set automatically by the corresponding ASP.NET Core middleware if you have configured it correctly. Obviously you must plug the forwarded for middleware before the firewall middleware for it to be picked up by the firewall as well. If the RemoteIpAddress is not set correctly then the forwarded for middleware is misconfigured. I don't think there is a bug in the firewall here. Maybe show me some code of your middlewares and I can help diagnose the issue.

So with this, I have added the configuration as it is highlighted in the docs, see the screenshot below (we are using this along with Yarp), and it seems to work on my local machine but does not map the remote IP address correctly in our Kubernetes environment where the traffic is controlled using an nginx controller that plays a load balancer role, it keeps the Nginx pod's IP address as the remote IP address.

image This is just a snippet of where the firewall is configured with some debug logs when the access denied delegate is triggered.

Could the problem be the fact that I am configuring the firewall on the Yarp pipeline?

Read my comment - you need to either clear the KnownProxies, or add your K8 ingress as a KnownProxy.

davidglassborow avatar Feb 27 '24 07:02 davidglassborow

@davidglassborow It worked, thank you very much for the assistance

Crownpack07 avatar Feb 27 '24 10:02 Crownpack07