mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-12-24 20:35:55 +00:00
Replace non-UTF-8 characters in log messages (#6227)
This commit is contained in:
@@ -117,7 +117,7 @@ async def journal_logs_reader(
|
||||
continue
|
||||
|
||||
# strip \n for simple fields before decoding
|
||||
entries[field_name] = data[:-1].decode("utf-8")
|
||||
entries[field_name] = data[:-1].decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def _parse_boot_json(boot_json_bytes: bytes) -> tuple[int, str]:
|
||||
|
||||
@@ -275,3 +275,25 @@ async def test_parsing_boots_none():
|
||||
boots.append((index, boot_id))
|
||||
|
||||
assert boots == []
|
||||
|
||||
|
||||
async def test_parsing_non_utf8_message():
|
||||
"""Test that non-UTF-8 bytes in message are replaced with replacement character."""
|
||||
journal_logs, stream = _journal_logs_mock()
|
||||
# Include invalid UTF-8 sequence (0xff is not valid UTF-8)
|
||||
stream.feed_data(b"MESSAGE=Hello, \xff world!\n\n")
|
||||
_, line = await anext(journal_logs_reader(journal_logs))
|
||||
assert line == "Hello, \ufffd world!"
|
||||
|
||||
|
||||
async def test_parsing_non_utf8_in_binary_message():
|
||||
"""Test that non-UTF-8 bytes in binary format message are replaced."""
|
||||
journal_logs, stream = _journal_logs_mock()
|
||||
# Binary format with invalid UTF-8 sequence
|
||||
stream.feed_data(
|
||||
b"ID=1\n"
|
||||
b"MESSAGE\n\x0f\x00\x00\x00\x00\x00\x00\x00Hello, \xff world!\n"
|
||||
b"AFTER=after\n\n"
|
||||
)
|
||||
_, line = await anext(journal_logs_reader(journal_logs))
|
||||
assert line == "Hello, \ufffd world!"
|
||||
|
||||
Reference in New Issue
Block a user