Fix typo in KEY_ALLOW_CONFIGRED_CORS (#119905)

This commit is contained in:
J. Nick Koston 2024-06-18 12:44:27 -05:00 committed by GitHub
parent d2faaa1531
commit 419dcbf9a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 14 additions and 14 deletions

View File

@ -37,7 +37,7 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import storage from homeassistant.helpers import storage
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.http import ( from homeassistant.helpers.http import (
KEY_ALLOW_CONFIGRED_CORS, KEY_ALLOW_CONFIGURED_CORS,
KEY_AUTHENTICATED, # noqa: F401 KEY_AUTHENTICATED, # noqa: F401
KEY_HASS, KEY_HASS,
HomeAssistantView, HomeAssistantView,
@ -427,7 +427,7 @@ class HomeAssistantHTTP:
# Should be instance of aiohttp.web_exceptions._HTTPMove. # Should be instance of aiohttp.web_exceptions._HTTPMove.
raise redirect_exc(redirect_to) # type: ignore[arg-type,misc] raise redirect_exc(redirect_to) # type: ignore[arg-type,misc]
self.app[KEY_ALLOW_CONFIGRED_CORS]( self.app[KEY_ALLOW_CONFIGURED_CORS](
self.app.router.add_route("GET", url, redirect) self.app.router.add_route("GET", url, redirect)
) )
@ -461,7 +461,7 @@ class HomeAssistantHTTP:
) -> None: ) -> None:
"""Register a folders or files to serve as a static path.""" """Register a folders or files to serve as a static path."""
app = self.app app = self.app
allow_cors = app[KEY_ALLOW_CONFIGRED_CORS] allow_cors = app[KEY_ALLOW_CONFIGURED_CORS]
for config in configs: for config in configs:
if resource := resources[config.url_path]: if resource := resources[config.url_path]:
app.router.register_resource(resource) app.router.register_resource(resource)

View File

@ -19,7 +19,7 @@ from homeassistant.const import HTTP_HEADER_X_REQUESTED_WITH
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.http import ( from homeassistant.helpers.http import (
KEY_ALLOW_ALL_CORS, KEY_ALLOW_ALL_CORS,
KEY_ALLOW_CONFIGRED_CORS, KEY_ALLOW_CONFIGURED_CORS,
AllowCorsType, AllowCorsType,
) )
@ -82,6 +82,6 @@ def setup_cors(app: Application, origins: list[str]) -> None:
) )
if origins: if origins:
app[KEY_ALLOW_CONFIGRED_CORS] = cast(AllowCorsType, _allow_cors) app[KEY_ALLOW_CONFIGURED_CORS] = cast(AllowCorsType, _allow_cors)
else: else:
app[KEY_ALLOW_CONFIGRED_CORS] = lambda _: None app[KEY_ALLOW_CONFIGURED_CORS] = lambda _: None

View File

@ -33,7 +33,7 @@ _LOGGER = logging.getLogger(__name__)
type AllowCorsType = Callable[[AbstractRoute | AbstractResource], None] type AllowCorsType = Callable[[AbstractRoute | AbstractResource], None]
KEY_AUTHENTICATED: Final = "ha_authenticated" KEY_AUTHENTICATED: Final = "ha_authenticated"
KEY_ALLOW_ALL_CORS = AppKey[AllowCorsType]("allow_all_cors") KEY_ALLOW_ALL_CORS = AppKey[AllowCorsType]("allow_all_cors")
KEY_ALLOW_CONFIGRED_CORS = AppKey[AllowCorsType]("allow_configured_cors") KEY_ALLOW_CONFIGURED_CORS = AppKey[AllowCorsType]("allow_configured_cors")
KEY_HASS: AppKey[HomeAssistant] = AppKey("hass") KEY_HASS: AppKey[HomeAssistant] = AppKey("hass")
current_request: ContextVar[Request | None] = ContextVar( current_request: ContextVar[Request | None] = ContextVar(
@ -181,7 +181,7 @@ class HomeAssistantView:
if self.cors_allowed: if self.cors_allowed:
allow_cors = app.get(KEY_ALLOW_ALL_CORS) allow_cors = app.get(KEY_ALLOW_ALL_CORS)
else: else:
allow_cors = app.get(KEY_ALLOW_CONFIGRED_CORS) allow_cors = app.get(KEY_ALLOW_CONFIGURED_CORS)
if allow_cors: if allow_cors:
for route in routes: for route in routes:

View File

@ -20,7 +20,7 @@ import pytest
from homeassistant.components.http.cors import setup_cors from homeassistant.components.http.cors import setup_cors
from homeassistant.components.http.view import HomeAssistantView from homeassistant.components.http.view import HomeAssistantView
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.http import KEY_ALLOW_CONFIGRED_CORS from homeassistant.helpers.http import KEY_ALLOW_CONFIGURED_CORS
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import HTTP_HEADER_HA_AUTH from . import HTTP_HEADER_HA_AUTH
@ -62,7 +62,7 @@ def client(
"""Fixture to set up a web.Application.""" """Fixture to set up a web.Application."""
app = web.Application() app = web.Application()
setup_cors(app, [TRUSTED_ORIGIN]) setup_cors(app, [TRUSTED_ORIGIN])
app[KEY_ALLOW_CONFIGRED_CORS](app.router.add_get("/", mock_handler)) app[KEY_ALLOW_CONFIGURED_CORS](app.router.add_get("/", mock_handler))
return event_loop.run_until_complete(aiohttp_client(app)) return event_loop.run_until_complete(aiohttp_client(app))

View File

@ -8,7 +8,7 @@ import voluptuous as vol
from homeassistant.components.http import KEY_HASS, HomeAssistantView from homeassistant.components.http import KEY_HASS, HomeAssistantView
from homeassistant.components.http.data_validator import RequestDataValidator from homeassistant.components.http.data_validator import RequestDataValidator
from homeassistant.helpers.http import KEY_ALLOW_CONFIGRED_CORS from homeassistant.helpers.http import KEY_ALLOW_CONFIGURED_CORS
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
@ -17,7 +17,7 @@ async def get_client(aiohttp_client, validator):
"""Generate a client that hits a view decorated with validator.""" """Generate a client that hits a view decorated with validator."""
app = web.Application() app = web.Application()
app[KEY_HASS] = Mock(is_stopping=False) app[KEY_HASS] = Mock(is_stopping=False)
app[KEY_ALLOW_CONFIGRED_CORS] = lambda _: None app[KEY_ALLOW_CONFIGURED_CORS] = lambda _: None
class TestView(HomeAssistantView): class TestView(HomeAssistantView):
url = "/" url = "/"

View File

@ -10,7 +10,7 @@ import pytest
from homeassistant.components.http import StaticPathConfig from homeassistant.components.http import StaticPathConfig
from homeassistant.components.http.static import CachingStaticResource, _get_file_path from homeassistant.components.http.static import CachingStaticResource, _get_file_path
from homeassistant.core import EVENT_HOMEASSISTANT_START, HomeAssistant from homeassistant.core import EVENT_HOMEASSISTANT_START, HomeAssistant
from homeassistant.helpers.http import KEY_ALLOW_CONFIGRED_CORS from homeassistant.helpers.http import KEY_ALLOW_CONFIGURED_CORS
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
@ -51,7 +51,7 @@ async def test_static_path_blocks_anchors(
resource = CachingStaticResource(url, str(tmp_path)) resource = CachingStaticResource(url, str(tmp_path))
assert resource.canonical == canonical_url assert resource.canonical == canonical_url
app.router.register_resource(resource) app.router.register_resource(resource)
app[KEY_ALLOW_CONFIGRED_CORS](resource) app[KEY_ALLOW_CONFIGURED_CORS](resource)
resp = await mock_http_client.get(canonical_url, allow_redirects=False) resp = await mock_http_client.get(canonical_url, allow_redirects=False)
assert resp.status == 403 assert resp.status == 403