1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Prevent overly large attributes from being stored in the database (#87105)

This commit is contained in:
J. Nick Koston
2023-02-02 11:49:33 -06:00
committed by GitHub
parent 5e81d28116
commit 389fc515a1
2 changed files with 42 additions and 5 deletions

View File

@@ -702,6 +702,33 @@ def test_saving_state_and_removing_entity(hass, hass_recorder):
assert states[2].state is None
def test_saving_state_with_oversized_attributes(hass_recorder, caplog):
"""Test saving states is limited to 16KiB of JSON encoded attributes."""
hass = hass_recorder()
massive_dict = {"a": "b" * 16384}
attributes = {"test_attr": 5, "test_attr_10": "nice"}
hass.states.set("switch.sane", "on", attributes)
hass.states.set("switch.too_big", "on", massive_dict)
wait_recording_done(hass)
states = []
with session_scope(hass=hass) as session:
for state, state_attributes in session.query(States, StateAttributes).outerjoin(
StateAttributes, States.attributes_id == StateAttributes.attributes_id
):
native_state = state.to_native()
native_state.attributes = state_attributes.to_native()
states.append(native_state)
assert "switch.too_big" in caplog.text
assert len(states) == 2
assert _state_with_context(hass, "switch.sane").as_dict() == states[0].as_dict()
assert states[1].state == "on"
assert states[1].entity_id == "switch.too_big"
assert states[1].attributes == {}
def test_recorder_setup_failure(hass):
"""Test some exceptions."""
recorder_helper.async_initialize_recorder(hass)