Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Commit c5fe960

Browse files
authored
Look for client IP as the second-to-last entry in the IP list (#2380)
Also add a debug log for when we fall back to rate limiting by IP address.
1 parent da995ad commit c5fe960

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

pkg/controller/middleware/firewall_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestProcessFirewall(t *testing.T) {
9797
{
9898
name: "single_allowed_xff",
9999
ctx: controller.WithRealm(ctx, &database.Realm{
100-
AllowedCIDRsServer: []string{"1.2.3.4/32"},
100+
AllowedCIDRsServer: []string{"5.6.7.8/32"},
101101
}),
102102
remoteAddr: "9.8.7.6",
103103
xff: "5.6.7.8, 1.2.3.4",

pkg/ratelimit/limitware/middleware.go

+4
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,14 @@ func UserIDKeyFunc(ctx context.Context, scope string, hmacKey []byte) httplimit.
194194

195195
// IPAddressKeyFunc uses the client IP to rate limit.
196196
func IPAddressKeyFunc(ctx context.Context, scope string, hmacKey []byte) httplimit.KeyFunc {
197+
logger := logging.FromContext(ctx)
198+
197199
return func(r *http.Request) (string, error) {
198200
// Get the remote addr
199201
ip := realip.FromGoogleCloud(r)
200202

203+
logger.Debugw("falling back to rate limiting by ip address", "address", ip)
204+
201205
dig, err := digest.HMAC(ip, hmacKey)
202206
if err != nil {
203207
return "", fmt.Errorf("failed to digest ip: %w", err)

pkg/realip/realip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func FromGoogleCloud(r *http.Request) string {
4141
if xff != "" {
4242
parts := strings.Split(xff, ",")
4343
if len(parts) > 1 {
44-
ip = parts[len(parts)-1]
44+
ip = parts[len(parts)-2]
4545
}
4646
}
4747

pkg/realip/realip_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ func TestOnGoogleCloud(t *testing.T) {
5151
{
5252
name: "xff_multi",
5353
xff: "34.1.2.3,231.5.4.3,2.2.2.2",
54-
exp: "2.2.2.2",
54+
exp: "231.5.4.3",
5555
},
5656
{
5757
name: "xff_multi_trim",
5858
xff: " 34.1.2.3, 231.5.4.3,2.2.2.2",
59-
exp: "2.2.2.2",
59+
exp: "231.5.4.3",
6060
},
6161
{
6262
name: "remote_addr_with_xff",
6363
remoteAddr: "1.1.1.1",
6464
xff: "34.1.2.3,231.5.4.3,2.2.2.2",
65-
exp: "2.2.2.2",
65+
exp: "231.5.4.3",
6666
},
6767
}
6868

0 commit comments

Comments
 (0)