Debian Codegraph @

realclientip_test.go

raw symbols SCIP
1 // SPDX: 0BSD
2
3 package realclientip
4
5 import (
6 "fmt"
7 "net"
8 "net/http"
9 "reflect"
10 "testing"
11
12 "github.com/realclientip/realclientip-go/ranges"
13 )
14
15 /*
16 IP types and formats to test:
17
18 IPv4 with no port
19 192.0.2.60
20
21 IPv4 with port
22 192.0.2.60:4833
23
24 IPv6 with no port
25 2607:f8b0:4004:83f::200e
26
27 IPv6 with port
28 [2607:f8b0:4004:83f::200e]:4711
29
30 IPv6 with zone and no port
31 fe80::abcd%zone
32
33 IPv6 with port and zone
34 [fe80::abcd%zone]:4711
35
36 IPv4-mapped IPv6
37 ::ffff:188.0.2.128
38
39 IPv4-mapped IPv6 with port
40 [::ffff:188.0.2.128]:48483
41
42 IPv4-mapped IPv6 in IPv6 (hex) form
43 ::ffff:bc15:0006
44 (this is 188.0.2.128; for an internal address use ::ffff:ac15:0006)
45
46 NAT64 IPv4-mapped IPv6
47 64:ff9b::188.0.2.128
48 (net.ParseIP converts to 64:ff9b::bc00:280, but netip.ParseAddr doesn't)
49
50 IPv4 loopback
51 127.0.0.1
52
53 IPv6 loopback
54 ::1
55
56 Forwarded header tests also require testing with quotes around full address.
57 */
58
59 func ipAddrsEqual(a, b net.IPAddr) bool {
60 return a.IP.Equal(b.IP) && a.Zone == b.Zone
61 }
62
63 func TestRemoteAddrStrategy(t *testing.T) {
64 // Ensure the strategy interface is implemented
65 var _ Strategy = RemoteAddrStrategy{}
66
67 type args struct {
68 headers http.Header
69 remoteAddr string
70 }
71 tests := []struct {
72 name string
73 args args
74 want string
75 }{
76 {
77 name: "IPv4 with port",
78 args: args{
79 remoteAddr: "2.2.2.2:1234",
80 },
81 want: "2.2.2.2",
82 },
83 {
84 name: "IPv4 with no port",
85 args: args{
86 remoteAddr: "2.2.2.2",
87 },
88 want: "2.2.2.2",
89 },
90 {
91 name: "IPv6 with port",
92 args: args{
93 remoteAddr: "[2607:f8b0:4004:83f::18]:3838",
94 },
95 want: "2607:f8b0:4004:83f::18",
96 },
97 {
98 name: "IPv6 with no port",
99 args: args{
100 remoteAddr: "2607:f8b0:4004:83f::18",
101 },
102 want: "2607:f8b0:4004:83f::18",
103 },
104 {
105 name: "IPv6 with zone and no port",
106 args: args{
107 remoteAddr: `fe80::1111%eth0`,
108 },
109 want: `fe80::1111%eth0`,
110 },
111 {
112 name: "IPv6 with zone and port",
113 args: args{
114 remoteAddr: `[fe80::2222%eth0]:4848`,
115 },
116 want: `fe80::2222%eth0`,
117 },
118 {
119 name: "IPv4-mapped IPv6",
120 args: args{
121 remoteAddr: "[::ffff:172.21.0.6]:4747",
122 },
123 // It is okay that this converts to the IPv4 format, since it's most important
124 // that the respresentation be consistent. It might also be good that it does,
125 // so that it will match the same plain IPv4 address.
126 // net/netip.ParseAddr gives a different form: "::ffff:172.21.0.6"
127 want: "172.21.0.6",
128 },
129 {
130 name: "IPv4-mapped IPv6 in IPv6 form",
131 args: args{
132 remoteAddr: "0:0:0:0:0:ffff:bc15:0006",
133 },
134 // net/netip.ParseAddr gives a different form: "::ffff:188.21.0.6"
135 want: "188.21.0.6",
136 },
137 {
138 name: "NAT64 IPv4-mapped IPv6",
139 args: args{
140 remoteAddr: "[64:ff9b::188.0.2.128]:4747",
141 },
142 // net.ParseIP and net/netip.ParseAddr convert to this. This is fine, as it is
143 // done consistently.
144 want: "64:ff9b::bc00:280",
145 },
146 {
147 name: "6to4 IPv4-mapped IPv6",
148 args: args{
149 remoteAddr: "[2002:c000:204::]:4747",
150 },
151 want: "2002:c000:204::",
152 },
153 {
154 name: "IPv4 loopback",
155 args: args{
156 remoteAddr: "127.0.0.1",
157 },
158 want: "127.0.0.1",
159 },
160 {
161 name: "IPv6 loopback",
162 args: args{
163 remoteAddr: "::1",
164 },
165 want: "::1",
166 },
167 {
168 name: "Garbage header (unused)",
169 args: args{
170 headers: http.Header{"X-Forwarded-For": []string{"!!!"}},
171 remoteAddr: "2.2.2.2:1234",
172 },
173 want: "2.2.2.2",
174 },
175 {
176 name: "Fail: empty RemoteAddr",
177 args: args{
178 remoteAddr: "",
179 },
180 want: "",
181 },
182 {
183 name: "Fail: garbage RemoteAddr",
184 args: args{
185 remoteAddr: "ohno",
186 },
187 want: "",
188 },
189 {
190 name: "Fail: zero RemoteAddr IP",
191 args: args{
192 remoteAddr: "0.0.0.0",
193 },
194 want: "",
195 },
196 {
197 name: "Fail: unspecified RemoteAddr IP",
198 args: args{
199 remoteAddr: "::",
200 },
201 want: "",
202 },
203 {
204 name: "Fail: Unix domain socket",
205 args: args{
206 remoteAddr: "@",
207 },
208 want: "",
209 },
210 }
211 for _, tt := range tests {
212 t.Run(tt.name, func(t *testing.T) {
213 strat := RemoteAddrStrategy{}
214 if got := strat.ClientIP(tt.args.headers, tt.args.remoteAddr); got != tt.want {
215 t.Fatalf("ClientIP = %q, want %q", got, tt.want)
216 }
217 })
218 }
219 }
220
221 func TestSingleIPHeaderStrategy(t *testing.T) {
222 // Ensure the strategy interface is implemented
223 var _ Strategy = SingleIPHeaderStrategy{}
224
225 type args struct {
226 headerName string
227 headers http.Header
228 remoteAddr string
229 }
230 tests := []struct {
231 name string
232 args args
233 want string
234 wantErr bool
235 }{
236 {
237 name: "IPv4 with port",
238 args: args{
239 headerName: "True-Client-IP",
240 headers: http.Header{
241 "X-Real-Ip": []string{"1.1.1.1"},
242 "True-Client-Ip": []string{"2.2.2.2:49489"},
243 "X-Forwarded-For": []string{"3.3.3.3"}},
244 },
245 want: "2.2.2.2",
246 },
247 {
248 name: "IPv4 with no port",
249 args: args{
250 headerName: "X-Real-IP",
251 headers: http.Header{
252 "X-Real-Ip": []string{"1.1.1.1"},
253 "True-Client-Ip": []string{"2.2.2.2:49489"},
254 "X-Forwarded-For": []string{"3.3.3.3"}},
255 },
256 want: "1.1.1.1",
257 },
258 {
259 name: "IPv6 with port",
260 args: args{
261 headerName: "X-Real-IP",
262 headers: http.Header{
263 "X-Real-Ip": []string{"[2607:f8b0:4004:83f::18]:3838"},
264 "True-Client-Ip": []string{"2.2.2.2:49489"},
265 "X-Forwarded-For": []string{"3.3.3.3"}},
266 },
267 want: "2607:f8b0:4004:83f::18",
268 },
269 {
270 name: "IPv6 with no port",
271 args: args{
272 headerName: "X-Real-IP",
273 headers: http.Header{
274 "X-Real-Ip": []string{"2607:f8b0:4004:83f::19"},
275 "True-Client-Ip": []string{"2.2.2.2:49489"},
276 "X-Forwarded-For": []string{"3.3.3.3"}},
277 },
278 want: "2607:f8b0:4004:83f::19",
279 },
280 {
281 name: "IPv6 with zone and no port",
282 args: args{
283 headerName: "a-b-c-d",
284 headers: http.Header{
285 "X-Real-Ip": []string{"2607:f8b0:4004:83f::19"},
286 "A-B-C-D": []string{"fe80::1111%zone"},
287 "X-Forwarded-For": []string{"3.3.3.3"}},
288 },
289 want: "fe80::1111%zone",
290 },
291 {
292 name: "IPv6 with zone and port",
293 args: args{
294 headerName: "a-b-c-d",
295 headers: http.Header{
296 "X-Real-Ip": []string{"2607:f8b0:4004:83f::19"},
297 "A-B-C-D": []string{"[fe80::1111%zone]:4848"},
298 "X-Forwarded-For": []string{"3.3.3.3"}},
299 },
300 want: "fe80::1111%zone",
301 },
302 {
303 name: "IPv6 with brackets but no port",
304 args: args{
305 headerName: "x-real-ip",
306 headers: http.Header{
307 "X-Real-Ip": []string{"2607:f8b0:4004:83f::19"},
308 "A-B-C-D": []string{"[fe80::1111%zone]:4848"},
309 "X-Forwarded-For": []string{"3.3.3.3"}},
310 },
311 want: "2607:f8b0:4004:83f::19",
312 },
313 {
314 name: "IP-mapped IPv6",
315 args: args{
316 headerName: "x-real-ip",
317 headers: http.Header{
318 "X-Real-Ip": []string{"::ffff:172.21.0.6"},
319 "A-B-C-D": []string{"[fe80::1111%zone]:4848"},
320 "X-Forwarded-For": []string{"3.3.3.3"}},
321 },
322 want: "172.21.0.6",
323 },
324 {
325 name: "IPv4-mapped IPv6 in IPv6 form",
326 args: args{
327 headerName: "x-real-ip",
328 headers: http.Header{
329 "X-Real-Ip": []string{"[64:ff9b::188.0.2.128]:4747"},
330 "A-B-C-D": []string{"[fe80::1111%zone]:4848"},
331 "X-Forwarded-For": []string{"3.3.3.3"}},
332 },
333 want: "64:ff9b::bc00:280",
334 },
335 {
336 name: "6to4 IPv4-mapped IPv6",
337 args: args{
338 headerName: "x-real-ip",
339 headers: http.Header{
340 "X-Real-Ip": []string{"2002:c000:204::"},
341 "A-B-C-D": []string{"[fe80::1111%zone]:4848"},
342 "X-Forwarded-For": []string{"3.3.3.3"}},
343 },
344 want: "2002:c000:204::",
345 },
346 {
347 name: "IPv4 loopback",
348 args: args{
349 headerName: "x-real-ip",
350 headers: http.Header{
351 "X-Real-Ip": []string{"127.0.0.1"},
352 "A-B-C-D": []string{"[fe80::1111%zone]:4848"},
353 "X-Forwarded-For": []string{"3.3.3.3"}},
354 },
355 want: "127.0.0.1",
356 },
357 {
358 name: "Fail: missing header",
359 args: args{
360 headerName: "x-real-ip",
361 headers: http.Header{
362 "A-B-C-D": []string{"[fe80::1111%zone]:4848"},
363 "X-Forwarded-For": []string{"3.3.3.3"}},
364 },
365 want: "",
366 },
367 {
368 name: "Fail: garbage IP",
369 args: args{
370 headerName: "True-Client-Ip",
371 headers: http.Header{
372 "X-Real-Ip": []string{"::1"},
373 "True-Client-Ip": []string{"nope"},
374 "X-Forwarded-For": []string{"3.3.3.3"}},
375 },
376 want: "",
377 },
378 {
379 name: "Fail: zero IP",
380 args: args{
381 headerName: "True-Client-Ip",
382 headers: http.Header{
383 "X-Real-Ip": []string{"::1"},
384 "True-Client-Ip": []string{"0.0.0.0"},
385 "X-Forwarded-For": []string{"3.3.3.3"}},
386 },
387 want: "",
388 },
389 {
390 name: "Error: empty header name",
391 args: args{
392 headerName: "",
393 headers: http.Header{
394 "X-Real-Ip": []string{"::1"},
395 "True-Client-Ip": []string{"2.2.2.2"},
396 "X-Forwarded-For": []string{"3.3.3.3"}},
397 },
398 wantErr: true,
399 },
400 {
401 name: "Error: X-Forwarded-For header",
402 args: args{
403 headerName: "X-Forwarded-For",
404 headers: http.Header{
405 "X-Real-Ip": []string{"::1"},
406 "True-Client-Ip": []string{"2.2.2.2"},
407 "X-Forwarded-For": []string{"3.3.3.3"}},
408 },
409 wantErr: true,
410 },
411 }
412 for _, tt := range tests {
413 t.Run(tt.name, func(t *testing.T) {
414 strat, err := NewSingleIPHeaderStrategy(tt.args.headerName)
415 if (err != nil) != tt.wantErr {
416 t.Fatalf("NewSingleIPHeaderStrategy error = %v, wantErr %v", err, tt.wantErr)
417 return
418 }
419
420 if err != nil {
421 // We can't continue
422 return
423 }
424
425 got := strat.ClientIP(tt.args.headers, tt.args.remoteAddr)
426 if !reflect.DeepEqual(got, tt.want) {
427 t.Fatalf("ClientIP = %q, want %q", got, tt.want)
428 }
429 })
430 }
431 }
432
433 func TestLeftmostNonPrivateStrategy(t *testing.T) {
434 // Ensure the strategy interface is implemented
435 var _ Strategy = LeftmostNonPrivateStrategy{}
436
437 type args struct {
438 headerName string
439 headers http.Header
440 remoteAddr string
441 }
442 tests := []struct {
443 name string
444 args args
445 want string
446 wantErr bool
447 }{
448 {
449 name: "IPv4 with port",
450 args: args{
451 headerName: "X-Forwarded-For",
452 headers: http.Header{
453 "X-Real-Ip": []string{`1.1.1.1`},
454 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
455 },
456 },
457 want: "2.2.2.2",
458 },
459 {
460 name: "IPv4 with no port",
461 args: args{
462 headerName: "Forwarded",
463 headers: http.Header{
464 "X-Real-Ip": []string{`1.1.1.1`},
465 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
466 "Forwarded": []string{`For=5.5.5.5`, `For=6.6.6.6`},
467 },
468 },
469 want: "5.5.5.5",
470 },
471 {
472 name: "IPv6 with port",
473 args: args{
474 headerName: "X-Forwarded-For",
475 headers: http.Header{
476 "X-Real-Ip": []string{`1.1.1.1`},
477 "X-Forwarded-For": []string{`[2607:f8b0:4004:83f::18]:3838, 3.3.3.3`, `4.4.4.4`},
478 },
479 },
480 want: "2607:f8b0:4004:83f::18",
481 },
482 {
483 name: "IPv6 with no port",
484 args: args{
485 headerName: "Forwarded",
486 headers: http.Header{
487 "X-Real-Ip": []string{`1.1.1.1`},
488 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
489 "Forwarded": []string{`Host=blah;For="2607:f8b0:4004:83f::18";Proto=https`},
490 },
491 },
492 want: "2607:f8b0:4004:83f::18",
493 },
494 {
495 name: "IPv6 with port and zone",
496 args: args{
497 headerName: "Forwarded",
498 headers: http.Header{
499 "X-Real-Ip": []string{`1.1.1.1`},
500 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
501 "Forwarded": []string{`For=[fe80::1111%zone], Host=blah;For="[2607:f8b0:4004:83f::18%zone]:9943";Proto=https`, `host=what;for=6.6.6.6;proto=https`},
502 },
503 },
504 want: "2607:f8b0:4004:83f::18%zone",
505 },
506 {
507 name: "IPv6 with port and zone, no quotes",
508 args: args{
509 headerName: "Forwarded",
510 headers: http.Header{
511 "X-Real-Ip": []string{`1.1.1.1`},
512 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
513 "Forwarded": []string{`For=[fe80::1111%zone], Host=blah;For=[2607:f8b0:4004:83f::18%zone]:9943;Proto=https`, `host=what;for=6.6.6.6;proto=https`},
514 },
515 },
516 want: "2607:f8b0:4004:83f::18%zone",
517 },
518 {
519 name: "IPv4-mapped IPv6",
520 args: args{
521 headerName: "x-forwarded-for",
522 headers: http.Header{
523 "X-Real-Ip": []string{`1.1.1.1`},
524 "X-Forwarded-For": []string{`::ffff:188.0.2.128, 3.3.3.3`, `4.4.4.4`},
525 "Forwarded": []string{`Host=blah;For="7.7.7.7";Proto=https`, `host=what;for=6.6.6.6;proto=https`},
526 },
527 },
528 want: "188.0.2.128",
529 },
530 {
531 name: "IPv4-mapped IPv6 with port",
532 args: args{
533 headerName: "x-forwarded-for",
534 headers: http.Header{
535 "X-Real-Ip": []string{`1.1.1.1`},
536 "X-Forwarded-For": []string{`[::ffff:188.0.2.128]:48483, 3.3.3.3`, `4.4.4.4`},
537 "Forwarded": []string{`Host=blah;For="7.7.7.7";Proto=https`, `host=what;for=6.6.6.6;proto=https`},
538 },
539 },
540 want: "188.0.2.128",
541 },
542 {
543 name: "IPv4-mapped IPv6 in IPv6 (hex) form",
544 args: args{
545 headerName: "forwarded",
546 headers: http.Header{
547 "X-Real-Ip": []string{`1.1.1.1`},
548 "X-Forwarded-For": []string{`[::ffff:188.0.2.128]:48483, 3.3.3.3`, `4.4.4.4`},
549 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
550 },
551 },
552 want: "188.21.0.6",
553 },
554 {
555 name: "NAT64 IPv4-mapped IPv6",
556 args: args{
557 headerName: "x-forwarded-for",
558 headers: http.Header{
559 "X-Real-Ip": []string{`1.1.1.1`},
560 "X-Forwarded-For": []string{`64:ff9b::188.0.2.128, 3.3.3.3`, `4.4.4.4`},
561 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
562 },
563 },
564 want: "64:ff9b::bc00:280",
565 },
566 {
567 name: "XFF: leftmost not desirable",
568 args: args{
569 headerName: "x-forwarded-for",
570 headers: http.Header{
571 "X-Real-Ip": []string{`1.1.1.1`},
572 "X-Forwarded-For": []string{`::1, nope`, `4.4.4.4, 5.5.5.5`},
573 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
574 },
575 },
576 want: "4.4.4.4",
577 },
578 {
579 name: "Forwarded: leftmost not desirable",
580 args: args{
581 headerName: "Forwarded",
582 headers: http.Header{
583 "X-Real-Ip": []string{`1.1.1.1`},
584 "X-Forwarded-For": []string{`::1, nope`, `4.4.4.4, 5.5.5.5`},
585 "Forwarded": []string{`For="", For="::ffff:192.168.1.1"`, `host=what;for=:48485;proto=https,For="2607:f8b0:4004:83f::18"`},
586 },
587 },
588 want: "2607:f8b0:4004:83f::18",
589 },
590 {
591 name: "Fail: XFF: none acceptable",
592 args: args{
593 headerName: "X-Forwarded-For",
594 headers: http.Header{
595 "X-Real-Ip": []string{`1.1.1.1`},
596 "X-Forwarded-For": []string{`::1, nope, ::, 0.0.0.0`, `192.168.1.1, !?!`},
597 "Forwarded": []string{`For="", For="::ffff:192.168.1.1"`, `host=what;for=:48485;proto=https,For="fe80::abcd%zone"`},
598 },
599 },
600 want: "",
601 },
602 {
603 name: "Fail: Forwarded: none acceptable",
604 args: args{
605 headerName: "Forwarded",
606 headers: http.Header{
607 "X-Real-Ip": []string{`1.1.1.1`},
608 "X-Forwarded-For": []string{`::1, nope`, `192.168.1.1, 2.2.2.2`},
609 "Forwarded": []string{`For="", For="::ffff:192.168.1.1"`, `host=what;for=:48485;proto=https,For="::ffff:ac15:0006%zone",For="::",For=0.0.0.0`},
610 },
611 },
612 want: "",
613 },
614 {
615 name: "Fail: XFF: no header",
616 args: args{
617 headerName: "Forwarded",
618 headers: http.Header{
619 "X-Real-Ip": []string{`1.1.1.1`},
620 "Forwarded": []string{`For="", For="::ffff:192.168.1.1"`, `host=what;for=:48485;proto=https,For="::ffff:ac15:0006%zone"`},
621 },
622 },
623 want: "",
624 },
625 {
626 name: "Fail: Forwarded: no header",
627 args: args{
628 headerName: "forwarded",
629 headers: http.Header{
630 "X-Real-Ip": []string{`1.1.1.1`},
631 "X-Forwarded-For": []string{`64:ff9b::188.0.2.128, 3.3.3.3`, `4.4.4.4`},
632 },
633 },
634 want: "",
635 },
636 {
637 name: "Error: empty header name",
638 args: args{
639 headerName: "",
640 headers: http.Header{
641 "X-Real-Ip": []string{"::1"},
642 "True-Client-Ip": []string{"2.2.2.2"},
643 "X-Forwarded-For": []string{"3.3.3.3"}},
644 },
645 wantErr: true,
646 },
647 {
648 name: "Error: invalid header",
649 args: args{
650 headerName: "X-Real-IP",
651 headers: http.Header{
652 "X-Real-Ip": []string{"::1"},
653 "True-Client-Ip": []string{"2.2.2.2"},
654 "X-Forwarded-For": []string{"3.3.3.3"}},
655 },
656 wantErr: true,
657 },
658 }
659 for _, tt := range tests {
660 t.Run(tt.name, func(t *testing.T) {
661 strat, err := NewLeftmostNonPrivateStrategy(tt.args.headerName)
662 if (err != nil) != tt.wantErr {
663 t.Fatalf("NewLeftmostNonPrivateStrategy error = %v, wantErr %v", err, tt.wantErr)
664 return
665 }
666
667 if err != nil {
668 // We can't continue
669 return
670 }
671
672 got := strat.ClientIP(tt.args.headers, tt.args.remoteAddr)
673 if !reflect.DeepEqual(got, tt.want) {
674 t.Fatalf("ClientIP = %q, want %q", got, tt.want)
675 }
676 })
677 }
678 }
679
680 func TestRightmostNonPrivateStrategy(t *testing.T) {
681 // Ensure the strategy interface is implemented
682 var _ Strategy = RightmostNonPrivateStrategy{}
683
684 type args struct {
685 headerName string
686 headers http.Header
687 remoteAddr string
688 }
689 tests := []struct {
690 name string
691 args args
692 want string
693 wantErr bool
694 }{
695 {
696 name: "IPv4 with port",
697 args: args{
698 headerName: "X-Forwarded-For",
699 headers: http.Header{
700 "X-Real-Ip": []string{`1.1.1.1`},
701 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4:39333`},
702 },
703 },
704 want: "4.4.4.4",
705 },
706 {
707 name: "IPv4 with no port",
708 args: args{
709 headerName: "Forwarded",
710 headers: http.Header{
711 "X-Real-Ip": []string{`1.1.1.1`},
712 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
713 "Forwarded": []string{`For=5.5.5.5`, `For=6.6.6.6`},
714 },
715 },
716 want: "6.6.6.6",
717 },
718 {
719 name: "IPv6 with port",
720 args: args{
721 headerName: "X-Forwarded-For",
722 headers: http.Header{
723 "X-Real-Ip": []string{`1.1.1.1`},
724 "X-Forwarded-For": []string{`[2607:f8b0:4004:83f::18]:3838`},
725 },
726 },
727 want: "2607:f8b0:4004:83f::18",
728 },
729 {
730 name: "IPv6 with no port",
731 args: args{
732 headerName: "Forwarded",
733 headers: http.Header{
734 "X-Real-Ip": []string{`1.1.1.1`},
735 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
736 "Forwarded": []string{`host=what;for=6.6.6.6;proto=https`, `Host=blah;For="2607:f8b0:4004:83f::18";Proto=https`},
737 },
738 },
739 want: "2607:f8b0:4004:83f::18",
740 },
741 {
742 name: "IPv6 with port and zone",
743 args: args{
744 headerName: "Forwarded",
745 headers: http.Header{
746 "X-Real-Ip": []string{`1.1.1.1`},
747 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
748 "Forwarded": []string{`host=what;for=6.6.6.6;proto=https`, `For="[2607:f8b0:4004:83f::18%eth0]:3393";Proto=https`, `Host=blah;For="[fe80::1111%zone]:9943";Proto=https`},
749 },
750 },
751 want: "2607:f8b0:4004:83f::18%eth0",
752 },
753 {
754 name: "IPv6 with port and zone, no quotes",
755 args: args{
756 headerName: "Forwarded",
757 headers: http.Header{
758 "X-Real-Ip": []string{`1.1.1.1`},
759 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
760 "Forwarded": []string{`host=what;for=6.6.6.6;proto=https`, `For="[2607:f8b0:4004:83f::18%eth0]:3393";Proto=https`, `Host=blah;For=[fe80::1111%zone]:9943;Proto=https`},
761 },
762 },
763 want: "2607:f8b0:4004:83f::18%eth0",
764 },
765 {
766 name: "IPv4-mapped IPv6",
767 args: args{
768 headerName: "x-forwarded-for",
769 headers: http.Header{
770 "X-Real-Ip": []string{`1.1.1.1`},
771 "X-Forwarded-For": []string{`3.3.3.3`, `4.4.4.4, ::ffff:188.0.2.128`},
772 "Forwarded": []string{`Host=blah;For="7.7.7.7";Proto=https`, `host=what;for=6.6.6.6;proto=https`},
773 },
774 },
775 want: "188.0.2.128",
776 },
777 {
778 name: "IPv4-mapped IPv6 with port",
779 args: args{
780 headerName: "x-forwarded-for",
781 headers: http.Header{
782 "X-Real-Ip": []string{`1.1.1.1`},
783 "X-Forwarded-For": []string{`3.3.3.3`, `4.4.4.4,[::ffff:188.0.2.128]:48483`},
784 "Forwarded": []string{`Host=blah;For="7.7.7.7";Proto=https`, `host=what;for=6.6.6.6;proto=https`},
785 },
786 },
787 want: "188.0.2.128",
788 },
789 {
790 name: "IPv4-mapped IPv6 in IPv6 (hex) form",
791 args: args{
792 headerName: "forwarded",
793 headers: http.Header{
794 "X-Real-Ip": []string{`1.1.1.1`},
795 "X-Forwarded-For": []string{`[::ffff:188.0.2.128]:48483, 3.3.3.3`, `4.4.4.4`},
796 "Forwarded": []string{`host=what;for=6.6.6.6;proto=https`, `For="::ffff:bc15:0006"`},
797 },
798 },
799 want: "188.21.0.6",
800 },
801 {
802 name: "NAT64 IPv4-mapped IPv6",
803 args: args{
804 headerName: "x-forwarded-for",
805 headers: http.Header{
806 "X-Real-Ip": []string{`1.1.1.1`},
807 "X-Forwarded-For": []string{`3.3.3.3`, `4.4.4.4, 64:ff9b::188.0.2.128`},
808 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
809 },
810 },
811 want: "64:ff9b::bc00:280",
812 },
813 {
814 name: "XFF: rightmost not desirable",
815 args: args{
816 headerName: "x-forwarded-for",
817 headers: http.Header{
818 "X-Real-Ip": []string{`1.1.1.1`},
819 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, nope`},
820 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
821 },
822 },
823 want: "5.5.5.5",
824 },
825 {
826 name: "Forwarded: rightmost not desirable",
827 args: args{
828 headerName: "Forwarded",
829 headers: http.Header{
830 "X-Real-Ip": []string{`1.1.1.1`},
831 "X-Forwarded-For": []string{`::1, nope`, `4.4.4.4, 5.5.5.5`},
832 "Forwarded": []string{`host=what;for=:48485;proto=https,For=2.2.2.2`, `For="", For="::ffff:192.168.1.1"`},
833 },
834 },
835 want: "2.2.2.2",
836 },
837 {
838 name: "Fail: XFF: none acceptable",
839 args: args{
840 headerName: "X-Forwarded-For",
841 headers: http.Header{
842 "X-Real-Ip": []string{`1.1.1.1`},
843 "X-Forwarded-For": []string{`::1, nope`, `192.168.1.1, !?!, ::, 0.0.0.0`},
844 "Forwarded": []string{`For="", For="::ffff:192.168.1.1"`, `host=what;for=:48485;proto=https,For="fe80::abcd%zone"`},
845 },
846 },
847 want: "",
848 },
849 {
850 name: "Fail: Forwarded: none acceptable",
851 args: args{
852 headerName: "Forwarded",
853 headers: http.Header{
854 "X-Real-Ip": []string{`1.1.1.1`},
855 "X-Forwarded-For": []string{`::1, nope`, `192.168.1.1, 2.2.2.2`},
856 "Forwarded": []string{`For="", For="::ffff:192.168.1.1"`, `host=what;for=:48485;proto=https,For="::ffff:ac15:0006%zone", For="::", For=0.0.0.0`},
857 },
858 },
859 want: "",
860 },
861 {
862 name: "Fail: XFF: no header",
863 args: args{
864 headerName: "Forwarded",
865 headers: http.Header{
866 "X-Real-Ip": []string{`1.1.1.1`},
867 "Forwarded": []string{`For="", For="::ffff:192.168.1.1"`, `host=what;for=:48485;proto=https,For="::ffff:ac15:0006%zone"`},
868 },
869 remoteAddr: "9.9.9.9",
870 },
871 want: "",
872 },
873 {
874 name: "Fail: Forwarded: no header",
875 args: args{
876 headerName: "forwarded",
877 headers: http.Header{
878 "X-Real-Ip": []string{`1.1.1.1`},
879 "X-Forwarded-For": []string{`64:ff9b::188.0.2.128, 3.3.3.3`, `4.4.4.4`},
880 },
881 },
882 want: "",
883 },
884 {
885 name: "Error: empty header name",
886 args: args{
887 headerName: "",
888 headers: http.Header{
889 "X-Real-Ip": []string{"::1"},
890 "True-Client-Ip": []string{"2.2.2.2"},
891 "X-Forwarded-For": []string{"3.3.3.3"}},
892 },
893 wantErr: true,
894 },
895 {
896 name: "Error: invalid header",
897 args: args{
898 headerName: "X-Real-IP",
899 headers: http.Header{
900 "X-Real-Ip": []string{"::1"},
901 "True-Client-Ip": []string{"2.2.2.2"},
902 "X-Forwarded-For": []string{"3.3.3.3"}},
903 },
904 wantErr: true,
905 },
906 }
907 for _, tt := range tests {
908 t.Run(tt.name, func(t *testing.T) {
909 strat, err := NewRightmostNonPrivateStrategy(tt.args.headerName)
910 if (err != nil) != tt.wantErr {
911 t.Fatalf("NewRightmostNonPrivateStrategy error = %v, wantErr %v", err, tt.wantErr)
912 return
913 }
914
915 if err != nil {
916 // We can't continue
917 return
918 }
919
920 got := strat.ClientIP(tt.args.headers, tt.args.remoteAddr)
921 if !reflect.DeepEqual(got, tt.want) {
922 t.Fatalf("ClientIP = %q, want %q", got, tt.want)
923 }
924 })
925 }
926 }
927
928 func TestRightmostTrustedCountStrategy(t *testing.T) {
929 // Ensure the strategy interface is implemented
930 var _ Strategy = RightmostTrustedCountStrategy{}
931
932 type args struct {
933 headerName string
934 trustedCount int
935 headers http.Header
936 remoteAddr string
937 }
938 tests := []struct {
939 name string
940 args args
941 want string
942 wantErr bool
943 }{
944 // TODO: Is it okay not to test every IP type, since the logic is sufficiently similar to RightmostNonPrivateStrategy?
945
946 {
947 name: "Count one",
948 args: args{
949 headerName: "Forwarded",
950 trustedCount: 1,
951 headers: http.Header{
952 "X-Real-Ip": []string{`1.1.1.1`},
953 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, fe80::382b:141b:fa4a:2a16%28`},
954 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
955 },
956 },
957 want: "6.6.6.6",
958 },
959 {
960 name: "Count five",
961 args: args{
962 headerName: "X-Forwarded-For",
963 trustedCount: 5,
964 headers: http.Header{
965 "X-Real-Ip": []string{`1.1.1.1`},
966 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, fe80::382b:141b:fa4a:2a16%28`, `7.7.7.7.7, 8.8.8.8, 9.9.9.9, 10.10.10.10,11.11.11.11, 12.12.12.12`},
967 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
968 },
969 },
970 want: "8.8.8.8",
971 },
972 {
973 name: "Fail: header too short/count too large",
974 args: args{
975 headerName: "X-Forwarded-For",
976 trustedCount: 50,
977 headers: http.Header{
978 "X-Real-Ip": []string{`1.1.1.1`},
979 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, fe80::382b:141b:fa4a:2a16%28`, `7.7.7.7.7, 8.8.8.8, 9.9.9.9, 10.10.10.10,11.11.11.11, 12.12.12.12`},
980 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
981 },
982 },
983 want: "",
984 },
985 {
986 name: "Fail: bad value at count index",
987 args: args{
988 headerName: "Forwarded",
989 trustedCount: 2,
990 headers: http.Header{
991 "X-Real-Ip": []string{`1.1.1.1`},
992 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, fe80::382b:141b:fa4a:2a16%28`, `7.7.7.7.7, 8.8.8.8, 9.9.9.9, 10.10.10.10,11.11.11.11, 12.12.12.12`},
993 "Forwarded": []string{`For="::ffff:bc15:0006"`, `For=nope`, `host=what;for=6.6.6.6;proto=https`},
994 },
995 },
996 want: "",
997 },
998 {
999 name: "Fail: zero value at count index",
1000 args: args{
1001 headerName: "Forwarded",
1002 trustedCount: 2,
1003 headers: http.Header{
1004 "X-Real-Ip": []string{`1.1.1.1`},
1005 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, fe80::382b:141b:fa4a:2a16%28`, `7.7.7.7.7, 8.8.8.8, 9.9.9.9, 10.10.10.10,11.11.11.11, 12.12.12.12`},
1006 "Forwarded": []string{`For="::ffff:bc15:0006"`, `For=0.0.0.0`, `host=what;for=6.6.6.6;proto=https`},
1007 },
1008 },
1009 want: "",
1010 },
1011 {
1012 name: "Fail: header missing",
1013 args: args{
1014 headerName: "Forwarded",
1015 trustedCount: 1,
1016 headers: http.Header{
1017 "X-Real-Ip": []string{`1.1.1.1`},
1018 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, fe80::382b:141b:fa4a:2a16%28`, `7.7.7.7.7, 8.8.8.8, 9.9.9.9, 10.10.10.10,11.11.11.11, 12.12.12.12`},
1019 },
1020 },
1021 want: "",
1022 },
1023 {
1024 name: "Error: empty header name",
1025 args: args{
1026 headerName: "",
1027 trustedCount: 1,
1028 headers: http.Header{
1029 "X-Real-Ip": []string{"::1"},
1030 "True-Client-Ip": []string{"2.2.2.2"},
1031 "X-Forwarded-For": []string{"3.3.3.3"}},
1032 },
1033 wantErr: true,
1034 },
1035 {
1036 name: "Error: invalid header",
1037 args: args{
1038 headerName: "X-Real-IP",
1039 trustedCount: 1,
1040 headers: http.Header{
1041 "X-Real-Ip": []string{"::1"},
1042 "True-Client-Ip": []string{"2.2.2.2"},
1043 "X-Forwarded-For": []string{"3.3.3.3"}},
1044 },
1045 wantErr: true,
1046 },
1047 {
1048 name: "Error: zero trustedCount",
1049 args: args{
1050 headerName: "x-forwarded-for",
1051 trustedCount: 0,
1052 headers: http.Header{
1053 "X-Real-Ip": []string{`1.1.1.1`},
1054 "X-Forwarded-For": []string{`4.4.4.4, 5.5.5.5`, `::1, nope`, `fe80::382b:141b:fa4a:2a16%28`},
1055 "Forwarded": []string{`For="::ffff:bc15:0006"`, `host=what;for=6.6.6.6;proto=https`},
1056 },
1057 },
1058 wantErr: true,
1059 },
1060 {
1061 name: "Error: negative trustedCount",
1062 args: args{
1063 headerName: "X-Forwarded-For",
1064 trustedCount: -999,
1065 headers: http.Header{
1066 "X-Real-Ip": []string{`1.1.1.1`},
1067 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4:39333`},
1068 },
1069 },
1070 wantErr: true,
1071 },
1072 }
1073 for _, tt := range tests {
1074 t.Run(tt.name, func(t *testing.T) {
1075 strat, err := NewRightmostTrustedCountStrategy(tt.args.headerName, tt.args.trustedCount)
1076 if (err != nil) != tt.wantErr {
1077 t.Fatalf("NewRightmostTrustedCountStrategy error = %v, wantErr %v", err, tt.wantErr)
1078 return
1079 }
1080
1081 if err != nil {
1082 // We can't continue
1083 return
1084 }
1085
1086 got := strat.ClientIP(tt.args.headers, tt.args.remoteAddr)
1087 if !reflect.DeepEqual(got, tt.want) {
1088 t.Fatalf("ClientIP = %q, want %q", got, tt.want)
1089 }
1090 })
1091 }
1092 }
1093
1094 func TestAddressesAndRangesToIPNets(t *testing.T) {
1095 tests := []struct {
1096 name string
1097 ranges []string
1098 want []string
1099 wantErr bool
1100 }{
1101 {
1102 name: "Empty input",
1103 ranges: []string{},
1104 want: nil,
1105 },
1106 {
1107 name: "Single IPv4 address",
1108 ranges: []string{"1.1.1.1"},
1109 want: []string{"1.1.1.1/32"},
1110 },
1111 {
1112 name: "Single IPv6 address",
1113 ranges: []string{"2607:f8b0:4004:83f::200e"},
1114 want: []string{"2607:f8b0:4004:83f::200e/128"},
1115 },
1116 {
1117 name: "Single IPv4 range",
1118 ranges: []string{"1.1.1.1/16"},
1119 want: []string{"1.1.0.0/16"},
1120 },
1121 {
1122 name: "Single IPv6 range",
1123 ranges: []string{"2607:f8b0:4004:83f::200e/48"},
1124 want: []string{"2607:f8b0:4004::/48"},
1125 },
1126 {
1127 name: "Mixed input",
1128 ranges: []string{
1129 "1.1.1.1", "2607:f8b0:4004:83f::200e",
1130 "1.1.1.1/32", "2607:f8b0:4004:83f::200e/128",
1131 "1.1.1.1/16", "2607:f8b0:4004:83f::200e/56",
1132 "::ffff:188.0.2.128/112", "::ffff:bc15:0006/104",
1133 "64:ff9b::188.0.2.128/112",
1134 },
1135 want: []string{
1136 "1.1.1.1/32", "2607:f8b0:4004:83f::200e/128",
1137 "1.1.1.1/32", "2607:f8b0:4004:83f::200e/128",
1138 "1.1.0.0/16", "2607:f8b0:4004:800::/56",
1139 "188.0.0.0/16", "188.0.0.0/8",
1140 "64:ff9b::bc00:0/112",
1141 },
1142 },
1143 {
1144 name: "No input",
1145 ranges: nil,
1146 want: nil,
1147 },
1148 {
1149 name: "Error: garbage CIDR",
1150 ranges: []string{"2607:f8b0:4004:83f::200e/nope"},
1151 wantErr: true,
1152 },
1153 {
1154 name: "Error: CIDR with zone",
1155 ranges: []string{"fe80::abcd%nope/64"},
1156 wantErr: true,
1157 },
1158 {
1159 name: "Error: garbage IP",
1160 ranges: []string{"1.1.1.nope"},
1161 wantErr: true,
1162 },
1163 {
1164 name: "Error: empty value",
1165 ranges: []string{""},
1166 wantErr: true,
1167 },
1168 }
1169 for _, tt := range tests {
1170 t.Run(tt.name, func(t *testing.T) {
1171 got, err := AddressesAndRangesToIPNets(tt.ranges...)
1172 if (err != nil) != tt.wantErr {
1173 t.Fatalf("AddressesAndRangesToIPNets() error = %v, wantErr %v", err, tt.wantErr)
1174 return
1175 }
1176
1177 if err != nil {
1178 // We can't continue
1179 return
1180 }
1181
1182 if len(got) != len(tt.want) {
1183 t.Fatalf("len mismatch: %d != %d", len(got), len(tt.want))
1184 }
1185
1186 for i := 0; i < len(got); i++ {
1187 if got[i].String() != tt.want[i] {
1188 t.Fatalf("got does not equal want; %d: %q != %q", i, got[i].String(), tt.want[i])
1189 }
1190 }
1191 })
1192 }
1193 }
1194
1195 func TestRightmostTrustedRangeStrategy(t *testing.T) {
1196 // Ensure the strategy interface is implemented
1197 var _ Strategy = RightmostTrustedRangeStrategy{}
1198
1199 type args struct {
1200 headerName string
1201 headers http.Header
1202 remoteAddr string
1203 trustedRanges []string
1204 }
1205 tests := []struct {
1206 name string
1207 args args
1208 want string
1209 wantErr bool
1210 }{
1211 {
1212 name: "No ranges",
1213 args: args{
1214 headerName: "X-Forwarded-For",
1215 headers: http.Header{
1216 "X-Real-Ip": []string{`1.1.1.1`},
1217 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1218 },
1219 trustedRanges: nil,
1220 },
1221 want: "4.4.4.4",
1222 },
1223 {
1224 name: "One range",
1225 args: args{
1226 headerName: "X-Forwarded-For",
1227 headers: http.Header{
1228 "X-Real-Ip": []string{`1.1.1.1`},
1229 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1230 },
1231 trustedRanges: []string{`4.4.4.0/24`},
1232 },
1233 want: "3.3.3.3",
1234 },
1235 {
1236 name: "One IP",
1237 args: args{
1238 headerName: "X-Forwarded-For",
1239 headers: http.Header{
1240 "X-Real-Ip": []string{`1.1.1.1`},
1241 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1242 },
1243 trustedRanges: []string{`4.4.4.4`},
1244 },
1245 want: "3.3.3.3",
1246 },
1247 {
1248 name: "Many kinds of ranges",
1249 args: args{
1250 headerName: "Forwarded",
1251 headers: http.Header{
1252 "X-Real-Ip": []string{`1.1.1.1`},
1253 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1254 "Forwarded": []string{
1255 `For=99.99.99.99, For=4.4.4.8, For="[2607:f8b0:4004:83f::200e]:4747"`,
1256 `For=2.2.2.2:8883, For=64:ff9b::188.0.2.200, For=3.3.5.5, For=2001:db7::abcd`,
1257 },
1258 },
1259 trustedRanges: []string{
1260 `2.2.2.2/32`, `2607:f8b0:4004:83f::200e/128`,
1261 `3.3.0.0/16`, `2001:db7::/64`,
1262 `::ffff:4.4.4.4/124`, `64:ff9b::188.0.2.128/112`,
1263 },
1264 },
1265 want: "99.99.99.99",
1266 },
1267 {
1268 name: "Cloudflare ranges",
1269 args: args{
1270 headerName: "X-Forwarded-For",
1271 headers: http.Header{
1272 "X-Real-Ip": []string{`1.1.1.1`},
1273 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`, `2400:cb00::1`},
1274 },
1275 trustedRanges: ranges.Cloudflare,
1276 },
1277 want: "4.4.4.4",
1278 },
1279 {
1280 name: "Fail: no non-trusted IP",
1281 args: args{
1282 headerName: "X-Forwarded-For",
1283 headers: http.Header{
1284 "X-Real-Ip": []string{`1.1.1.1`},
1285 "X-Forwarded-For": []string{`2.2.2.2:3384, 2.2.2.3`, `2.2.2.4`},
1286 },
1287 trustedRanges: []string{`2.2.2.0/24`},
1288 },
1289 want: "",
1290 },
1291 {
1292 name: "Fail: rightmost non-trusted IP invalid",
1293 args: args{
1294 headerName: "X-Forwarded-For",
1295 headers: http.Header{
1296 "X-Real-Ip": []string{`1.1.1.1`},
1297 "X-Forwarded-For": []string{`nope, 2.2.2.2:3384, 2.2.2.3`, `2.2.2.4`},
1298 },
1299 trustedRanges: []string{`2.2.2.0/24`},
1300 },
1301 want: "",
1302 },
1303 {
1304 name: "Fail: rightmost non-trusted IP unspecified",
1305 args: args{
1306 headerName: "X-Forwarded-For",
1307 headers: http.Header{
1308 "X-Real-Ip": []string{`1.1.1.1`},
1309 "X-Forwarded-For": []string{`::, 2.2.2.2:3384, 2.2.2.3`, `2.2.2.4`},
1310 },
1311 trustedRanges: []string{`2.2.2.0/24`},
1312 },
1313 want: "",
1314 },
1315 {
1316 name: "Fail: no values in header",
1317 args: args{
1318 headerName: "X-Forwarded-For",
1319 headers: http.Header{
1320 "X-Real-Ip": []string{`1.1.1.1`},
1321 "X-Forwarded-For": []string{}},
1322 trustedRanges: []string{`2.2.2.0/24`},
1323 },
1324 want: "",
1325 },
1326 {
1327 name: "Error: empty header nanme",
1328 args: args{
1329 headerName: "",
1330 headers: http.Header{
1331 "X-Real-Ip": []string{`1.1.1.1`},
1332 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1333 },
1334 trustedRanges: nil,
1335 },
1336 wantErr: true,
1337 },
1338 {
1339 name: "Error: bad header nanme",
1340 args: args{
1341 headerName: "Not-XFF-Or-Forwarded",
1342 headers: http.Header{
1343 "X-Real-Ip": []string{`1.1.1.1`},
1344 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1345 },
1346 trustedRanges: nil,
1347 },
1348 wantErr: true,
1349 },
1350 }
1351 for _, tt := range tests {
1352 t.Run(tt.name, func(t *testing.T) {
1353 ranges, err := AddressesAndRangesToIPNets(tt.args.trustedRanges...)
1354 if err != nil {
1355 // We're not testing AddressesAndRangesToIPNets here
1356 t.Fatalf("AddressesAndRangesToIPNets failed")
1357 }
1358
1359 strat, err := NewRightmostTrustedRangeStrategy(tt.args.headerName, ranges)
1360 if (err != nil) != tt.wantErr {
1361 t.Fatalf("NewRightmostTrustedRangeStrategy error = %v, wantErr %v", err, tt.wantErr)
1362 return
1363 }
1364
1365 if err != nil {
1366 // We can't continue
1367 return
1368 }
1369
1370 got := strat.ClientIP(tt.args.headers, tt.args.remoteAddr)
1371 if !reflect.DeepEqual(got, tt.want) {
1372 t.Fatalf("ClientIP = %q, want %q", got, tt.want)
1373 }
1374 })
1375 }
1376 }
1377
1378 func TestChainStrategy(t *testing.T) {
1379 type args struct {
1380 strategies []Strategy
1381 headers http.Header
1382 remoteAddr string
1383 }
1384 tests := []struct {
1385 name string
1386 args args
1387 want string
1388 }{
1389 {
1390 name: "Single strategy",
1391 args: args{
1392 strategies: []Strategy{RemoteAddrStrategy{}},
1393 headers: http.Header{
1394 "X-Real-Ip": []string{`1.1.1.1`},
1395 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1396 },
1397 remoteAddr: `5.5.5.5`,
1398 },
1399 want: "5.5.5.5",
1400 },
1401 {
1402 name: "Multiple strategies",
1403 args: args{
1404 strategies: []Strategy{
1405 Must(NewRightmostNonPrivateStrategy("Forwarded")),
1406 Must(NewSingleIPHeaderStrategy("true-client-ip")),
1407 Must(NewSingleIPHeaderStrategy("x-real-ip")),
1408 RemoteAddrStrategy{},
1409 },
1410 headers: http.Header{
1411 "X-Real-Ip": []string{`1.1.1.1`},
1412 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1413 },
1414 remoteAddr: `5.5.5.5`,
1415 },
1416 want: "1.1.1.1",
1417 },
1418 {
1419 name: "Fail: No strategies",
1420 args: args{
1421 strategies: nil,
1422 headers: http.Header{
1423 "X-Real-Ip": []string{`1.1.1.1`},
1424 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1425 },
1426 remoteAddr: `5.5.5.5`,
1427 },
1428 want: "",
1429 },
1430 {
1431 name: "Fail: Multiple strategies, all fail",
1432 args: args{
1433 strategies: []Strategy{
1434 Must(NewRightmostNonPrivateStrategy("Forwarded")),
1435 Must(NewSingleIPHeaderStrategy("true-client-ip")),
1436 Must(NewSingleIPHeaderStrategy("x-real-ip")),
1437 RemoteAddrStrategy{},
1438 },
1439 headers: http.Header{
1440 "X-Forwarded-For": []string{`2.2.2.2:3384, 3.3.3.3`, `4.4.4.4`},
1441 },
1442 remoteAddr: "",
1443 },
1444 want: "",
1445 },
1446 }
1447 for _, tt := range tests {
1448 t.Run(tt.name, func(t *testing.T) {
1449 strat := NewChainStrategy(tt.args.strategies...)
1450
1451 got := strat.ClientIP(tt.args.headers, tt.args.remoteAddr)
1452 if !reflect.DeepEqual(got, tt.want) {
1453 t.Fatalf("ClientIP = %q, want %q", got, tt.want)
1454 }
1455 })
1456 }
1457 }
1458
1459 func TestMust(t *testing.T) {
1460 // We test the non-panic path elsewhere, but we need to specifically check the panic case
1461 defer func() {
1462 if r := recover(); r == nil {
1463 t.Fatalf("Must() did not panic")
1464 }
1465 }()
1466
1467 Must(RemoteAddrStrategy{}, fmt.Errorf("oh no"))
1468 }
1469
1470 func TestMustParseIPAddr(t *testing.T) {
1471 // We test the non-panic path elsewhere, but we need to specifically check the panic case
1472 defer func() {
1473 if r := recover(); r == nil {
1474 t.Fatalf("MustParseIPAddr() did not panic")
1475 }
1476 }()
1477
1478 MustParseIPAddr("nope")
1479 }
1480
1481 func TestParseIPAddr(t *testing.T) {
1482 tests := []struct {
1483 name string
1484 ipStr string
1485 want net.IPAddr
1486 wantErr bool
1487 }{
1488 {
1489 name: "Empty zone",
1490 ipStr: "1.1.1.1%",
1491 want: net.IPAddr{IP: net.ParseIP("1.1.1.1"), Zone: ""},
1492 },
1493 {
1494 name: "No zone",
1495 ipStr: "1.1.1.1",
1496 want: net.IPAddr{IP: net.ParseIP("1.1.1.1"), Zone: ""},
1497 },
1498 {
1499 name: "With zone",
1500 ipStr: "fe80::abcd%zone",
1501 want: net.IPAddr{IP: net.ParseIP("fe80::abcd"), Zone: "zone"},
1502 },
1503 {
1504 name: "With zone and port",
1505 ipStr: "[2607:f8b0:4004:83f::200e%zone]:4484",
1506 want: net.IPAddr{IP: net.ParseIP("2607:f8b0:4004:83f::200e"), Zone: "zone"},
1507 },
1508 {
1509 name: "With port",
1510 ipStr: "1.1.1.1:48944",
1511 want: net.IPAddr{IP: net.ParseIP("1.1.1.1"), Zone: ""},
1512 },
1513 {
1514 name: "Bad port (is discarded)",
1515 ipStr: "[fe80::abcd%eth0]:xyz",
1516 want: net.IPAddr{IP: net.ParseIP("fe80::abcd"), Zone: "eth0"},
1517 },
1518 {
1519 name: "Zero address",
1520 ipStr: "0.0.0.0",
1521 want: net.IPAddr{IP: net.ParseIP("0.0.0.0"), Zone: ""},
1522 },
1523 {
1524 name: "Unspecified address",
1525 ipStr: "::",
1526 want: net.IPAddr{IP: net.ParseIP("::"), Zone: ""},
1527 },
1528 {
1529 name: "Error: bad IP with zone",
1530 ipStr: "nope%zone",
1531 wantErr: true,
1532 },
1533 {
1534 name: "Error: bad IP",
1535 ipStr: "nope!!",
1536 wantErr: true,
1537 },
1538 }
1539 for _, tt := range tests {
1540 t.Run(tt.name, func(t *testing.T) {
1541 got, err := ParseIPAddr(tt.ipStr)
1542 if (err != nil) != tt.wantErr {
1543 t.Fatalf("ParseIPAddr() error = %v, wantErr %v, got = %v", err, tt.wantErr, got)
1544 return
1545 }
1546
1547 if !ipAddrsEqual(got, tt.want) {
1548 t.Fatalf("ParseIPAddr() = %v, want %v", got, tt.want)
1549 }
1550 })
1551 }
1552 }
1553
1554 func Test_goodIPAddr(t *testing.T) {
1555 // This is mostly a copy of TestParseIPAddr, except that zero and unspecified addresses are disallowed
1556 tests := []struct {
1557 name string
1558 ipStr string
1559 want *net.IPAddr
1560 }{
1561 {
1562 name: "Empty zone",
1563 ipStr: "1.1.1.1%",
1564 want: &net.IPAddr{IP: net.ParseIP("1.1.1.1"), Zone: ""},
1565 },
1566 {
1567 name: "No zone",
1568 ipStr: "1.1.1.1",
1569 want: &net.IPAddr{IP: net.ParseIP("1.1.1.1"), Zone: ""},
1570 },
1571 {
1572 name: "With zone",
1573 ipStr: "fe80::abcd%zone",
1574 want: &net.IPAddr{IP: net.ParseIP("fe80::abcd"), Zone: "zone"},
1575 },
1576 {
1577 name: "With zone and port",
1578 ipStr: "[2607:f8b0:4004:83f::200e%zone]:4484",
1579 want: &net.IPAddr{IP: net.ParseIP("2607:f8b0:4004:83f::200e"), Zone: "zone"},
1580 },
1581 {
1582 name: "With port",
1583 ipStr: "1.1.1.1:48944",
1584 want: &net.IPAddr{IP: net.ParseIP("1.1.1.1"), Zone: ""},
1585 },
1586 {
1587 name: "Bad port (is discarded)",
1588 ipStr: "[fe80::abcd%eth0]:xyz",
1589 want: &net.IPAddr{IP: net.ParseIP("fe80::abcd"), Zone: "eth0"},
1590 },
1591 {
1592 name: "Error: Zero address",
1593 ipStr: "0.0.0.0",
1594 want: nil,
1595 },
1596 {
1597 name: "Error: Unspecified address",
1598 ipStr: "::",
1599 want: nil,
1600 },
1601 {
1602 name: "Error: bad IP with zone",
1603 ipStr: "nope%zone",
1604 want: nil,
1605 },
1606 {
1607 name: "Error: bad IP",
1608 ipStr: "nope!!",
1609 want: nil,
1610 },
1611 }
1612 for _, tt := range tests {
1613 t.Run(tt.name, func(t *testing.T) {
1614 got := goodIPAddr(tt.ipStr)
1615
1616 if got == nil || tt.want == nil {
1617 if got != tt.want {
1618 t.Fatalf("ParseIPAddr() = %v, want %v", got, tt.want)
1619 }
1620 return
1621 }
1622
1623 if !ipAddrsEqual(*got, *tt.want) {
1624 t.Fatalf("ParseIPAddr() = %v, want %v", *got, *tt.want)
1625 }
1626 })
1627 }
1628 }
1629
1630 func Test_isPrivateOrLocal(t *testing.T) {
1631 tests := []struct {
1632 name string
1633 ip string
1634 want bool
1635 }{
1636 {
1637 name: "IPv4 loopback",
1638 ip: `127.0.0.2`,
1639 want: true,
1640 },
1641 {
1642 name: "IPv6 loopback",
1643 ip: `::1`,
1644 want: true,
1645 },
1646 {
1647 name: "IPv4 10.*",
1648 ip: `10.0.0.1`,
1649 want: true,
1650 },
1651 {
1652 name: "IPv4 192.168.*",
1653 ip: `192.168.1.1`,
1654 want: true,
1655 },
1656 {
1657 name: "IPv6 unique local address",
1658 ip: `fd12:3456:789a:1::1`,
1659 want: true,
1660 },
1661 {
1662 name: "IPv4 link-local",
1663 ip: `169.254.1.1`,
1664 want: true,
1665 },
1666 {
1667 name: "IPv6 link-local",
1668 ip: `fe80::abcd`,
1669 want: true,
1670 },
1671 {
1672 name: "Non-local IPv4",
1673 ip: `1.1.1.1`,
1674 want: false,
1675 },
1676 {
1677 name: "Non-local IPv4-mapped IPv6",
1678 ip: `::ffff:188.0.2.128`,
1679 want: false,
1680 },
1681 }
1682 for _, tt := range tests {
1683 t.Run(tt.name, func(t *testing.T) {
1684 ip := net.ParseIP(tt.ip)
1685 if ip == nil {
1686 t.Fatalf("net.ParseIP failed; bad test input")
1687 }
1688 if got := isPrivateOrLocal(ip); got != tt.want {
1689 t.Fatalf("isPrivateOrLocal() = %v, want %v", got, tt.want)
1690 }
1691 })
1692 }
1693 }
1694
1695 func Test_mustParseCIDR(t *testing.T) {
1696 // We test the non-panic path elsewhere, but we need to specifically check the panic case
1697 defer func() {
1698 if r := recover(); r == nil {
1699 t.Fatalf("mustParseCIDR() did not panic")
1700 }
1701 }()
1702
1703 mustParseCIDR("nope")
1704 }
1705
1706 func Test_trimMatchedEnds(t *testing.T) {
1707 // We test the non-panic paths elsewhere, but we need to specifically check the panic case
1708 defer func() {
1709 if r := recover(); r == nil {
1710 t.Fatalf("trimMatchedEnds() did not panic")
1711 }
1712 }()
1713
1714 trimMatchedEnds("nope", "abcd")
1715 }
1716
1717 func Test_parseForwardedListItem(t *testing.T) {
1718 mustParseIPAddrPtr := func(ipStr string) *net.IPAddr {
1719 res := MustParseIPAddr(ipStr)
1720 return &res
1721 }
1722
1723 tests := []struct {
1724 name string
1725 fwd string
1726 want *net.IPAddr
1727 }{
1728 {
1729 // This is the correct form for IPv6 wit port
1730 name: "IPv6 with port and quotes",
1731 fwd: `For="[2607:f8b0:4004:83f::200e]:4711"`,
1732 want: mustParseIPAddrPtr("2607:f8b0:4004:83f::200e"),
1733 },
1734 {
1735 // This is the correct form for IP with no port
1736 name: "IPv6 with quotes, brackets and no port",
1737 fwd: `fOR="[2607:f8b0:4004:83f::200e]"`,
1738 want: mustParseIPAddrPtr("2607:f8b0:4004:83f::200e"),
1739 },
1740 {
1741 // RFC deviation: missing brackets
1742 name: "IPv6 with quotes, no brackets, and no port",
1743 fwd: `for="2607:f8b0:4004:83f::200e"`,
1744 want: mustParseIPAddrPtr("2607:f8b0:4004:83f::200e"),
1745 },
1746 {
1747 // RFC deviation: missing quotes
1748 name: "IPv6 with brackets, no quotes, and no port",
1749 fwd: `FOR=[2607:f8b0:4004:83f::200e]`,
1750 want: mustParseIPAddrPtr("2607:f8b0:4004:83f::200e"),
1751 },
1752 {
1753 // RFC deviation: missing quotes
1754 name: "IPv6 with port and no quotes",
1755 fwd: `For=[2607:f8b0:4004:83f::200e]:4711`,
1756 want: mustParseIPAddrPtr("2607:f8b0:4004:83f::200e"),
1757 },
1758 {
1759 name: "IPv6 with port, quotes, and zone",
1760 fwd: `For="[fe80::abcd%zone]:4711"`,
1761 want: mustParseIPAddrPtr("fe80::abcd%zone"),
1762 },
1763 {
1764 // RFC deviation: missing brackets
1765 name: "IPv6 with zone, no quotes, no port",
1766 fwd: `For="fe80::abcd%zone"`,
1767 want: mustParseIPAddrPtr("fe80::abcd%zone"),
1768 },
1769 {
1770 // RFC deviation: missing quotes
1771 name: "IPv4 with port",
1772 fwd: `FoR=192.0.2.60:4711`,
1773 want: mustParseIPAddrPtr("192.0.2.60"),
1774 },
1775 {
1776 name: "IPv4 with no port",
1777 fwd: `for=192.0.2.60`,
1778 want: mustParseIPAddrPtr("192.0.2.60"),
1779 },
1780 {
1781 name: "IPv4 with quotes",
1782 fwd: `for="192.0.2.60"`,
1783 want: mustParseIPAddrPtr("192.0.2.60"),
1784 },
1785 {
1786 name: "IPv4 with port and quotes",
1787 fwd: `for="192.0.2.60:4823"`,
1788 want: mustParseIPAddrPtr("192.0.2.60"),
1789 },
1790 {
1791 name: "Error: invalid IPv4",
1792 fwd: `for=192.0.2.999`,
1793 want: nil,
1794 },
1795 {
1796 name: "Error: invalid IPv6",
1797 fwd: `for="2607:f8b0:4004:83f::999999"`,
1798 want: nil,
1799 },
1800 {
1801 name: "Error: non-IP identifier",
1802 fwd: `for="_test"`,
1803 want: nil,
1804 },
1805 {
1806 name: "Error: empty IP value",
1807 fwd: `for=`,
1808 want: nil,
1809 },
1810 {
1811 name: "Multiple IPv4 directives",
1812 fwd: `by=1.1.1.1; for=2.2.2.2;host=myhost; proto=https`,
1813 want: mustParseIPAddrPtr("2.2.2.2"),
1814 },
1815 {
1816 // RFC deviation: missing quotes around IPv6
1817 name: "Multiple IPv6 directives",
1818 fwd: `by=1::1;host=myhost;for=2::2;proto=https`,
1819 want: mustParseIPAddrPtr("2::2"),
1820 },
1821 {
1822 // RFC deviation: missing quotes around IPv6
1823 name: "Multiple mixed directives",
1824 fwd: `by=1::1;host=myhost;proto=https;for=2.2.2.2`,
1825 want: mustParseIPAddrPtr("2.2.2.2"),
1826 },
1827 {
1828 name: "IPv4-mapped IPv6",
1829 fwd: `for="[::ffff:188.0.2.128]"`,
1830 want: mustParseIPAddrPtr("188.0.2.128"),
1831 },
1832 {
1833 name: "IPv4-mapped IPv6 with port and quotes",
1834 fwd: `for="[::ffff:188.0.2.128]:49428"`,
1835 want: mustParseIPAddrPtr("188.0.2.128"),
1836 },
1837 {
1838 name: "IPv4-mapped IPv6 in IPv6 form",
1839 fwd: `for="[0:0:0:0:0:ffff:bc15:0006]"`,
1840 want: mustParseIPAddrPtr("188.21.0.6"),
1841 },
1842 {
1843 name: "NAT64 IPv4-mapped IPv6",
1844 fwd: `for="[64:ff9b::188.0.2.128]"`,
1845 want: mustParseIPAddrPtr("64:ff9b::188.0.2.128"),
1846 },
1847 {
1848 name: "IPv4 loopback",
1849 fwd: `for=127.0.0.1`,
1850 want: mustParseIPAddrPtr("127.0.0.1"),
1851 },
1852 {
1853 name: "IPv6 loopback",
1854 fwd: `for="[::1]"`,
1855 want: mustParseIPAddrPtr("::1"),
1856 },
1857 {
1858 // RFC deviation: quotes must be matched
1859 name: "Error: Unmatched quote",
1860 fwd: `for="1.1.1.1`,
1861 want: nil,
1862 },
1863 {
1864 // RFC deviation: brackets must be matched
1865 name: "Error: IPv6 loopback",
1866 fwd: `for="::1]"`,
1867 want: nil,
1868 },
1869 {
1870 name: "Error: misplaced quote",
1871 fwd: `for="[0:0:0:0:0:ffff:bc15:0006"]`,
1872 want: nil,
1873 },
1874 {
1875 name: "Error: garbage",
1876 fwd: "ads\x00jkl&#*(383fdljk",
1877 want: nil,
1878 },
1879 {
1880 // Per RFC 7230 section 3.2.6, this should not be an error, but we don't have
1881 // full syntax support yet.
1882 name: "RFC deviation: quoted pair",
1883 fwd: `for=1.1.1.\1`,
1884 want: nil,
1885 },
1886 {
1887 // Per RFC 7239, this extraneous whitespace should be an error, but we don't
1888 // have full syntax support yet.
1889 name: "RFC deviation: Incorrect whitespace",
1890 fwd: `for= 1.1.1.1`,
1891 want: mustParseIPAddrPtr("1.1.1.1"),
1892 },
1893 }
1894 for _, tt := range tests {
1895 t.Run(tt.name, func(t *testing.T) {
1896 got := parseForwardedListItem(tt.fwd)
1897
1898 if got == nil || tt.want == nil {
1899 if got != tt.want {
1900 t.Fatalf("parseForwardedListItem() = %v, want %v", got, tt.want)
1901 }
1902 return
1903 }
1904
1905 if !ipAddrsEqual(*got, *tt.want) {
1906 t.Fatalf("parseForwardedListItem() = %v, want %v", got, tt.want)
1907 }
1908 })
1909 }
1910 }
1911
1912 // Demonstrate parsing deviations from Forwarded header syntax RFCs, particularly
1913 // RFC 7239 (Forwarded header) and RFC 7230 (HTTP/1.1 syntax) section 3.2.6.
1914 func Test_forwardedHeaderRFCDeviations(t *testing.T) {
1915 mustParseIPAddrPtr := func(s string) *net.IPAddr {
1916 res := MustParseIPAddr(s)
1917 return &res
1918 }
1919
1920 type args struct {
1921 headers http.Header
1922 headerName string
1923 }
1924 tests := []struct {
1925 name string
1926 args args
1927 want []*net.IPAddr
1928 }{
1929 {
1930 // The value in quotes should be a single value but we split by comma, so it's not.
1931 // The first and third "For=" bits have one double-quote in them, so they are
1932 // considered invalid by our parser. The second is still in the quoted-string,
1933 // but doesn't have any quotes in it, so it parses okay.
1934 name: "Comma in quotes",
1935 args: args{
1936 headers: http.Header{"Forwarded": []string{`For="1.1.1.1, For=2.2.2.2, For=3.3.3.3", For="4.4.4.4"`}},
1937 headerName: "Forwarded",
1938 },
1939 // There are really only two values, so we actually want: {nil, "4.4.4.4"}
1940 want: []*net.IPAddr{nil, mustParseIPAddrPtr("2.2.2.2"), nil, mustParseIPAddrPtr("4.4.4.4")},
1941 },
1942 {
1943 // Per 7239, the opening unmatched quote makes the whole rest of the header invalid.
1944 // But that would mean that an attacker can invalidate the whole header with a
1945 // quote character early on, even the trusted IPs added by our reverse proxies.
1946 // Our actual behaviour is probably the best approach.
1947 name: "Unmatched quote",
1948 args: args{
1949 headers: http.Header{"Forwarded": []string{`For="1.1.1.1, For=2.2.2.2`}},
1950 headerName: "Forwarded",
1951 },
1952 // There are really only two values, so the RFC would require: {nil} (or empty slice?)
1953 want: []*net.IPAddr{nil, mustParseIPAddrPtr("2.2.2.2")},
1954 },
1955 {
1956 // The invalid non-For parameter should invalidate the whole item, but we're
1957 // not checking anything but the "For=" part.
1958 name: "Invalid characters",
1959 args: args{
1960 headers: http.Header{"Forwarded": []string{`For=1.1.1.1;@!=😀, For=2.2.2.2`}},
1961 headerName: "Forwarded",
1962 },
1963 // Only the last value is valid, so it should be: {nil, "2.2.2.2"}
1964 want: []*net.IPAddr{mustParseIPAddrPtr("1.1.1.1"), mustParseIPAddrPtr("2.2.2.2")},
1965 },
1966 {
1967 // The duplicate "For=" parameter should invalidate the whole item but we don't check for it
1968 name: "Duplicate token",
1969 args: args{
1970 headers: http.Header{"Forwarded": []string{`For=1.1.1.1;For=2.2.2.2, For=3.3.3.3`}},
1971 headerName: "Forwarded",
1972 },
1973 // Only the last value is valid, so it should be: {nil, "3.3.3.3"}
1974 want: []*net.IPAddr{mustParseIPAddrPtr("1.1.1.1"), mustParseIPAddrPtr("3.3.3.3")},
1975 },
1976 {
1977 // An escaped character in quotes should be unescaped, but we're not doing it.
1978 // (And if we do end up doing it, make sure that `\\` becomes `\` after escaping.
1979 // And escaping is only allowed in quoted strings.)
1980 // There is no good reason for any part of an IP address to be escaped anyway.
1981 name: "Escaped character",
1982 args: args{
1983 headers: http.Header{"Forwarded": []string{`For="3.3.3.\3"`}},
1984 headerName: "Forwarded",
1985 },
1986 // The value is valid, so it should be: {nil, "3.3.3.3"}
1987 want: []*net.IPAddr{nil},
1988 },
1989 {
1990 // Spaces are not allowed around the equal signs, but due to the way we parse
1991 // a space after the equal will pass but one before won't.
1992 name: "Equal sign spaces",
1993 args: args{
1994 headers: http.Header{"Forwarded": []string{`For =1.1.1.1, For= 3.3.3.3`}},
1995 headerName: "Forwarded",
1996 },
1997 // Neither value is valid, so it should be: {nil, nil}
1998 want: []*net.IPAddr{nil, mustParseIPAddrPtr("3.3.3.3")},
1999 },
2000 {
2001 // Disallowed characters are only allowed in quoted strings. This means
2002 // that IPv6 addresses must be quoted.
2003 name: "Disallowed characters in unquoted value (like colons and square brackets",
2004 args: args{
2005 headers: http.Header{"Forwarded": []string{`For=[2607:f8b0:4004:83f::200e]`}},
2006 headerName: "Forwarded",
2007 },
2008 // Value is invalid without quotes, so should be {nil}
2009 want: []*net.IPAddr{mustParseIPAddrPtr("2607:f8b0:4004:83f::200e")},
2010 },
2011 {
2012 // IPv6 addresses are required to be contained in square brackets. We don't
2013 // require this simply to be more flexible in what is accepted.
2014 name: "IPv6 brackets",
2015 args: args{
2016 headers: http.Header{"Forwarded": []string{`For="2607:f8b0:4004:83f::200e"`}},
2017 headerName: "Forwarded",
2018 },
2019 // IPv6 is invalid without brackets, so should be {nil}
2020 want: []*net.IPAddr{mustParseIPAddrPtr("2607:f8b0:4004:83f::200e")},
2021 },
2022 {
2023 // IPv4 addresses are _not_ supposed to be in square brackets, but we trim
2024 // them unconditionally.
2025 name: "IPv4 brackets",
2026 args: args{
2027 headers: http.Header{"Forwarded": []string{`For="[1.1.1.1]"`}},
2028 headerName: "Forwarded",
2029 },
2030 // IPv4 is invalid with brackets, so should be {nil}
2031 want: []*net.IPAddr{mustParseIPAddrPtr("1.1.1.1")},
2032 },
2033 }
2034
2035 for _, tt := range tests {
2036 t.Run(tt.name, func(t *testing.T) {
2037 if got := getIPAddrList(tt.args.headers, tt.args.headerName); !reflect.DeepEqual(got, tt.want) {
2038 t.Errorf("getIPAddrList() = %v, want %v", got, tt.want)
2039 }
2040 })
2041 }
2042 }