Skip to content

Commit 78a2e15

Browse files
authored
Replace non-UTF-8 characters in log messages (#6227)
1 parent f3e1e0f commit 78a2e15

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

supervisor/utils/systemd_journal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def journal_logs_reader(
117117
continue
118118

119119
# strip \n for simple fields before decoding
120-
entries[field_name] = data[:-1].decode("utf-8")
120+
entries[field_name] = data[:-1].decode("utf-8", errors="replace")
121121

122122

123123
def _parse_boot_json(boot_json_bytes: bytes) -> tuple[int, str]:

tests/utils/test_systemd_journal.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,25 @@ async def test_parsing_boots_none():
275275
boots.append((index, boot_id))
276276

277277
assert boots == []
278+
279+
280+
async def test_parsing_non_utf8_message():
281+
"""Test that non-UTF-8 bytes in message are replaced with replacement character."""
282+
journal_logs, stream = _journal_logs_mock()
283+
# Include invalid UTF-8 sequence (0xff is not valid UTF-8)
284+
stream.feed_data(b"MESSAGE=Hello, \xff world!\n\n")
285+
_, line = await anext(journal_logs_reader(journal_logs))
286+
assert line == "Hello, \ufffd world!"
287+
288+
289+
async def test_parsing_non_utf8_in_binary_message():
290+
"""Test that non-UTF-8 bytes in binary format message are replaced."""
291+
journal_logs, stream = _journal_logs_mock()
292+
# Binary format with invalid UTF-8 sequence
293+
stream.feed_data(
294+
b"ID=1\n"
295+
b"MESSAGE\n\x0f\x00\x00\x00\x00\x00\x00\x00Hello, \xff world!\n"
296+
b"AFTER=after\n\n"
297+
)
298+
_, line = await anext(journal_logs_reader(journal_logs))
299+
assert line == "Hello, \ufffd world!"

0 commit comments

Comments
 (0)