mirror of
https://github.com/esphome/esphome.git
synced 2026-09-18 18:48:39 +00:00
add max length validation for device class strings
Defense-in-depth: validate device class strings don't exceed the 48-byte PROGMEM buffer limit (47 chars + null), matching the same pattern used for icon strings.
This commit is contained in:
@@ -23,6 +23,7 @@ from esphome.core.entity_helpers import (
|
||||
_setup_entity_impl,
|
||||
entity_duplicate_validator,
|
||||
get_base_entity_object_id,
|
||||
register_device_class,
|
||||
register_icon,
|
||||
setup_entity,
|
||||
)
|
||||
@@ -926,6 +927,22 @@ def test_register_icon_max_length() -> None:
|
||||
assert register_icon("") == 0
|
||||
|
||||
|
||||
def test_register_device_class_max_length() -> None:
|
||||
"""Test register_device_class rejects device classes exceeding 47 characters."""
|
||||
# 47 chars should succeed
|
||||
max_dc = "a" * 47
|
||||
idx = register_device_class(max_dc)
|
||||
assert idx > 0
|
||||
|
||||
# 48 chars should fail
|
||||
too_long = "a" * 48
|
||||
with pytest.raises(ValueError, match="Device class string too long"):
|
||||
register_device_class(too_long)
|
||||
|
||||
# Empty string returns 0
|
||||
assert register_device_class("") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_entity_with_entity_category(
|
||||
setup_test_environment: list[str],
|
||||
|
||||
Reference in New Issue
Block a user