File tree 2 files changed +29
-2
lines changed
2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change 5
5
* Module dependencies.
6
6
*/
7
7
8
- const URL = require ( 'url' ) . URL ;
8
+ const url = require ( 'url' ) ;
9
9
const net = require ( 'net' ) ;
10
10
const contentType = require ( 'content-type' ) ;
11
11
const stringify = require ( 'url' ) . format ;
@@ -283,7 +283,7 @@ module.exports = {
283
283
const host = this . host ;
284
284
const originalUrl = this . originalUrl || '' ; // avoid undefined in template string
285
285
try {
286
- this . memoizedURL = new URL ( `${ protocol } ://${ host } ${ originalUrl } ` ) ;
286
+ this . memoizedURL = new url . URL ( `${ protocol } ://${ host } ${ originalUrl } ` ) ;
287
287
} catch ( err ) {
288
288
this . memoizedURL = Object . create ( null ) ;
289
289
}
@@ -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 url . 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