This repository was archived by the owner on Apr 5, 2024. It is now read-only.
File tree 3 files changed +29
-13
lines changed
3 files changed +29
-13
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const Boom = require ( '@hapi/boom' ) ;
4
+ const Url = require ( 'url' ) ;
4
5
5
6
6
7
const internals = { } ;
@@ -17,12 +18,6 @@ exports.limits = {
17
18
} ;
18
19
19
20
20
- // Extract host and port from request
21
-
22
- // $1 $2
23
- internals . hostHeaderRegex = / ^ (?: (?: \r \n ) ? \s ) * ( (?: [ ^ : ] + ) | (?: \[ [ ^ \] ] + \] ) ) (?: : ( \d + ) ) ? (?: (?: \r \n ) ? \s ) * $ / ; // (IPv4, hostname)|(IPv6)
24
-
25
-
26
21
exports . parseHost = function ( req , hostHeaderName ) {
27
22
28
23
hostHeaderName = ( hostHeaderName ? hostHeaderName . toLowerCase ( ) : 'host' ) ;
@@ -35,14 +30,21 @@ exports.parseHost = function (req, hostHeaderName) {
35
30
return null ;
36
31
}
37
32
38
- const hostParts = hostHeader . match ( internals . hostHeaderRegex ) ;
39
- if ( ! hostParts ) {
33
+ if ( hostHeader . indexOf ( '/' ) !== - 1 ) {
34
+ return null ;
35
+ }
36
+
37
+ let uri ;
38
+ try {
39
+ uri = new Url . URL ( 'http://' + hostHeader ) ;
40
+ }
41
+ catch ( err ) {
40
42
return null ;
41
43
}
42
44
43
45
return {
44
- name : hostParts [ 1 ] ,
45
- port : ( hostParts [ 2 ] ? hostParts [ 2 ] : ( req . connection && req . connection . encrypted ? 443 : 80 ) )
46
+ name : uri . hostname ,
47
+ port : ( uri . port ? uri . port : ( req . connection && req . connection . encrypted ? 443 : 80 ) )
46
48
} ;
47
49
} ;
48
50
Original file line number Diff line number Diff line change @@ -433,6 +433,20 @@ describe('Server', () => {
433
433
await expect ( Hawk . server . authenticate ( req , credentialsFunc , { localtimeOffsetMsec : 1353788437000 - Hawk . utils . now ( ) } ) ) . to . reject ( 'Invalid Host header' ) ;
434
434
} ) ;
435
435
436
+ it ( 'errors on an bad host header (includes path and query)' , async ( ) => {
437
+
438
+ const req = {
439
+ method : 'GET' ,
440
+ url : '/resource/4?filter=a' ,
441
+ headers : {
442
+ host : 'example.com:8080/path?x=z' ,
443
+ authorization : 'Hawk'
444
+ }
445
+ } ;
446
+
447
+ await expect ( Hawk . server . authenticate ( req , credentialsFunc , { localtimeOffsetMsec : 1353788437000 - Hawk . utils . now ( ) } ) ) . to . reject ( 'Invalid Host header' ) ;
448
+ } ) ;
449
+
436
450
it ( 'errors on an bad host header (pad port)' , async ( ) => {
437
451
438
452
const req = {
Original file line number Diff line number Diff line change @@ -55,7 +55,7 @@ describe('Utils', () => {
55
55
method : 'POST' ,
56
56
url : '/resource/4?filter=a' ,
57
57
headers : {
58
- host : '[123:123:123]' ,
58
+ host : '[123:123:: 123]' ,
59
59
'content-type' : 'text/plain;x=y'
60
60
} ,
61
61
connection : {
@@ -72,7 +72,7 @@ describe('Utils', () => {
72
72
method : 'POST' ,
73
73
url : '/resource/4?filter=a' ,
74
74
headers : {
75
- host : '[123:123:123]:8000' ,
75
+ host : '[123:123:: 123]:8000' ,
76
76
'content-type' : 'text/plain;x=y'
77
77
} ,
78
78
connection : {
@@ -82,7 +82,7 @@ describe('Utils', () => {
82
82
83
83
const host = Hawk . utils . parseHost ( req , 'Host' ) ;
84
84
expect ( host . port ) . to . equal ( '8000' ) ;
85
- expect ( host . name ) . to . equal ( '[123:123:123]' ) ;
85
+ expect ( host . name ) . to . equal ( '[123:123:: 123]' ) ;
86
86
} ) ;
87
87
88
88
it ( 'errors on header too long' , ( ) => {
You can’t perform that action at this time.
0 commit comments