mirror of
https://github.com/inventree/InvenTree.git
synced 2026-12-03 04:41:52 +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
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
- [#13271](https://github.com/inventree/InvenTree/pull/13271) makes a number of API fields read-only, as they are managed by dedicated endpoints and must not be edited directly: `consumed` (BuildLine), `received_date` (ReturnOrderLineItem), `barcode_hash` (ManufacturerPart, SalesOrderShipment), `issue_date` (SalesOrder, ReturnOrder, TransferOrder) and `complete_date` (ReturnOrder, TransferOrder). Values provided for these fields are now ignored.
|
||||
- [#13293](https://github.com/inventree/InvenTree/pull/13293) moves various (internal) model base classes, if your plugin uses InvenTree base classes update your imports from InvenTree.models to generic.models before the next major release.
|
||||
- [#13416](https://github.com/inventree/InvenTree/pull/13416) The configuration file template no longer sets `allowed_hosts` to `*`. Allowed hosts now default to the host of `INVENTREE_SITE_URL`, and a production server with neither `INVENTREE_SITE_URL` nor `INVENTREE_ALLOWED_HOSTS` set will not start. Existing configuration files are not changed - remove `*` from `allowed_hosts` and set `INVENTREE_SITE_URL` instead. A warning (INVE-W23) is now shown in the admin center if `*` is present.
|
||||
|
||||
### Added
|
||||
|
||||
|
||||
@@ -297,6 +297,15 @@ The message lists each affected plugin, together with the plugin settings which
|
||||
|
||||
If the check itself fails, the message "Unable to check plugin integrations" is shown instead. Check the server log for details.
|
||||
|
||||
#### INVE-W23
|
||||
**Allowed hosts is set to allow all hosts - Backend**
|
||||
|
||||
The `INVENTREE_ALLOWED_HOSTS` / `allowed_hosts` setting contains `*`, which allows requests with any `Host` header. Older versions of the configuration file template set this value by default.
|
||||
|
||||
It is recommended to remove `*` and set the [site URL](../start/config.md#site-url) instead; the host of the site URL is allowed automatically. Any additional host names can be added to the allowed hosts list - see the [allowed hosts settings](../start/config.md#allowed-hosts).
|
||||
|
||||
This warning is also shown in [debug mode](../start/config.md#debug-mode), where all hosts are allowed if no allowed hosts are specified.
|
||||
|
||||
|
||||
### INVE-I (InvenTree Information)
|
||||
Information — These are not errors but information messages. They might point out potential issues or just provide information.
|
||||
|
||||
@@ -75,6 +75,15 @@ The *INVENTREE_SITE_URL* option defines the base URL for the InvenTree server. T
|
||||
|
||||
The site URL is the URL that users will use to access the InvenTree server. For example, if the server is accessible at `https://inventree.example.com`, the site URL should be set to `https://inventree.example.com`. Note that this is not necessarily the same as the internal URL that the server is running on - the internal URL will depend entirely on your server configuration and may be obscured by a reverse proxy or other such setup.
|
||||
|
||||
The site URL is used for:
|
||||
|
||||
- **Allowed hosts** - the host part of the site URL is always added to the list of [allowed hosts](#allowed-hosts).
|
||||
- **Trusted origins** - it is used as the default for the trusted CSRF and CORS origins (see [Server Access](#server-access)).
|
||||
- **Links in emails** - links sent out by the server are built from the site URL, not from the address used to make the request.
|
||||
|
||||
!!! warning "Required in Production"
|
||||
If neither `INVENTREE_SITE_URL` nor `INVENTREE_ALLOWED_HOSTS` is specified, the server will refuse to start (unless running in [debug mode](#debug-mode)).
|
||||
|
||||
### Timezone
|
||||
|
||||
By default, the InvenTree server is configured to use the UTC timezone. This can be adjusted to your desired local timezone. You can refer to [Wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) for a list of available timezones. Use the values specified in the *TZ Identifier* column in the linked page. For example, to change to the United States Pacific timezone, set `INVENTREE_TIMEZONE='America/Los_Angeles'`.
|
||||
@@ -119,7 +128,7 @@ Depending on how your InvenTree installation is configured, you will need to pay
|
||||
The following settings require a certain assumed level of knowledge. You should also refer to the [django documentation]({% include "django.html" %}/ref/settings/) for more information.
|
||||
|
||||
!!! danger "Not Secure"
|
||||
Allowing access from any host is not secure, and should be adjusted for your installation.
|
||||
Allowing access from any host (e.g. setting `INVENTREE_ALLOWED_HOSTS` to `*`) is not secure, and should not be used in production. Refer to [allowed hosts](#allowed-hosts) below.
|
||||
|
||||
!!! success "INVENTREE_SITE_URL"
|
||||
If you have specified the `INVENTREE_SITE_URL`, this will automatically be used as a trusted CSRF and CORS host (see below).
|
||||
@@ -128,7 +137,7 @@ Depending on how your InvenTree installation is configured, you will need to pay
|
||||
If InvenTree is served behind an existing reverse proxy (e.g. NGINX, Traefik) with SSL termination happening upstream, refer to the [worked example](./processes.md#integrating-with-existing-proxy) for the combination of `INVENTREE_TRUSTED_ORIGINS` and `INVENTREE_USE_X_FORWARDED_*` settings that setup requires.
|
||||
|
||||
{{ configtable() }}
|
||||
{{ configsetting("INVENTREE_ALLOWED_HOSTS") }} List of allowed hosts |
|
||||
{{ configsetting("INVENTREE_ALLOWED_HOSTS", default="Uses the *INVENTREE_SITE_URL* parameter, if set. Otherwise, an empty list.") }} List of allowed hosts. Do not set this to `*` in production, as it allows Host header spoofing. |
|
||||
{{ configsetting("INVENTREE_TRUSTED_ORIGINS", default="Uses the *INVENTREE_SITE_URL* parameter, if set. Otherwise, an empty list.") }} List of trusted origins. Refer to the [django documentation]({% include "django.html" %}/ref/settings/#csrf-trusted-origins) |
|
||||
{{ configsetting("INVENTREE_CORS_ORIGIN_ALLOW_ALL") }} Allow all remote URLS for CORS checks |
|
||||
{{ configsetting("INVENTREE_CORS_ORIGIN_WHITELIST", default="Uses the *INVENTREE_SITE_URL* parameter, if set. Otherwise, an empty list.") }} List of whitelisted CORS URLs. Refer to the [django-cors-headers documentation](https://github.com/adamchainz/django-cors-headers#cors_allowed_origins-sequencestr) |
|
||||
@@ -144,6 +153,15 @@ Depending on how your InvenTree installation is configured, you will need to pay
|
||||
{{ configsetting("INVENTREE_THROTTLE_ANON") }} Throttle rate for anonymous users (e.g. '20/minute') |
|
||||
{{ configsetting("INVENTREE_THROTTLE_USER") }} Throttle rate for authenticated users (e.g. '5/second') |
|
||||
|
||||
### Allowed Hosts
|
||||
|
||||
The `INVENTREE_ALLOWED_HOSTS` setting is a list of the host (domain) names that the InvenTree server will respond to. Requests made with any other `Host` header are rejected. Refer to the [django documentation]({% include "django.html" %}/ref/settings/#allowed-hosts) for more information.
|
||||
|
||||
In most cases, this setting does not need to be specified: the host of the [site URL](#site-url) is automatically allowed. You only need to set it if users access the server using additional names.
|
||||
|
||||
!!! danger "Do not allow all hosts"
|
||||
Setting `INVENTREE_ALLOWED_HOSTS` to `*` allows a client to send requests with any `Host` header. This can be exploited to spoof links and redirects. Older versions of the [configuration file template](#configuration-file-template) set this value to `*` by default - if your configuration file contains this value, you should remove it and set the [site URL](#site-url) instead.
|
||||
|
||||
### Debug Mode
|
||||
|
||||
Note that in [debug mode](./index.md#debug-mode), some of the above settings are automatically adjusted to allow for easier development. The following settings are internally overridden in debug mode with the values specified below:
|
||||
@@ -161,7 +179,7 @@ Note that if you set the `INVENTREE_COOKIE_SAMESITE` to `None`, then `INVENTREE_
|
||||
|
||||
### Proxy Considerations
|
||||
|
||||
If you are running InvenTree behind a proxy, or forwarded HTTPS connections, you will need to ensure that the InvenTree server is configured to listen on the correct host and port. You will likely have to adjust the `INVENTREE_ALLOWED_HOSTS` setting to ensure that the server will accept requests from the proxy.
|
||||
If you are running InvenTree behind a proxy, or forwarded HTTPS connections, you will need to ensure that the InvenTree server is configured to listen on the correct host and port. Ensure that the [allowed hosts](#allowed-hosts) include the host which the proxy forwards to the InvenTree server.
|
||||
|
||||
Additionally, you may need to configure the following header to ensure that the InvenTree server is watching for information forwarded by the proxy:
|
||||
|
||||
|
||||
@@ -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