diff --git a/docs/configuration.md b/docs/configuration.md index 903347aba..a2b064faf 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -510,9 +510,9 @@ You can read more about this in [the Django project's documentation](https://doc - With neither setting configured, Paperless accepts any host. - With only `PAPERLESS_URL` set, Paperless accepts the hostname from that URL - and `localhost` (used by the Docker health check). - - `PAPERLESS_ALLOWED_HOSTS` adds hosts to `localhost` and the hostname from - `PAPERLESS_URL`, if set. A blank list adds no hosts. + and the loopback hosts `localhost`, `127.0.0.1`, and `[::1]`. + - `PAPERLESS_ALLOWED_HOSTS` adds hosts to those loopback hosts and the hostname + from `PAPERLESS_URL`, if set. A blank list adds no hosts. - With `PAPERLESS_ALLOWED_HOSTS=*`, Paperless accepts any host, even when `PAPERLESS_URL` is set. diff --git a/src/paperless/settings/__init__.py b/src/paperless/settings/__init__.py index ed01604c5..3db1e88d0 100644 --- a/src/paperless/settings/__init__.py +++ b/src/paperless/settings/__init__.py @@ -458,8 +458,8 @@ def _get_allowed_hosts() -> list[str]: else: allowed_hosts = ["*"] if allowed_hosts != ["*"]: - # Always allow localhost for the Docker health check. - allowed_hosts.append("localhost") + # Allow local access and health checks via loopback hostnames and addresses. + allowed_hosts.extend(["localhost", "127.0.0.1", "[::1]"]) return allowed_hosts diff --git a/src/paperless/tests/settings/test_settings.py b/src/paperless/tests/settings/test_settings.py index 996b9c037..232aea266 100644 --- a/src/paperless/tests/settings/test_settings.py +++ b/src/paperless/tests/settings/test_settings.py @@ -177,11 +177,17 @@ class TestPaperlessURLSettings(TestCase): ("url", "allowed_hosts", "expected", "accepted_host", "rejected_host"), [ (None, None, ["*"], "other.example.com", None), - (None, "", ["localhost"], "localhost:8000", "other.example.com"), + ( + None, + "", + ["localhost", "127.0.0.1", "[::1]"], + "localhost:8000", + "other.example.com", + ), ( None, "lan.example.com", - ["lan.example.com", "localhost"], + ["lan.example.com", "localhost", "127.0.0.1", "[::1]"], "lan.example.com", "other.example.com", ), @@ -189,29 +195,29 @@ class TestPaperlessURLSettings(TestCase): ( "https://example.com:8443", None, - ["localhost", "example.com"], + ["localhost", "127.0.0.1", "[::1]", "example.com"], "example.com:8443", "other.example.com", ), ( "https://example.com.", "", - ["localhost", "example.com"], + ["localhost", "127.0.0.1", "[::1]", "example.com"], "example.com.", "other.example.com", ), ( - "http://[::1]:8000", + "http://[2001:db8::1]:8000", None, - ["localhost", "[::1]"], - "[::1]:8000", + ["localhost", "127.0.0.1", "[::1]", "[2001:db8::1]"], + "[2001:db8::1]:8000", "other.example.com", ), ("https://example.com", "*", ["*", "example.com"], "other.example.com", None), ( "https://example.com", "lan.example.com", - ["lan.example.com", "localhost", "example.com"], + ["lan.example.com", "localhost", "127.0.0.1", "[::1]", "example.com"], "lan.example.com", "other.example.com", ), @@ -241,9 +247,10 @@ def test_allowed_hosts_at_startup( assert expected == paperless_settings.ALLOWED_HOSTS request = HttpRequest() - with override_settings(ALLOWED_HOSTS=expected): - request.META["HTTP_HOST"] = accepted_host - assert request.get_host() == accepted_host + with override_settings(ALLOWED_HOSTS=paperless_settings.ALLOWED_HOSTS): + for host in (accepted_host, "localhost:8000", "127.0.0.1:30070", "[::1]:8000"): + request.META["HTTP_HOST"] = host + assert request.get_host() == host if rejected_host: request.META["HTTP_HOST"] = rejected_host with pytest.raises(DisallowedHost):