diff --git a/oauthproxy.go b/oauthproxy.go index 895f61a2..1c81cc2d 100644 --- a/oauthproxy.go +++ b/oauthproxy.go @@ -611,9 +611,7 @@ func (p *OAuthProxy) isAPIPath(req *http.Request) bool { // isTrustedIP is used to check if a request comes from a trusted client IP address. func (p *OAuthProxy) isTrustedIP(req *http.Request) bool { - // RemoteAddr @ means unix socket - // https://github.com/golang/go/blob/0fa53e41f122b1661d0678a6d36d71b7b5ad031d/src/syscall/syscall_linux.go#L506-L511 - if p.trustedIPs == nil && req.RemoteAddr != "@" { + if p.trustedIPs == nil { return false } diff --git a/oauthproxy_test.go b/oauthproxy_test.go index 69951375..1082db96 100644 --- a/oauthproxy_test.go +++ b/oauthproxy_test.go @@ -2149,6 +2149,32 @@ func TestTrustedIPs(t *testing.T) { }(), expectTrusted: false, }, + // Check Unix socket with no trusted IPs configured does not error. + { + name: "UnixSocketWithoutTrustedIPs", + trustedIPs: nil, + reverseProxy: false, + realClientIPHeader: "X-Real-IP", + req: func() *http.Request { + req, _ := http.NewRequest("GET", "/", nil) + req.RemoteAddr = "@" + return req + }(), + expectTrusted: false, + }, + // Check Unix socket with trusted IPs configured returns false (no IP to match). + { + name: "UnixSocketWithTrustedIPs", + trustedIPs: []string{"127.0.0.1"}, + reverseProxy: false, + realClientIPHeader: "X-Real-IP", + req: func() *http.Request { + req, _ := http.NewRequest("GET", "/", nil) + req.RemoteAddr = "@" + return req + }(), + expectTrusted: false, + }, // Check using req.RemoteAddr (Options.ReverseProxy == false). { name: "WithRemoteAddr", diff --git a/pkg/ip/realclientip.go b/pkg/ip/realclientip.go index 8bfc7ea3..db8f2595 100644 --- a/pkg/ip/realclientip.go +++ b/pkg/ip/realclientip.go @@ -73,6 +73,12 @@ func GetClientIP(p ipapi.RealClientIPParser, req *http.Request) (net.IP, error) // getRemoteIP obtains the IP of the low-level connected network host func getRemoteIP(req *http.Request) (net.IP, error) { + // Unix domain sockets set RemoteAddr to "@" which has no meaningful IP. + // https://github.com/golang/go/blob/0fa53e41f122b1661d0678a6d36d71b7b5ad031d/src/syscall/syscall_linux.go#L506-L511 + if req.RemoteAddr == "@" { + return nil, nil + } + //revive:disable:indent-error-flow if ipStr, _, err := net.SplitHostPort(req.RemoteAddr); err != nil { return nil, fmt.Errorf("unable to get ip and port from http.RemoteAddr (%s)", req.RemoteAddr) @@ -94,7 +100,7 @@ func GetClientString(p ipapi.RealClientIPParser, req *http.Request, full bool) ( } var remoteIPStr string - if remoteIP, err := getRemoteIP(req); err == nil { + if remoteIP, err := getRemoteIP(req); err == nil && remoteIP != nil { remoteIPStr = remoteIP.String() } diff --git a/pkg/ip/realclientip_test.go b/pkg/ip/realclientip_test.go index c56e0170..3cbca114 100644 --- a/pkg/ip/realclientip_test.go +++ b/pkg/ip/realclientip_test.go @@ -112,6 +112,8 @@ func TestGetRemoteIP(t *testing.T) { errString string expectedIP net.IP }{ + // Unix domain sockets set RemoteAddr to "@" + {"@", "", nil}, {"", "unable to get ip and port from http.RemoteAddr ()", nil}, {"nil", "unable to get ip and port from http.RemoteAddr (nil)", nil}, {"235.28.129.186", "unable to get ip and port from http.RemoteAddr (235.28.129.186)", nil}, @@ -155,6 +157,8 @@ func TestGetClientString(t *testing.T) { }{ // Should fail quietly, only printing warnings to the log {nil, "", "", "", ""}, + // Unix domain socket — no IP available + {nil, "@", "", "", ""}, {p, "127.0.0.1:11950", "", "127.0.0.1", "127.0.0.1"}, {p, "[::1]:28660", "99.103.56.12", "99.103.56.12", "::1 (99.103.56.12)"}, {nil, "10.254.244.165:62750", "", "10.254.244.165", "10.254.244.165"},