Skip to content

Commit

Permalink
refactor: filter out base64 encoded image fields in logs
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
zehnm committed Mar 13, 2024
1 parent 2cd7cdd commit 8b402b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

_Changes in the next release_

### Changed
- Filter out base64 encoded media-player image fields in log messages ([#17](https://github.com/unfoldedcircle/integration-python-library/issues/17)).

---

## v0.1.6 - 2024-03-04
Expand Down
41 changes: 37 additions & 4 deletions ucapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf

import ucapi.api_definitions as uc
from ucapi import media_player
from ucapi.entities import Entities

_LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -258,10 +259,14 @@ async def _broadcast_ws_event(
:param category: event category
"""
data = {"kind": "event", "msg": msg, "msg_data": msg_data, "cat": category}
data_dump = json.dumps(data)
# filter fields
if _LOG.isEnabledFor(logging.DEBUG):
data_log = json.dumps(data) if filter_log_msg_data(data) else data_dump

for websocket in self._clients:
data_dump = json.dumps(data)
_LOG.debug("[%s] ->: %s", websocket.remote_address, data_dump)
if _LOG.isEnabledFor(logging.DEBUG):
_LOG.debug("[%s] ->: %s", websocket.remote_address, data_log)
try:
await websocket.send(data_dump)
except websockets.exceptions.WebSocketException:
Expand All @@ -282,10 +287,12 @@ async def _send_ws_event(
websockets.ConnectionClosed: When the connection is closed.
"""
data = {"kind": "event", "msg": msg, "msg_data": msg_data, "cat": category}
data_dump = json.dumps(data)

if websocket in self._clients:
data_dump = json.dumps(data)
_LOG.debug("[%s] ->: %s", websocket.remote_address, data_dump)
if _LOG.isEnabledFor(logging.DEBUG):
data_log = json.dumps(data) if filter_log_msg_data(data) else data_dump
_LOG.debug("[%s] ->: %s", websocket.remote_address, data_log)
await websocket.send(data_dump)
else:
_LOG.error("Error sending event: connection no longer established")
Expand Down Expand Up @@ -843,3 +850,29 @@ def local_hostname() -> str:
os.getenv("UC_MDNS_LOCAL_HOSTNAME")
or f"{socket.gethostname().split('.', 1)[0]}.local."
)


def filter_log_msg_data(data: dict[str, Any]) -> bool:
"""
Filter attribute fields to exclude for log messages in the given msg data dict.
Attention: the dictionary is modified!
- Attributes are filtered in `data["msg_data"]["attributes"]`
- Filtered attributes: `MEDIA_IMAGE_URL`
:param data: the message data dict
:return: True if a field was filtered, False otherwise
"""
# filter out base64 encoded images in the media player's media_image_url attribute
if (
"msg_data" in data
and "attributes" in data["msg_data"]
and media_player.Attributes.MEDIA_IMAGE_URL in data["msg_data"]["attributes"]
and data["msg_data"]["attributes"][
media_player.Attributes.MEDIA_IMAGE_URL
].startswith("data:")
):
data["msg_data"]["attributes"][media_player.Attributes.MEDIA_IMAGE_URL] = "***"
return True
return False

0 comments on commit 8b402b7

Please sign in to comment.