Skip to content

Commit 4682afa

Browse files
author
Ajay Kelkar
authored
fix: x-forwarded-for header parsing (#2807)
1 parent a6f2793 commit 4682afa

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

session/session.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ func (s *Session) SaveSessionDeviceInformation(r *http.Request) {
242242
device.IPAddress = &trueClientIP
243243
} else if realClientIP := r.Header.Get("X-Real-IP"); realClientIP != "" {
244244
device.IPAddress = &realClientIP
245-
} else if forwardedIP := r.Header["X-Forwarded-For"]; len(forwardedIP) != 0 {
246-
ip, _ := httpx.GetClientIPAddress(forwardedIP, httpx.InternalIPSet)
245+
} else if forwardedIP := r.Header.Get("X-Forwarded-For"); forwardedIP != "" {
246+
ip, _ := httpx.GetClientIPAddress(strings.Split(forwardedIP, ","), httpx.InternalIPSet)
247247
device.IPAddress = &ip
248248
} else {
249249
device.IPAddress = &r.RemoteAddr

session/session_test.go

+44-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,46 @@ func TestSession(t *testing.T) {
7373
})
7474

7575
t.Run("case=client information reverse proxy forward", func(t *testing.T) {
76+
for _, tc := range []struct {
77+
input string
78+
expected string
79+
}{
80+
{
81+
input: "10.10.8.1, 172.19.2.7",
82+
expected: "",
83+
},
84+
{
85+
input: "217.73.188.139,162.158.203.149, 172.19.2.7",
86+
expected: "162.158.203.149",
87+
},
88+
{
89+
input: "122.122.122.122 , 123.123.123.123",
90+
expected: "123.123.123.123",
91+
},
92+
} {
93+
t.Run("case=parse "+tc.input, func(t *testing.T) {
94+
req := x.NewTestHTTPRequest(t, "GET", "/sessions/whoami", nil)
95+
req.Header["User-Agent"] = []string{"Mozilla/5.0 (X11; Linux x86_64)", "AppleWebKit/537.36 (KHTML, like Gecko)", "Chrome/51.0.2704.103 Safari/537.36"}
96+
req.Header.Set("X-Forwarded-For", tc.input)
97+
98+
s := session.NewInactiveSession()
99+
require.NoError(t, s.Activate(req, &identity.Identity{State: identity.StateActive}, conf, authAt))
100+
assert.True(t, s.Active)
101+
assert.Equal(t, identity.NoAuthenticatorAssuranceLevel, s.AuthenticatorAssuranceLevel)
102+
assert.Equal(t, authAt, s.AuthenticatedAt)
103+
assert.Equal(t, 1, len(s.Devices))
104+
assert.Equal(t, s.ID.String(), s.Devices[0].SessionID.String())
105+
assert.Equal(t, tc.expected, *s.Devices[0].IPAddress)
106+
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Devices[0].UserAgent)
107+
assert.Equal(t, "", *s.Devices[0].Location)
108+
})
109+
}
110+
})
111+
112+
t.Run("case=client information reverse proxy real IP set", func(t *testing.T) {
76113
req := x.NewTestHTTPRequest(t, "GET", "/sessions/whoami", nil)
77114
req.Header["User-Agent"] = []string{"Mozilla/5.0 (X11; Linux x86_64)", "AppleWebKit/537.36 (KHTML, like Gecko)", "Chrome/51.0.2704.103 Safari/537.36"}
115+
req.Header.Set("X-Real-IP", "54.155.246.155")
78116
req.Header["X-Forwarded-For"] = []string{"54.155.246.232", "10.145.1.10"}
79117

80118
s := session.NewInactiveSession()
@@ -84,16 +122,18 @@ func TestSession(t *testing.T) {
84122
assert.Equal(t, authAt, s.AuthenticatedAt)
85123
assert.Equal(t, 1, len(s.Devices))
86124
assert.Equal(t, s.ID.String(), s.Devices[0].SessionID.String())
87-
assert.Equal(t, "54.155.246.232", *s.Devices[0].IPAddress)
125+
assert.NotNil(t, s.Devices[0].UpdatedAt)
126+
assert.NotNil(t, s.Devices[0].CreatedAt)
127+
assert.Equal(t, "54.155.246.155", *s.Devices[0].IPAddress)
88128
assert.Equal(t, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", *s.Devices[0].UserAgent)
89129
assert.Equal(t, "", *s.Devices[0].Location)
90130
})
91131

92-
t.Run("case=client information reverse proxy real IP set", func(t *testing.T) {
132+
t.Run("case=client information CF true client IP set", func(t *testing.T) {
93133
req := x.NewTestHTTPRequest(t, "GET", "/sessions/whoami", nil)
94134
req.Header["User-Agent"] = []string{"Mozilla/5.0 (X11; Linux x86_64)", "AppleWebKit/537.36 (KHTML, like Gecko)", "Chrome/51.0.2704.103 Safari/537.36"}
95-
req.Header.Set("X-Real-IP", "54.155.246.155")
96-
req.Header["X-Forwarded-For"] = []string{"54.155.246.232", "10.145.1.10"}
135+
req.Header.Set("True-Client-IP", "54.155.246.155")
136+
req.Header.Set("X-Forwarded-For", "217.73.188.139,162.158.203.149, 172.19.2.7")
97137

98138
s := session.NewInactiveSession()
99139
require.NoError(t, s.Activate(req, &identity.Identity{State: identity.StateActive}, conf, authAt))

0 commit comments

Comments
 (0)