package middleware import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/gin-gonic/gin" "github.com/phishingclub/phishingclub/utils" ) // TestUseClientIP: the client IP comes from the forwarding header only // when the connection is from a trusted proxy. A client that connects // directly and sends the header keeps its own address, with and without // proxies set. Only the set header is read, and in a list the entry the // proxy added wins over what the client sent. func TestUseClientIP(t *testing.T) { gin.SetMode(gin.TestMode) clientIP := func(proxies []string, header string, remoteAddr string, headers map[string]string) string { t.Helper() r := gin.New() if err := UseClientIP(r, utils.IPTrust{Proxies: proxies, Header: header}); err != nil { t.Fatal(err) } got := "" r.GET("/", func(c *gin.Context) { got = c.ClientIP() }) req := httptest.NewRequest(http.MethodGet, "/", nil) req.RemoteAddr = remoteAddr for k, v := range headers { // a | parts the lines of a header that is sent more than once for _, line := range strings.Split(v, "|") { req.Header.Add(k, line) } } r.ServeHTTP(httptest.NewRecorder(), req) return got } proxies := []string{"10.0.0.0/8"} forged := map[string]string{"X-Real-IP": "127.0.0.1"} // a direct client can not pick its address if got := clientIP(proxies, "X-Real-IP", "203.0.113.9:1234", forged); got != "203.0.113.9" { t.Fatalf("a direct client set its own IP with the header: %s", got) } // with a header set but no proxies, no header is read if got := clientIP(nil, "X-Real-IP", "203.0.113.9:1234", forged); got != "203.0.113.9" { t.Fatalf("the header was read without trusted proxies: %s", got) } for _, h := range []string{"X-Forwarded-For", "X-Real-IP"} { if got := clientIP(nil, "", "203.0.113.9:1234", map[string]string{h: "127.0.0.1"}); got != "203.0.113.9" { t.Fatalf("%s was read without trusted proxies: %s", h, got) } } // a trusted proxy tells the client IP if got := clientIP(proxies, "X-Real-IP", "10.0.0.5:1234", map[string]string{"X-Real-IP": "198.51.100.7"}); got != "198.51.100.7" { t.Fatalf("the header of a trusted proxy was not read: %s", got) } // only the set header is read if got := clientIP(proxies, "X-Real-IP", "10.0.0.5:1234", map[string]string{"X-Forwarded-For": "127.0.0.1"}); got != "10.0.0.5" { t.Fatalf("another header than the set one was read: %s", got) } // the client sent the left entry, the proxy added the right one if got := clientIP(proxies, "X-Forwarded-For", "10.0.0.5:1234", map[string]string{"X-Forwarded-For": "127.0.0.1, 198.51.100.7"}); got != "198.51.100.7" { t.Fatalf("an entry the client sent was used: %s", got) } // the client sent the first line, the proxy added the second if got := clientIP(proxies, "X-Real-IP", "10.0.0.5:1234", map[string]string{"X-Real-IP": "127.0.0.1|198.51.100.7"}); got != "198.51.100.7" { t.Fatalf("a header line the client sent was used: %s", got) } // a port after the address is left out if got := clientIP(proxies, "X-Forwarded-For", "10.0.0.5:1234", map[string]string{"X-Forwarded-For": "198.51.100.7:51234"}); got != "198.51.100.7" { t.Fatalf("an address with a port was not read: %s", got) } // a value that is not an IP is not used if got := clientIP(proxies, "X-Real-IP", "10.0.0.5:1234", map[string]string{"X-Real-IP": "not an ip"}); got != "10.0.0.5" { t.Fatalf("a value that is not an IP was used: %s", got) } }