sebest / xff Goto Github PK
View Code? Open in Web Editor NEWA Golang Middleware to handle X-Forwarded-For Header
License: MIT License
A Golang Middleware to handle X-Forwarded-For Header
License: MIT License
The privateMasks
list is incomplete. Here is a good list to use:
https://github.com/letsencrypt/boulder/blob/30a516737c9daa4c88c8c47070c25a5e7033cdcf/bdns/dns.go#L31-L145
(Then you can also remove the ip.IsGlobalUnicast
check.)
Hello,
I have noticed that your code does not have a license. Are the terms of the MIT License okay with you?
IsPublicIP is called from Parse, this applies even if we've configured AllowedSubnets
. I'd still argue IsPublicIP does not belong as a filter by default since there's no reason why a proxy must be globally addressable (think entirely private services), but that could be a dangerous change to make at this point.
I'd be happy to come up with a PR for this, is there some form you'd like to see this implemented?
The README says that xff is intended to parse the RFC 7239 Forwarded
header, but it's not -- it parses the X-Forwarded-For
header. These are very different things.
https://husobee.github.io/golang/ip-address/2015/12/17/remote-ip-go.html mentions:
Instead of walking from the left to right, walk backwards through the number of ip addresses by the number of proxies you have in your environment to the internet. That way, you will be adverse to any mucking with the X-Forwarded-For header by the client.
So the code that he/she suggests is the following:
func getIPAdress(r *http.Request) string {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
addresses := strings.Split(r.Header.Get(h), ",")
// march from right to left until we get a public address
// that will be the address right before our proxy.
for i := len(addresses) -1 ; i >= 0; i-- {
ip := addresses[i]
// header can contain spaces too, strip those out.
realIP := net.ParseIP(strings.Replace(ip, " ", "", -1))
if !realIP.IsGlobalUnicast() && !isPrivateSubnet(realIP) {
// bad address, go to next
continue
}
return ip
}
}
return ""
}
Thoughts?
Line 72 in 6c115e0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.