@@ -63,43 +63,49 @@ def test_httprequest_location(self):
6363 'http://www.example.com/path/with:colons' )
6464
6565 def test_http_get_host (self ):
66- old_USE_X_FORWARDED_HOST = settings .USE_X_FORWARDED_HOST
66+ _old_USE_X_FORWARDED_HOST = settings .USE_X_FORWARDED_HOST
67+ _old_ALLOWED_HOSTS = settings .ALLOWED_HOSTS
6768 try :
6869 settings .USE_X_FORWARDED_HOST = False
70+ settings .ALLOWED_HOSTS = [
71+ 'forward.com' , 'example.com' , 'internal.com' , '12.34.56.78' ,
72+ '[2001:19f0:feee::dead:beef:cafe]' , 'xn--4ca9at.com' ,
73+ '.multitenant.com' , 'INSENSITIVE.com' ,
74+ ]
6975
7076 # Check if X_FORWARDED_HOST is provided.
7177 request = HttpRequest ()
7278 request .META = {
73- u 'HTTP_X_FORWARDED_HOST' : u 'forward.com' ,
74- u 'HTTP_HOST' : u 'example.com' ,
75- u 'SERVER_NAME' : u 'internal.com' ,
76- u 'SERVER_PORT' : 80 ,
79+ 'HTTP_X_FORWARDED_HOST' : 'forward.com' ,
80+ 'HTTP_HOST' : 'example.com' ,
81+ 'SERVER_NAME' : 'internal.com' ,
82+ 'SERVER_PORT' : 80 ,
7783 }
7884 # X_FORWARDED_HOST is ignored.
7985 self .assertEqual (request .get_host (), 'example.com' )
8086
8187 # Check if X_FORWARDED_HOST isn't provided.
8288 request = HttpRequest ()
8389 request .META = {
84- u 'HTTP_HOST' : u 'example.com' ,
85- u 'SERVER_NAME' : u 'internal.com' ,
86- u 'SERVER_PORT' : 80 ,
90+ 'HTTP_HOST' : 'example.com' ,
91+ 'SERVER_NAME' : 'internal.com' ,
92+ 'SERVER_PORT' : 80 ,
8793 }
8894 self .assertEqual (request .get_host (), 'example.com' )
8995
9096 # Check if HTTP_HOST isn't provided.
9197 request = HttpRequest ()
9298 request .META = {
93- u 'SERVER_NAME' : u 'internal.com' ,
94- u 'SERVER_PORT' : 80 ,
99+ 'SERVER_NAME' : 'internal.com' ,
100+ 'SERVER_PORT' : 80 ,
95101 }
96102 self .assertEqual (request .get_host (), 'internal.com' )
97103
98104 # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
99105 request = HttpRequest ()
100106 request .META = {
101- u 'SERVER_NAME' : u 'internal.com' ,
102- u 'SERVER_PORT' : 8042 ,
107+ 'SERVER_NAME' : 'internal.com' ,
108+ 'SERVER_PORT' : 8042 ,
103109 }
104110 self .assertEqual (request .get_host (), 'internal.com:8042' )
105111
@@ -112,6 +118,9 @@ def test_http_get_host(self):
112118 '[2001:19f0:feee::dead:beef:cafe]' ,
113119 '[2001:19f0:feee::dead:beef:cafe]:8080' ,
114120 'xn--4ca9at.com' , # Punnycode for öäü.com
121+ 'anything.multitenant.com' ,
122+ 'multitenant.com' ,
123+ 'insensitive.com' ,
115124 ]
116125
117126 poisoned_hosts = [
@@ -120,6 +129,7 @@ def test_http_get_host(self):
120129 'example.com:dr.frankenstein@evil.tld:80' ,
121130 'example.com:80/badpath' ,
122131 'example.com: recovermypassword.com' ,
132+ 'other.com' , # not in ALLOWED_HOSTS
123133 ]
124134
125135 for host in legit_hosts :
@@ -130,55 +140,57 @@ def test_http_get_host(self):
130140 request .get_host ()
131141
132142 for host in poisoned_hosts :
133- def test_host_poisoning ():
143+ def _test ():
134144 request = HttpRequest ()
135145 request .META = {
136146 'HTTP_HOST' : host ,
137147 }
138148 request .get_host ()
139- self .assertRaises (SuspiciousOperation , test_host_poisoning )
140-
149+ self .assertRaises (SuspiciousOperation , _test )
141150 finally :
142- settings .USE_X_FORWARDED_HOST = old_USE_X_FORWARDED_HOST
151+ settings .ALLOWED_HOSTS = _old_ALLOWED_HOSTS
152+ settings .USE_X_FORWARDED_HOST = _old_USE_X_FORWARDED_HOST
143153
144154 def test_http_get_host_with_x_forwarded_host (self ):
145- old_USE_X_FORWARDED_HOST = settings .USE_X_FORWARDED_HOST
155+ _old_USE_X_FORWARDED_HOST = settings .USE_X_FORWARDED_HOST
156+ _old_ALLOWED_HOSTS = settings .ALLOWED_HOSTS
146157 try :
147158 settings .USE_X_FORWARDED_HOST = True
159+ settings .ALLOWED_HOSTS = ['*' ]
148160
149161 # Check if X_FORWARDED_HOST is provided.
150162 request = HttpRequest ()
151163 request .META = {
152- u 'HTTP_X_FORWARDED_HOST' : u 'forward.com' ,
153- u 'HTTP_HOST' : u 'example.com' ,
154- u 'SERVER_NAME' : u 'internal.com' ,
155- u 'SERVER_PORT' : 80 ,
164+ 'HTTP_X_FORWARDED_HOST' : 'forward.com' ,
165+ 'HTTP_HOST' : 'example.com' ,
166+ 'SERVER_NAME' : 'internal.com' ,
167+ 'SERVER_PORT' : 80 ,
156168 }
157169 # X_FORWARDED_HOST is obeyed.
158170 self .assertEqual (request .get_host (), 'forward.com' )
159171
160172 # Check if X_FORWARDED_HOST isn't provided.
161173 request = HttpRequest ()
162174 request .META = {
163- u 'HTTP_HOST' : u 'example.com' ,
164- u 'SERVER_NAME' : u 'internal.com' ,
165- u 'SERVER_PORT' : 80 ,
175+ 'HTTP_HOST' : 'example.com' ,
176+ 'SERVER_NAME' : 'internal.com' ,
177+ 'SERVER_PORT' : 80 ,
166178 }
167179 self .assertEqual (request .get_host (), 'example.com' )
168180
169181 # Check if HTTP_HOST isn't provided.
170182 request = HttpRequest ()
171183 request .META = {
172- u 'SERVER_NAME' : u 'internal.com' ,
173- u 'SERVER_PORT' : 80 ,
184+ 'SERVER_NAME' : 'internal.com' ,
185+ 'SERVER_PORT' : 80 ,
174186 }
175187 self .assertEqual (request .get_host (), 'internal.com' )
176188
177189 # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
178190 request = HttpRequest ()
179191 request .META = {
180- u 'SERVER_NAME' : u 'internal.com' ,
181- u 'SERVER_PORT' : 8042 ,
192+ 'SERVER_NAME' : 'internal.com' ,
193+ 'SERVER_PORT' : 8042 ,
182194 }
183195 self .assertEqual (request .get_host (), 'internal.com:8042' )
184196
@@ -209,16 +221,33 @@ def test_http_get_host_with_x_forwarded_host(self):
209221 request .get_host ()
210222
211223 for host in poisoned_hosts :
212- def test_host_poisoning ():
224+ def _test ():
213225 request = HttpRequest ()
214226 request .META = {
215227 'HTTP_HOST' : host ,
216228 }
217229 request .get_host ()
218- self .assertRaises (SuspiciousOperation , test_host_poisoning )
230+ self .assertRaises (SuspiciousOperation , _test )
231+ finally :
232+ settings .ALLOWED_HOSTS = _old_ALLOWED_HOSTS
233+ settings .USE_X_FORWARDED_HOST = _old_USE_X_FORWARDED_HOST
234+
235+ def test_host_validation_disabled_in_debug_mode (self ):
236+ """If ALLOWED_HOSTS is empty and DEBUG is True, all hosts pass."""
237+ _old_DEBUG = settings .DEBUG
238+ _old_ALLOWED_HOSTS = settings .ALLOWED_HOSTS
239+ try :
240+ settings .DEBUG = True
241+ settings .ALLOWED_HOSTS = []
219242
243+ request = HttpRequest ()
244+ request .META = {
245+ 'HTTP_HOST' : 'example.com' ,
246+ }
247+ self .assertEqual (request .get_host (), 'example.com' )
220248 finally :
221- settings .USE_X_FORWARDED_HOST = old_USE_X_FORWARDED_HOST
249+ settings .DEBUG = _old_DEBUG
250+ settings .ALLOWED_HOSTS = _old_ALLOWED_HOSTS
222251
223252 def test_near_expiration (self ):
224253 "Cookie will expire when an near expiration time is provided"
0 commit comments