diff --git a/starlette/middleware/trustedhost.py b/starlette/middleware/trustedhost.py index f8ccb65a..39836524 100644 --- a/starlette/middleware/trustedhost.py +++ b/starlette/middleware/trustedhost.py @@ -11,9 +11,12 @@ class TrustedHostMiddleware: def __init__( self, app: ASGIApp, - allowed_hosts: typing.Sequence[str] = ["*"], + allowed_hosts: typing.Sequence[str] = None, www_redirect: bool = True, ) -> None: + if allowed_hosts is None: + allowed_hosts = ["*"] + for pattern in allowed_hosts: assert "*" not in pattern[1:], ENFORCE_DOMAIN_WILDCARD if pattern.startswith("*") and pattern != "*": diff --git a/tests/middleware/test_trusted_host.py b/tests/middleware/test_trusted_host.py index 54b2a0c1..934f2477 100644 --- a/tests/middleware/test_trusted_host.py +++ b/tests/middleware/test_trusted_host.py @@ -28,6 +28,12 @@ def test_trusted_host_middleware(): assert response.status_code == 400 +def test_default_allowed_hosts(): + app = Starlette() + middleware = TrustedHostMiddleware(app) + assert middleware.allowed_hosts == ["*"] + + def test_www_redirect(): app = Starlette()