-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Enforce per-entity permissions in calendar HTTP and WS APIs #169235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,10 @@ | |
| from dateutil.rrule import rrulestr | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.auth.models import User | ||
| from homeassistant.auth.permissions.const import POLICY_CONTROL, POLICY_READ | ||
| from homeassistant.components import frontend, http, websocket_api | ||
| from homeassistant.components.http import KEY_HASS_USER | ||
| from homeassistant.components.websocket_api import ( | ||
| ERR_INVALID_FORMAT, | ||
| ERR_NOT_FOUND, | ||
|
|
@@ -32,7 +35,7 @@ | |
| SupportsResponse, | ||
| callback, | ||
| ) | ||
| from homeassistant.exceptions import HomeAssistantError | ||
| from homeassistant.exceptions import HomeAssistantError, Unauthorized | ||
| from homeassistant.helpers import config_validation as cv, entity_registry as er | ||
| from homeassistant.helpers.debounce import Debouncer | ||
| from homeassistant.helpers.entity import Entity, EntityDescription | ||
|
|
@@ -786,6 +789,10 @@ def __init__(self, component: EntityComponent[CalendarEntity]) -> None: | |
|
|
||
| async def get(self, request: web.Request, entity_id: str) -> web.Response: | ||
| """Return calendar events.""" | ||
| user: User = request[KEY_HASS_USER] | ||
| if not user.permissions.check_entity(entity_id, POLICY_READ): | ||
| raise Unauthorized(entity_id=entity_id) | ||
|
|
||
| if not (entity := self.component.get_entity(entity_id)) or not isinstance( | ||
| entity, CalendarEntity | ||
| ): | ||
|
|
@@ -837,10 +844,14 @@ def __init__(self, component: EntityComponent[CalendarEntity]) -> None: | |
|
|
||
| async def get(self, request: web.Request) -> web.Response: | ||
| """Retrieve calendar list.""" | ||
| user: User = request[KEY_HASS_USER] | ||
| hass = request.app[http.KEY_HASS] | ||
| entity_perm = user.permissions.check_entity | ||
| calendar_list: list[dict[str, str]] = [] | ||
|
|
||
| for entity in self.component.entities: | ||
| if not entity_perm(entity.entity_id, POLICY_READ): | ||
| continue | ||
| state = hass.states.get(entity.entity_id) | ||
| assert state | ||
| calendar_list.append({"name": state.name, "entity_id": entity.entity_id}) | ||
|
|
@@ -860,6 +871,9 @@ async def handle_calendar_event_create( | |
| hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
| ) -> None: | ||
| """Handle creation of a calendar event.""" | ||
| if not connection.user.permissions.check_entity(msg["entity_id"], POLICY_CONTROL): | ||
| raise Unauthorized(entity_id=msg["entity_id"]) | ||
|
|
||
| if not (entity := hass.data[DATA_COMPONENT].get_entity(msg["entity_id"])): | ||
| connection.send_error(msg["id"], ERR_NOT_FOUND, "Entity not found") | ||
| return | ||
|
|
@@ -899,6 +913,8 @@ async def handle_calendar_event_delete( | |
| hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
| ) -> None: | ||
| """Handle delete of a calendar event.""" | ||
| if not connection.user.permissions.check_entity(msg["entity_id"], POLICY_CONTROL): | ||
| raise Unauthorized(entity_id=msg["entity_id"]) | ||
|
|
||
| if not (entity := hass.data[DATA_COMPONENT].get_entity(msg["entity_id"])): | ||
| connection.send_error(msg["id"], ERR_NOT_FOUND, "Entity not found") | ||
|
|
@@ -945,6 +961,9 @@ async def handle_calendar_event_update( | |
| hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
| ) -> None: | ||
| """Handle creation of a calendar event.""" | ||
| if not connection.user.permissions.check_entity(msg["entity_id"], POLICY_CONTROL): | ||
| raise Unauthorized(entity_id=msg["entity_id"]) | ||
|
|
||
| if not (entity := hass.data[DATA_COMPONENT].get_entity(msg["entity_id"])): | ||
| connection.send_error(msg["id"], ERR_NOT_FOUND, "Entity not found") | ||
| return | ||
|
|
@@ -989,6 +1008,9 @@ async def handle_calendar_event_subscribe( | |
| """Subscribe to calendar event updates.""" | ||
| entity_id: str = msg["entity_id"] | ||
|
|
||
| if not connection.user.permissions.check_entity(entity_id, POLICY_READ): | ||
| raise Unauthorized(entity_id=entity_id) | ||
|
|
||
|
Comment on lines
1008
to
+1013
|
||
| if not (entity := hass.data[DATA_COMPONENT].get_entity(entity_id)): | ||
| connection.send_error( | ||
| msg["id"], | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.