Fix image content-type validation case sensitivity (#125236)

pull/126494/head
Nicholas Pike 2024-09-23 05:33:29 -07:00 committed by GitHub
parent 8410c142ab
commit 691b2879bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 1 deletions

View File

@ -70,7 +70,7 @@ class ImageContentTypeError(HomeAssistantError):
def valid_image_content_type(content_type: str | None) -> str:
"""Validate the assigned content type is one of an image."""
if content_type is None or content_type.split("/", 1)[0] != "image":
if content_type is None or content_type.split("/", 1)[0].lower() != "image":
raise ImageContentTypeError
return content_type

View File

@ -52,6 +52,21 @@ class MockImageEntityInvalidContentType(image.ImageEntity):
return b"Test"
class MockImageEntityCapitalContentType(image.ImageEntity):
"""Mock image entity with correct content type, but capitalized."""
_attr_name = "Test"
async def async_added_to_hass(self):
"""Set the update time and assign and incorrect content type."""
self._attr_content_type = "Image/jpeg"
self._attr_image_last_updated = dt_util.utcnow()
async def async_image(self) -> bytes | None:
"""Return bytes of image."""
return b"Test"
class MockURLImageEntity(image.ImageEntity):
"""Mock image entity."""

View File

@ -18,6 +18,7 @@ from homeassistant.setup import async_setup_component
from .conftest import (
MockImageEntity,
MockImageEntityCapitalContentType,
MockImageEntityInvalidContentType,
MockImageNoStateEntity,
MockImagePlatform,
@ -138,6 +139,32 @@ async def test_no_valid_content_type(
assert resp.status == HTTPStatus.INTERNAL_SERVER_ERROR
async def test_valid_but_capitalized_content_type(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None:
"""Test invalid content type."""
mock_integration(hass, MockModule(domain="test"))
mock_platform(
hass, "test.image", MockImagePlatform([MockImageEntityCapitalContentType(hass)])
)
assert await async_setup_component(
hass, image.DOMAIN, {"image": {"platform": "test"}}
)
await hass.async_block_till_done()
client = await hass_client()
state = hass.states.get("image.test")
access_token = state.attributes["access_token"]
assert state.attributes == {
"access_token": access_token,
"entity_picture": f"/api/image_proxy/image.test?token={access_token}",
"friendly_name": "Test",
}
resp = await client.get(f"/api/image_proxy/image.test?token={access_token}")
assert resp.status == HTTPStatus.OK
async def test_fetch_image_authenticated(
hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_image_platform: None
) -> None: