Ticket #27575: django-27575.diff
| File django-27575.diff, 5.4 KB (added by , 2 weeks ago) |
|---|
-
django/core/handlers/base.py
diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index d65a5ed..302865f 100644
a b class BaseHandler: 311 311 resolver = get_resolver(urlconf) 312 312 else: 313 313 resolver = get_resolver() 314 # Request middleware may have normalized proxy headers. Validate the 315 # effective host before resolving the URL or calling view middleware. 316 request.get_host() 314 317 # Resolve the view, and assign the match object back to the request. 315 318 resolver_match = resolver.resolve(request.path_info) 316 319 request.resolver_match = resolver_match -
docs/ref/settings.txt
diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index a1ab130..a929780 100644
a b is validated against ``['.localhost', '127.0.0.1', '[::1]']``. 103 103 ``ALLOWED_HOSTS`` is also :ref:`checked when running tests 104 104 <topics-testing-advanced-multiple-hosts>`. 105 105 106 This validation only applies via :meth:`~django.http.HttpRequest.get_host`; 107 if your code accesses the ``Host`` header directly from ``request.META`` you 108 are bypassing this security protection. 106 Django calls :meth:`~django.http.HttpRequest.get_host` before resolving the 107 request URL, after middleware has had the opportunity to normalize proxy 108 headers. Middleware that uses the host before calling ``get_response()``, 109 including middleware that returns its own response, should call 110 :meth:`~django.http.HttpRequest.get_host` to obtain the validated host. 111 Reading a header directly from ``request.META`` doesn't validate it. 109 112 110 113 .. setting:: APPEND_SLASH 111 114 -
docs/releases/6.2.txt
diff --git a/docs/releases/6.2.txt b/docs/releases/6.2.txt index fc7475a..1e7c400 100644
a b Models 458 458 Requests and responses 459 459 ---------------------- 460 460 461 * Django now checks :setting:`ALLOWED_HOSTS` before URL resolution even when 462 no middleware or view calls :meth:`~django.http.HttpRequest.get_host`. 463 Requests that reach URL resolution with a disallowed host now receive a 464 400 response. Middleware that returns a response earlier remains responsible 465 for validating any host it uses. 466 461 467 * Most Django-provided middleware now set ``async_capable = False`` to 462 468 avoid repetitive context switching in ``MiddlewareMixin.__acall__()`` under 463 469 ASGI. For atypical use cases, e.g. high in-process concurrency over non-ORM -
docs/topics/security.txt
diff --git a/docs/topics/security.txt b/docs/topics/security.txt index 075bc98..471115c 100644
a b Because even seemingly-secure web server configurations are susceptible to fake 225 225 :setting:`ALLOWED_HOSTS` setting in the 226 226 :meth:`django.http.HttpRequest.get_host` method. 227 227 228 This validation only applies via :meth:`~django.http.HttpRequest.get_host`; 229 if your code accesses the ``Host`` header directly from ``request.META`` you 230 are bypassing this security protection. 228 The request handler calls :meth:`~django.http.HttpRequest.get_host` before 229 resolving the URL. Middleware that reads the host earlier should use the same 230 method. Accessing headers directly through ``request.META`` doesn't validate 231 them. 231 232 232 233 For more details see the full :setting:`ALLOWED_HOSTS` documentation. 233 234 -
tests/handlers/tests.py
diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py index b1da08f..beedb70 100644
a b def empty_middleware(get_response): 199 199 pass 200 200 201 201 202 def last_forwarded_host_middleware(get_response): 203 def middleware(request): 204 forwarded = request.META["HTTP_X_FORWARDED_HOST"] 205 request.META["HTTP_X_FORWARDED_HOST"] = forwarded.split(",")[-1].strip() 206 return get_response(request) 207 208 return middleware 209 210 211 @override_settings( 212 ALLOWED_HOSTS=["example.com"], ROOT_URLCONF="handlers.urls", MIDDLEWARE=[] 213 ) 214 class HostValidationTests(SimpleTestCase): 215 def test_disallowed_host(self): 216 with self.assertLogs("django.security.DisallowedHost", "ERROR"): 217 response = self.client.get("/regular/", headers={"host": "other.test"}) 218 self.assertEqual(response.status_code, 400) 219 self.assertIsNone(response.wsgi_request.resolver_match) 220 221 async def test_disallowed_host_async(self): 222 with self.assertLogs("django.security.DisallowedHost", "ERROR"): 223 response = await self.async_client.request( 224 path="/async_regular/", headers=[(b"host", b"other.test")] 225 ) 226 self.assertEqual(response.status_code, 400) 227 self.assertIsNone(response.asgi_request.resolver_match) 228 229 def test_allowed_host(self): 230 response = self.client.get("/regular/", headers={"host": "example.com:8080"}) 231 self.assertContains(response, "regular content") 232 233 @override_settings( 234 MIDDLEWARE=["handlers.tests.last_forwarded_host_middleware"], 235 USE_X_FORWARDED_HOST=True, 236 ) 237 def test_forwarded_host_after_middleware(self): 238 for host, status in [("example.com", 200), ("other.test", 400)]: 239 with self.subTest(host=host): 240 response = self.client.get( 241 "/regular/", 242 headers={"x-forwarded-host": f"proxy.internal, {host}"}, 243 ) 244 self.assertEqual(response.status_code, status) 245 246 202 247 @override_settings(ROOT_URLCONF="handlers.urls") 203 248 class HandlerRequestTests(SimpleTestCase): 204 249 request_factory = RequestFactory()