mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #19987 -- Disabled host validation when DEBUG=True.
The documentation promises that host validation is disabled when DEBUG=True, that all hostnames are accepted. Domains not compliant with RFC 1034/1035 were however being validated, this validation has now been removed when DEBUG=True. Additionally, when DEBUG=False a more detailed SuspiciousOperation exception message is provided when host validation fails because the hostname is not RFC 1034/1035 compliant.
This commit is contained in:
parent
acd1d439fd
commit
1c3c21b38d
2 changed files with 23 additions and 2 deletions
|
@ -620,12 +620,20 @@ class HostValidationTests(SimpleTestCase):
|
|||
}
|
||||
self.assertEqual(request.get_host(), 'example.com')
|
||||
|
||||
# Invalid hostnames would normally raise a SuspiciousOperation,
|
||||
# but we have DEBUG=True, so this check is disabled.
|
||||
request = HttpRequest()
|
||||
request.META = {
|
||||
'HTTP_HOST': "invalid_hostname.com",
|
||||
}
|
||||
self.assertEqual(request.get_host(), "invalid_hostname.com")
|
||||
|
||||
@override_settings(ALLOWED_HOSTS=[])
|
||||
def test_get_host_suggestion_of_allowed_host(self):
|
||||
"""get_host() makes helpful suggestions if a valid-looking host is not in ALLOWED_HOSTS."""
|
||||
msg_invalid_host = "Invalid HTTP_HOST header: %r."
|
||||
msg_suggestion = msg_invalid_host + "You may need to add %r to ALLOWED_HOSTS."
|
||||
msg_suggestion2 = msg_invalid_host + "The domain name provided is not valid according to RFC 1034/1035"
|
||||
|
||||
for host in [ # Valid-looking hosts
|
||||
'example.com',
|
||||
|
@ -664,6 +672,14 @@ class HostValidationTests(SimpleTestCase):
|
|||
request.get_host
|
||||
)
|
||||
|
||||
request = HttpRequest()
|
||||
request.META = {'HTTP_HOST': "invalid_hostname.com"}
|
||||
self.assertRaisesMessage(
|
||||
SuspiciousOperation,
|
||||
msg_suggestion2 % "invalid_hostname.com",
|
||||
request.get_host
|
||||
)
|
||||
|
||||
|
||||
@skipIf(connection.vendor == 'sqlite'
|
||||
and connection.settings_dict['TEST_NAME'] in (None, '', ':memory:'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue