mirror of
https://github.com/inventree/InvenTree.git
synced 2026-12-03 12:51:34 +00:00
Adjust default value for INVENTRE_ALLOWED_HOSTS (#13416)
* Adjust default value for INVENTRE_ALLOWED_HOSTS Starting in 1.8.0 the default value for INVENTREE_ALLOWED_HOSTS is unset. Currently it is '*' - which is overly permissive and can lead to host spoofing and other security weaknesses. The new default value (only applied to fresh setups) will require more work from the sys-admin but is safer overall * Add CHANGELOG entry * adjust settings for unit tests * Allow testserver for schema building * Add INVE-W23
This commit is contained in:
1 parent
855164e91f
commit
819231c342
9 files changed
+82
-8
No files matched your search
@@ -110,6 +110,15 @@ class Command(spectacular.Command):
|
||||
|
||||
settings.SPECTACULAR_SETTINGS['APPEND_COMPONENTS'] = components
|
||||
|
||||
# drf-spectacular builds mock requests via APIRequestFactory, which uses
|
||||
# the 'testserver' host. Allow it for this command only (no server is
|
||||
# running here), as ALLOWED_HOSTS no longer defaults to '*'.
|
||||
if (
|
||||
'*' not in settings.ALLOWED_HOSTS
|
||||
and 'testserver' not in settings.ALLOWED_HOSTS
|
||||
):
|
||||
settings.ALLOWED_HOSTS = [*settings.ALLOWED_HOSTS, 'testserver']
|
||||
|
||||
super().handle(*args, **kwargs)
|
||||
|
||||
return 'done'
|
||||
@@ -885,7 +885,7 @@ SITE_ID = get_setting('INVENTREE_SITE_ID', 'site_id', 1 if SITE_MULTI else None)
|
||||
if not SITE_MULTI:
|
||||
INSTALLED_APPS.remove('django.contrib.sites')
|
||||
|
||||
# List of allowed hosts (default = allow all)
|
||||
# List of allowed hosts (default = host of SITE_URL; all hosts in debug mode)
|
||||
# Ref: https://docs.djangoproject.com/en/4.2/ref/settings/#allowed-hosts
|
||||
ALLOWED_HOSTS = get_setting(
|
||||
'INVENTREE_ALLOWED_HOSTS',
|
||||
@@ -920,6 +920,11 @@ for i, host in enumerate(ALLOWED_HOSTS):
|
||||
if ':' in host:
|
||||
ALLOWED_HOSTS[i] = host = host.split(':')[0]
|
||||
|
||||
if '*' in ALLOWED_HOSTS and not DEBUG and isInServerThread():
|
||||
logger.warning(
|
||||
'INVE-W23: ALLOWED_HOSTS is set to allow all hosts. This is not secure, and should not be used in production'
|
||||
)
|
||||
|
||||
# List of trusted origins for unsafe requests
|
||||
# Ref: https://docs.djangoproject.com/en/4.2/ref/settings/#csrf-trusted-origins
|
||||
CSRF_TRUSTED_ORIGINS = get_setting(
|
||||
|
||||
@@ -322,14 +322,28 @@ class MiddlewareTests(InvenTreeTestCase):
|
||||
def test_site_lax_protocol(self):
|
||||
"""Test that the site URL check is correctly working with/without lax protocol check."""
|
||||
# Test that a completely different host fails
|
||||
# (host must pass ALLOWED_HOSTS to reach the site URL check)
|
||||
with self.settings(
|
||||
SITE_URL='https://testserver', CSRF_TRUSTED_ORIGINS=['https://testserver']
|
||||
SITE_URL='https://testserver',
|
||||
CSRF_TRUSTED_ORIGINS=['https://testserver'],
|
||||
ALLOWED_HOSTS=['testserver', 'otherhost.example.com'],
|
||||
):
|
||||
response = self.client.get(
|
||||
reverse('web'), headers={'host': 'otherhost.example.com'}
|
||||
)
|
||||
self.assertContains(response, 'INVE-E7: The visited path', status_code=500)
|
||||
|
||||
# A host not in ALLOWED_HOSTS is rejected by Django before the site URL check
|
||||
with self.settings(
|
||||
SITE_URL='https://testserver',
|
||||
CSRF_TRUSTED_ORIGINS=['https://testserver'],
|
||||
ALLOWED_HOSTS=['testserver'],
|
||||
):
|
||||
response = self.client.get(
|
||||
reverse('web'), headers={'host': 'otherhost.example.com'}
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
# Simple setup with proxy
|
||||
with self.settings(
|
||||
SITE_URL='https://testserver', CSRF_TRUSTED_ORIGINS=['https://testserver']
|
||||
@@ -377,6 +391,8 @@ class MiddlewareTests(InvenTreeTestCase):
|
||||
'http://testserver',
|
||||
'https://testserver.example.com',
|
||||
],
|
||||
# Hosts must pass ALLOWED_HOSTS to reach the site URL check
|
||||
ALLOWED_HOSTS=['testserver', '.example.com'],
|
||||
):
|
||||
# this will run with testserver as host by default
|
||||
response = self.client.get(reverse('web'))
|
||||
|
||||
@@ -193,6 +193,13 @@ def get_core_admin_messages() -> list[AdminMessage]:
|
||||
bool(settings.CORS_ALLOW_ALL_ORIGINS),
|
||||
code='INVE-W16',
|
||||
),
|
||||
_check(
|
||||
'allowed_hosts',
|
||||
_('Allowed Hosts'),
|
||||
_('ALLOWED_HOSTS is set to allow all hosts.'),
|
||||
'*' in settings.ALLOWED_HOSTS,
|
||||
code='INVE-W23',
|
||||
),
|
||||
_check(
|
||||
'basic_auth',
|
||||
_('Basic Authentication'),
|
||||
|
||||
@@ -3679,6 +3679,7 @@ class AdminMessageAPITests(InvenTreeAPITestCase):
|
||||
'email',
|
||||
'migrations',
|
||||
'cors',
|
||||
'allowed_hosts',
|
||||
'basic_auth',
|
||||
'plugin_integration',
|
||||
}
|
||||
@@ -3728,6 +3729,7 @@ class AdminMessageAPITests(InvenTreeAPITestCase):
|
||||
self.assertIsNone(msg['plugin'])
|
||||
|
||||
self.assertEqual(messages['basic_auth']['code'], 'INVE-W18')
|
||||
self.assertEqual(messages['allowed_hosts']['code'], 'INVE-W23')
|
||||
|
||||
def test_ordering(self):
|
||||
"""Messages are returned most-severe first."""
|
||||
@@ -3748,6 +3750,12 @@ class AdminMessageAPITests(InvenTreeAPITestCase):
|
||||
self.assertEqual(messages['cors']['classifier'], 'success')
|
||||
self.assertEqual(messages['basic_auth']['classifier'], 'success')
|
||||
|
||||
with override_settings(ALLOWED_HOSTS=['testserver', '*']):
|
||||
self.assertEqual(self._messages()['allowed_hosts']['classifier'], 'warning')
|
||||
|
||||
with override_settings(ALLOWED_HOSTS=['testserver', 'inventree.example.com']):
|
||||
self.assertEqual(self._messages()['allowed_hosts']['classifier'], 'success')
|
||||
|
||||
set_global_setting('_PENDING_MIGRATIONS', 3, None)
|
||||
set_global_setting('SERVER_RESTART_REQUIRED', True, None)
|
||||
messages = self._messages()
|
||||
|
||||
@@ -133,9 +133,10 @@ auto_update: False
|
||||
|
||||
# Allowed hosts (see ALLOWED_HOSTS in Django settings documentation)
|
||||
# A list of strings representing the host/domain names that this Django site can serve.
|
||||
# Default behaviour is to allow all hosts (THIS IS NOT SECURE!)
|
||||
allowed_hosts:
|
||||
- '*'
|
||||
# If not specified, only the host of the site_url is allowed.
|
||||
# Do not use '*' in production - it allows Host header spoofing
|
||||
#allowed_hosts:
|
||||
# - 'inventree.example.com'
|
||||
|
||||
# Trusted origins (see CSRF_TRUSTED_ORIGINS in Django settings documentation)
|
||||
# If you are running behind a proxy, you may need to add the proxy address here
|
||||
|
||||
Reference in new issue
Block a user