-
-
Notifications
You must be signed in to change notification settings - Fork 996
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
RealIP sets r.RemoteAddr without port #453
Comments
From Go pkg net: https://golang.org/pkg/net/#SplitHostPort
A literal IPv6 address in hostport must be enclosed in square brackets, as in "[::1]:80", "[::1%lo0]:80 Your IPv6 need to follow these recomendations. |
@Dirbaio is correct. The
Maybe changing the |
I'm using this in my code in place of Chi's func RealIP(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if rip := realIP(r); rip != "" {
r.RemoteAddr = net.JoinHostPort(rip, "0")
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
} This would be a breaking change, of course. |
this is an interesting point. any thoughts on the ideal solution? |
Maybe you could take the port from the existing |
alternatively, RealIP can change so it doesn't override r.RemoteAddr at all, and instead it will set a "RealIP" on the request context, I think that would be cleaner |
Please, have a look at this proposal: #967 |
The Go documentation states the following on
http.Request.RemoteAddr
:Therefore, you could expect that a code like this is correct:
However, the RealIP middleware is just copying the
X-Forwarded-For
value intor.RemoteAddr
, which usually does not contain a port, making the code fail:Perhaps RealIP should try to parse
X-Forwarded-For
for ahost:port
, and if it isn't, add a port? Maybe0
to denote the port is unknown, like1.2.3.4
->1.2.3.4:0
?This is particularly frustrating with IPv6 addresses because they have colons but they're not the
host:port
colon, making it hard to parse the RemoteAddr in user code.The text was updated successfully, but these errors were encountered: