Skip to content
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

Add regex validation for the entity id filter #89991

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion homeassistant/components/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
sqlalchemy_filter_from_include_exclude_conf,
)
from homeassistant.components.recorder.util import session_scope
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, valid_entity_id
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entityfilter import (
INCLUDE_EXCLUDE_BASE_FILTER_SCHEMA,
Expand Down Expand Up @@ -120,6 +120,11 @@ async def get(
entity_ids = None
if entity_ids_str:
entity_ids = entity_ids_str.lower().split(",")
for entity_id in entity_ids:
if not valid_entity_id(entity_id):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if it's in the state machine first before calling this check as it can get expensive if the entity id list is large

Also this needs a test.

Thanks

flip-dots marked this conversation as resolved.
Show resolved Hide resolved
return self.json_message(
"Invalid filter_entity_id", HTTPStatus.BAD_REQUEST
)
include_start_time_state = "skip_initial_state" not in request.query
significant_changes_only = (
request.query.get("significant_changes_only", "1") != "0"
Expand Down
52 changes: 52 additions & 0 deletions tests/components/history/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,3 +911,55 @@ async def test_entity_ids_limit_via_api_with_skip_initial_state(
assert len(response_json) == 2
assert response_json[0][0]["entity_id"] == "light.kitchen"
assert response_json[1][0]["entity_id"] == "light.cow"


async def test_history_with_invalid_entity_ids(
recorder_mock: Recorder, hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None:
"""Test sending valid and invalid entity_ids to the API."""
await async_setup_component(
hass,
"history",
{"history": {}},
)
hass.states.async_set("light.kitchen", "on")
hass.states.async_set("light.cow", "on")

await async_wait_recording_done(hass)
now = dt_util.utcnow().isoformat()
client = await hass_client()

response = await client.get(
f"/api/history/period/{now}?filter_entity_id=light.kitchen,light.cow",
)
assert response.status == HTTPStatus.OK

response = await client.get(
f"/api/history/period/{now}?filter_entity_id=light.kit!chen",
)
assert response.status == HTTPStatus.BAD_REQUEST

response = await client.get(
f"/api/history/period/{now}?filter_entity_id=light.kitchen,li-ght.cow",
)
assert response.status == HTTPStatus.BAD_REQUEST

response = await client.get(
f"/api/history/period/{now}?filter_entity_id=light.kitchen,light.cow&",
)
assert response.status == HTTPStatus.OK

response = await client.get(
f"/api/history/period/{now}?filter_entity_id=lig+ht.kitchen,light.cow",
)
assert response.status == HTTPStatus.BAD_REQUEST

response = await client.get(
f"/api/history/period/{now}?filter_entity_id=light.kitchenlight.cow",
)
assert response.status == HTTPStatus.BAD_REQUEST

response = await client.get(
f"/api/history/period/{now}?filter_entity_id=cow",
)
assert response.status == HTTPStatus.BAD_REQUEST