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

Add port number to RealIP middleware #518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion middleware/realip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package middleware
// https://github.com/zenazn/goji/tree/master/web/middleware

import (
"net"
"net/http"
"strings"
)
Expand All @@ -29,7 +30,7 @@ var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
func RealIP(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if rip := realIP(r); rip != "" {
r.RemoteAddr = rip
r.RemoteAddr = net.JoinHostPort(rip, "0")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we add :0 port to the address? This doesn't make sense to me. Can you clarify?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that's the format Go uses for RemoteAddr; see #453

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if the use of port 0 is actually the best way to go?
Since chi is an HTTP router - wouldn't 1.2.3.4 be equal to 1.2.3.4:80 (the default HTTP port)?
Or am I missing something?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's normally set to the source port, not destination port. Any value is "wrong", and at least 0 is "clearly wrong".

}
h.ServeHTTP(w, r)
}
Expand Down
97 changes: 58 additions & 39 deletions middleware/realip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,69 @@ import (
"github.com/go-chi/chi"
)

func TestXRealIP(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("X-Real-IP", "100.100.100.100")
w := httptest.NewRecorder()

r := chi.NewRouter()
r.Use(RealIP)

realIP := ""
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
realIP = r.RemoteAddr
w.Write([]byte("Hello World"))
})
r.ServeHTTP(w, req)

if w.Code != 200 {
t.Fatal("Response Code should be 200")
func TestRealIP(t *testing.T) {
tests := []struct {
name string
remoteAddr string
header http.Header
want string
}{
{
"remote-addr",
"1.1.1.1:42",
nil,
"1.1.1.1:42",
},
{
"x-real-ip",
"1.1.1.1",
http.Header{"X-Real-Ip": {"100.100.100.100"}},
"100.100.100.100:0",
},
{
"x-real-ip-6",
"2001:beef::0",
http.Header{"X-Real-Ip": {"2001:dead::0"}},
"[2001:dead::0]:0",
},
{
"x-forwarded-for",
"1.1.1.1",
http.Header{"X-Forwarded-For": {"100.100.100.100"}},
"100.100.100.100:0",
},
{
"x-forwarded-for-6",
"2001:beef::0",
http.Header{"X-Forwarded-For": {"2001:dead::0"}},
"[2001:dead::0]:0",
},
}

if realIP != "100.100.100.100" {
t.Fatal("Test get real IP error.")
}
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.RemoteAddr = tt.remoteAddr
req.Header = tt.header

func TestXForwardForIP(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("X-Forwarded-For", "100.100.100.100")
w := httptest.NewRecorder()
w := httptest.NewRecorder()
r := chi.NewRouter()
r.Use(RealIP)

r := chi.NewRouter()
r.Use(RealIP)
realIP := ""
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
realIP = r.RemoteAddr
w.Write([]byte("Hello World"))
})
r.ServeHTTP(w, req)

realIP := ""
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
realIP = r.RemoteAddr
w.Write([]byte("Hello World"))
})
r.ServeHTTP(w, req)

if w.Code != 200 {
t.Fatal("Response Code should be 200")
}
if w.Code != 200 {
t.Fatalf("wrong response code: %d; wanted 200", w.Code)
}

if realIP != "100.100.100.100" {
t.Fatal("Test get real IP error.")
if realIP != tt.want {
t.Fatalf("wrong IP: %q; wanted %q", realIP, tt.want)
}
})
}
}