Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle X-Forwarded-For for IPv6 correctly #762

Open
ajscholl opened this issue Feb 16, 2022 · 0 comments
Open

Handle X-Forwarded-For for IPv6 correctly #762

ajscholl opened this issue Feb 16, 2022 · 0 comments

Comments

@ajscholl
Copy link
Contributor

ajscholl commented Feb 16, 2022

When determining the IP of the user for a request, we currently don't support IPv6 properly. The reason for this is, that we (or gin) have all IPv4 IPs configured as trusted and not a single IPv6 IP. Thus, we stop the search for the correct IP as soon as we see the first IPv6 IP, causing the following behavior:

Currently working:

  • X-Forwarded-For: IPv4-a, Request: IPv4-b, Result: IPv4-a, Expected: IPv4-a
  • X-Forwarded-For: IPv6-a, Request: IPv4-a, Result: IPv6-a, Expected: IPv6-a
  • X-Forwarded-For: IPv4-a, IPv4-b, Request: IPv4-c, Result: IPv4-a, Expected: IPv4-a
  • X-Forwarded-For: IPv6-a, IPv4-a, Request: IPv4-b, Result: IPv6-a, Expected: IPv6-a

Currently broken:

  • X-Forwarded-For: IPv4-a, IPv6-a, Request: IPv4-b, Result: IPv6-a, Expected: IPv4-a
  • X-Forwarded-For: IPv6-a, IPv6-b, Request: IPv4-a, Result: IPv6-b, Expected: IPv6-a
  • X-Forwarded-For: IPv4-a, Request: IPv6-a, Result: IPv6-a, Expected: IPv4-a
  • X-Forwarded-For: IPv6-a, Request: IPv6-b, Result: IPv6-b, Expected: IPv6-a
  • X-Forwarded-For: IPv4-a, IPv4-b, Request: IPv6-a, Result: IPv6-a, Expected: IPv4-a
  • X-Forwarded-For: IPv6-a, IPv4-a, Request: IPv6-b, Result: IPv6-b, Expected: IPv6-a

The consequence is that you can not run an API server behind a proxy or load balancer which speaks via IPv6 to your server as you will get the IP of the proxy or load balancer instead of the user passed to your code.

TODO:

  • Figure out how to properly configure the trustedCIDRs and trustedProxies fields of the gin engine. The former seems to get the single value 0.0.0.0/0 from go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:31. We could try to add ::0/0 there to support IPv6, too.
  • Ideally, we would only trust our load balancers etc. and properly set their IPs as trusted. However, we need to do so in a way that doesn't require too much maintenance and doesn't break at the most unexpected time.
  • Add some tests to ensure this stays fixed.
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

No branches or pull requests

1 participant