From 1b35f0878ee75f1b87587a6f60d6786b9fa6290d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 4 Dec 2016 10:57:24 -0800 Subject: [PATCH] Fix CORS when static resources registered (#4727) --- homeassistant/components/http/__init__.py | 6 ++++++ tests/components/http/test_init.py | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index dc18dd2481d..11a9e755bb4 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -277,9 +277,15 @@ class HomeAssistantWSGI(object): @asyncio.coroutine def start(self): """Start the wsgi server.""" + cors_added = set() if self.cors is not None: for route in list(self.app.router.routes()): + if hasattr(route, 'resource'): + route = route.resource + if route in cors_added: + continue self.cors.add(route) + cors_added.add(route) if self.ssl_certificate: context = ssl.SSLContext(SSL_VERSION) diff --git a/tests/components/http/test_init.py b/tests/components/http/test_init.py index cd0d4fe1ffa..f50e1fb9dbf 100644 --- a/tests/components/http/test_init.py +++ b/tests/components/http/test_init.py @@ -44,6 +44,10 @@ def setUpModule(): bootstrap.setup_component(hass, 'api') + # Registering static path as it caused CORS to blow up + hass.http.register_static_path( + '/custom_components', hass.config.path('custom_components')) + hass.start() @@ -53,11 +57,12 @@ def tearDownModule(): hass.stop() -class TestHttp: +class TestCors: """Test HTTP component.""" def test_cors_allowed_with_password_in_url(self): """Test cross origin resource sharing with password in url.""" + req = requests.get(_url(const.URL_API), params={'api_password': API_PASSWORD}, headers={const.HTTP_HEADER_ORIGIN: HTTP_BASE_URL})