@@ -77,13 +77,19 @@ describe('Validation Utils', () => {
7777 } ,
7878 } ) ;
7979
80- expect ( ( ) => validateRequest ( req , allowedHosts ) ) . not . toThrow ( ) ;
80+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . not . toThrow ( ) ;
81+ } ) ;
82+
83+ it ( 'should pass for valid request when disableHostCheck is true' , ( ) => {
84+ const req = new Request ( 'http://evil.com' ) ;
85+
86+ expect ( ( ) => validateRequest ( req , allowedHosts , true ) ) . not . toThrow ( ) ;
8187 } ) ;
8288
8389 it ( 'should throw if URL hostname is invalid' , ( ) => {
8490 const req = new Request ( 'http://evil.com' ) ;
8591
86- expect ( ( ) => validateRequest ( req , allowedHosts ) ) . toThrowError (
92+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . toThrowError (
8793 / U R L w i t h h o s t n a m e " e v i l .c o m " i s n o t a l l o w e d / ,
8894 ) ;
8995 } ) ;
@@ -93,7 +99,7 @@ describe('Validation Utils', () => {
9399 headers : { 'x-forwarded-port' : 'abc' } ,
94100 } ) ;
95101
96- expect ( ( ) => validateRequest ( req , allowedHosts ) ) . toThrowError (
102+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . toThrowError (
97103 'Header "x-forwarded-port" must be a numeric value.' ,
98104 ) ;
99105 } ) ;
@@ -102,16 +108,32 @@ describe('Validation Utils', () => {
102108 const req = new Request ( 'http://example.com' , {
103109 headers : { 'x-forwarded-proto' : 'ftp' } ,
104110 } ) ;
105- expect ( ( ) => validateRequest ( req , allowedHosts ) ) . toThrowError (
111+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . toThrowError (
106112 'Header "x-forwarded-proto" must be either "http" or "https".' ,
107113 ) ;
108114 } ) ;
109115
116+ it ( 'should pass for valid x-forwarded-proto (case-insensitive)' , ( ) => {
117+ const req = new Request ( 'http://example.com' , {
118+ headers : { 'x-forwarded-proto' : 'HTTP' } ,
119+ } ) ;
120+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . not . toThrow ( ) ;
121+ } ) ;
122+
110123 it ( 'should throw if host contains path separators' , ( ) => {
111124 const req = new Request ( 'http://example.com' , {
112125 headers : { 'host' : 'example.com/bad' } ,
113126 } ) ;
114- expect ( ( ) => validateRequest ( req , allowedHosts ) ) . toThrowError (
127+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . toThrowError (
128+ 'Header "host" contains characters that are not allowed.' ,
129+ ) ;
130+ } ) ;
131+
132+ it ( 'should throw if host contains invalid characters' , ( ) => {
133+ const req = new Request ( 'http://example.com' , {
134+ headers : { 'host' : 'example.com?query=1' } ,
135+ } ) ;
136+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . toThrowError (
115137 'Header "host" contains characters that are not allowed.' ,
116138 ) ;
117139 } ) ;
@@ -120,7 +142,7 @@ describe('Validation Utils', () => {
120142 const req = new Request ( 'http://example.com' , {
121143 headers : { 'x-forwarded-host' : 'example.com/bad' } ,
122144 } ) ;
123- expect ( ( ) => validateRequest ( req , allowedHosts ) ) . toThrowError (
145+ expect ( ( ) => validateRequest ( req , allowedHosts , false ) ) . toThrowError (
124146 'Header "x-forwarded-host" contains characters that are not allowed.' ,
125147 ) ;
126148 } ) ;
@@ -135,7 +157,7 @@ describe('Validation Utils', () => {
135157 } ,
136158 } ) ;
137159
138- expect ( ( ) => validateRequest ( request , allowedHosts ) )
160+ expect ( ( ) => validateRequest ( request , allowedHosts , false ) )
139161 . withContext ( `Prefix: "${ prefix } "` )
140162 . toThrowError (
141163 'Header "x-forwarded-prefix" must not start with multiple "/" or "\\" or contain ".", ".." path segments.' ,
@@ -168,7 +190,7 @@ describe('Validation Utils', () => {
168190 } ,
169191 } ) ;
170192
171- expect ( ( ) => validateRequest ( request , allowedHosts ) )
193+ expect ( ( ) => validateRequest ( request , allowedHosts , false ) )
172194 . withContext ( `Prefix: "${ prefix } "` )
173195 . toThrowError (
174196 'Header "x-forwarded-prefix" must not start with multiple "/" or "\\" or contain ".", ".." path segments.' ,
@@ -186,7 +208,7 @@ describe('Validation Utils', () => {
186208 } ,
187209 } ) ;
188210
189- expect ( ( ) => validateRequest ( request , allowedHosts ) )
211+ expect ( ( ) => validateRequest ( request , allowedHosts , false ) )
190212 . withContext ( `Prefix: "${ prefix } "` )
191213 . not . toThrow ( ) ;
192214 }
0 commit comments