Skip to content

Commit f46a0ec

Browse files
committed
feat: filter out all media_image_url base64 data
The data was only filtered for entity_change events. Also filter entity_states responses.
1 parent 9cc9be5 commit f46a0ec

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

ucapi/api.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ async def _send_ws_response(
241241

242242
if websocket in self._clients:
243243
data_dump = json.dumps(data)
244-
_LOG.debug("[%s] ->: %s", websocket.remote_address, data_dump)
244+
if _LOG.isEnabledFor(logging.DEBUG):
245+
data_log = json.dumps(data) if filter_log_msg_data(data) else data_dump
246+
_LOG.debug("[%s] ->: %s", websocket.remote_address, data_log)
245247
await websocket.send(data_dump)
246248
else:
247249
_LOG.error("Error sending response: connection no longer established")
@@ -267,7 +269,7 @@ async def _broadcast_ws_event(
267269

268270
for websocket in self._clients:
269271
if _LOG.isEnabledFor(logging.DEBUG):
270-
_LOG.debug("[%s] ->: %s", websocket.remote_address, data_log)
272+
_LOG.debug("[%s] =>: %s", websocket.remote_address, data_log)
271273
try:
272274
await websocket.send(data_dump)
273275
except websockets.exceptions.WebSocketException:
@@ -470,7 +472,7 @@ async def _entity_command(
470472
self, websocket, req_id: int, msg_data: dict[str, Any] | None
471473
) -> None:
472474
if not msg_data:
473-
_LOG.warning("Ignoring _entity_command: called with empty msg_data")
475+
_LOG.warning("Ignoring entity command: called with empty msg_data")
474476
await self.acknowledge_command(
475477
websocket, req_id, uc.StatusCodes.BAD_REQUEST
476478
)
@@ -860,21 +862,41 @@ def filter_log_msg_data(data: dict[str, Any]) -> bool:
860862
861863
Attention: the dictionary is modified!
862864
863-
- Attributes are filtered in `data["msg_data"]["attributes"]`
865+
- Attributes are filtered in `data["msg_data"]`:
866+
- dict object: key `attributes`
867+
- list object: every list item `attributes`
864868
- Filtered attributes: `MEDIA_IMAGE_URL`
865869
866870
:param data: the message data dict
867871
:return: True if a field was filtered, False otherwise
868872
"""
869873
# filter out base64 encoded images in the media player's media_image_url attribute
870-
if (
871-
"msg_data" in data
872-
and "attributes" in data["msg_data"]
873-
and media_player.Attributes.MEDIA_IMAGE_URL in data["msg_data"]["attributes"]
874-
and data["msg_data"]["attributes"][
875-
media_player.Attributes.MEDIA_IMAGE_URL
876-
].startswith("data:")
877-
):
878-
data["msg_data"]["attributes"][media_player.Attributes.MEDIA_IMAGE_URL] = "***"
879-
return True
874+
if "msg_data" in data:
875+
if (
876+
"attributes" in data["msg_data"]
877+
and media_player.Attributes.MEDIA_IMAGE_URL
878+
in data["msg_data"]["attributes"]
879+
and data["msg_data"]["attributes"][
880+
media_player.Attributes.MEDIA_IMAGE_URL
881+
].startswith("data:")
882+
):
883+
data["msg_data"]["attributes"][
884+
media_player.Attributes.MEDIA_IMAGE_URL
885+
] = "data:***"
886+
return True
887+
888+
if isinstance(data["msg_data"], list):
889+
for item in data["msg_data"]:
890+
if (
891+
"attributes" in item
892+
and media_player.Attributes.MEDIA_IMAGE_URL in item["attributes"]
893+
and item["attributes"][
894+
media_player.Attributes.MEDIA_IMAGE_URL
895+
].startswith("data:")
896+
):
897+
item["attributes"][
898+
media_player.Attributes.MEDIA_IMAGE_URL
899+
] = "data:***"
900+
return True
901+
880902
return False

0 commit comments

Comments
 (0)