mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Fix image content-type validation case sensitivity (#125236)
This commit is contained in:
parent
8410c142ab
commit
691b2879bd
@ -70,7 +70,7 @@ class ImageContentTypeError(HomeAssistantError):
|
|||||||
|
|
||||||
def valid_image_content_type(content_type: str | None) -> str:
|
def valid_image_content_type(content_type: str | None) -> str:
|
||||||
"""Validate the assigned content type is one of an image."""
|
"""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
|
raise ImageContentTypeError
|
||||||
return content_type
|
return content_type
|
||||||
|
|
||||||
|
@ -52,6 +52,21 @@ class MockImageEntityInvalidContentType(image.ImageEntity):
|
|||||||
return b"Test"
|
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):
|
class MockURLImageEntity(image.ImageEntity):
|
||||||
"""Mock image entity."""
|
"""Mock image entity."""
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ from homeassistant.setup import async_setup_component
|
|||||||
|
|
||||||
from .conftest import (
|
from .conftest import (
|
||||||
MockImageEntity,
|
MockImageEntity,
|
||||||
|
MockImageEntityCapitalContentType,
|
||||||
MockImageEntityInvalidContentType,
|
MockImageEntityInvalidContentType,
|
||||||
MockImageNoStateEntity,
|
MockImageNoStateEntity,
|
||||||
MockImagePlatform,
|
MockImagePlatform,
|
||||||
@ -138,6 +139,32 @@ async def test_no_valid_content_type(
|
|||||||
assert resp.status == HTTPStatus.INTERNAL_SERVER_ERROR
|
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(
|
async def test_fetch_image_authenticated(
|
||||||
hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_image_platform: None
|
hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_image_platform: None
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user