From 57f84cbbaa6945b4326dc1e9f0b70870325c714e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 4 Aug 2019 23:24:54 -0700 Subject: [PATCH] Update HTTP defaults (#25702) * Update HTTP defaults * Fix tests --- homeassistant/components/http/__init__.py | 4 +++- homeassistant/components/http/cors.py | 3 +++ tests/components/http/test_init.py | 9 +++++++++ tests/scripts/test_check_config.py | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index 84c7d15a5804fd..6d31c3fc700629 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -51,6 +51,8 @@ DEFAULT_SERVER_HOST = "0.0.0.0" DEFAULT_DEVELOPMENT = "0" +# To be able to load custom cards. +DEFAULT_CORS = "https://cast.home-assistant.io" NO_LOGIN_ATTEMPT_THRESHOLD = -1 @@ -91,7 +93,7 @@ def api_password_deprecated(value): vol.Optional(CONF_SSL_CERTIFICATE): cv.isfile, vol.Optional(CONF_SSL_PEER_CERTIFICATE): cv.isfile, vol.Optional(CONF_SSL_KEY): cv.isfile, - vol.Optional(CONF_CORS_ORIGINS, default=[]): vol.All( + vol.Optional(CONF_CORS_ORIGINS, default=[DEFAULT_CORS]): vol.All( cv.ensure_list, [cv.string] ), vol.Inclusive(CONF_USE_X_FORWARDED_FOR, "proxy"): cv.boolean, diff --git a/homeassistant/components/http/cors.py b/homeassistant/components/http/cors.py index 5c24ecbebed1e5..19fe88c5cde644 100644 --- a/homeassistant/components/http/cors.py +++ b/homeassistant/components/http/cors.py @@ -45,6 +45,9 @@ def _allow_cors(route, config=None): path = path.canonical + if path.startswith("/api/hassio_ingress/"): + return + if path in cors_added: return diff --git a/tests/components/http/test_init.py b/tests/components/http/test_init.py index a3837a0b745198..d8e613df6df01a 100644 --- a/tests/components/http/test_init.py +++ b/tests/components/http/test_init.py @@ -232,3 +232,12 @@ async def test_ssl_profile_change_modern(hass): await hass.async_block_till_done() assert len(mock_context.mock_calls) == 1 + + +async def test_cors_defaults(hass): + """Test the CORS default settings.""" + with patch("homeassistant.components.http.setup_cors") as mock_setup: + assert await async_setup_component(hass, "http", {}) + + assert len(mock_setup.mock_calls) == 1 + assert mock_setup.mock_calls[0][1][1] == ["https://cast.home-assistant.io"] diff --git a/tests/scripts/test_check_config.py b/tests/scripts/test_check_config.py index 98c634cd400adc..a07b812bc96830 100644 --- a/tests/scripts/test_check_config.py +++ b/tests/scripts/test_check_config.py @@ -103,7 +103,7 @@ def test_secrets(isfile_patch, loop): assert res["components"].keys() == {"homeassistant", "http"} assert res["components"]["http"] == { "api_password": "abc123", - "cors_allowed_origins": [], + "cors_allowed_origins": ["https://cast.home-assistant.io"], "ip_ban_enabled": True, "login_attempts_threshold": -1, "server_host": "0.0.0.0",