File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -433,6 +433,15 @@ module.exports = {
433
433
const val = this . get ( 'X-Forwarded-For' ) ;
434
434
return proxy && val
435
435
? val . split ( / \s * , \s * / )
436
+ . map ( host => {
437
+ let normalizedHost = host ;
438
+ if ( net . isIPv6 ( host ) ) {
439
+ normalizedHost = `[${ host } ]` ;
440
+ }
441
+
442
+ return parse ( `http://${ normalizedHost } ` ) . hostname ;
443
+ } )
444
+ . filter ( ip => ! ! ip )
436
445
: [ ] ;
437
446
} ,
438
447
Original file line number Diff line number Diff line change @@ -23,5 +23,23 @@ describe('req.ips', () => {
23
23
assert . deepEqual ( req . ips , [ '127.0.0.1' , '127.0.0.2' ] ) ;
24
24
} ) ;
25
25
} ) ;
26
+
27
+ describe ( 'and contains IPv4' , ( ) => {
28
+ it ( 'should not return port' , ( ) => {
29
+ const req = request ( ) ;
30
+ req . app . proxy = true ;
31
+ req . header [ 'x-forwarded-for' ] = '127.0.0.1:80,127.0.0.2' ;
32
+ assert . deepEqual ( req . ips , [ '127.0.0.1' , '127.0.0.2' ] ) ;
33
+ } ) ;
34
+ } ) ;
35
+
36
+ describe ( 'and contains IPv6' , ( ) => {
37
+ it ( 'should parse correctly' , ( ) => {
38
+ const req = request ( ) ;
39
+ req . app . proxy = true ;
40
+ req . header [ 'x-forwarded-for' ] = '::1' ;
41
+ assert . deepEqual ( req . ips , [ '::1' ] ) ;
42
+ } ) ;
43
+ } ) ;
26
44
} ) ;
27
45
} ) ;
You can’t perform that action at this time.
0 commit comments