Skip to content

Commit

Permalink
Improve redaction for stream error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter committed Jun 30, 2024
1 parent 419d89f commit 9304f93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
17 changes: 13 additions & 4 deletions homeassistant/components/stream/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class StreamWorkerError(Exception):
"""An exception thrown while processing a stream."""


def redact_av_error_string(err: av.AVError) -> str:
"""Return an error string with credentials redacted from the url."""
parts = [str(err.type), err.strerror]
if err.filename is not None:
parts.append(redact_credentials(err.filename))
return ", ".join(parts)


class StreamEndedError(StreamWorkerError):
"""Raised when the stream is complete, exposed for facilitating testing."""

Expand Down Expand Up @@ -517,8 +525,7 @@ def stream_worker(
container = av.open(source, options=pyav_options, timeout=SOURCE_TIMEOUT)
except av.AVError as err:
raise StreamWorkerError(
f"Error opening stream ({err.type}, {err.strerror})"
f" {redact_credentials(str(source))}"
f"Error opening stream ({redact_av_error_string(err)})"
) from err
try:
video_stream = container.streams.video[0]
Expand Down Expand Up @@ -593,7 +600,7 @@ def is_video(packet: av.Packet) -> Any:
except av.AVError as ex:
container.close()
raise StreamWorkerError(
f"Error demuxing stream while finding first packet: {ex!s}"
f"Error demuxing stream while finding first packet ({redact_av_error_string(ex)})"
) from ex

muxer = StreamMuxer(
Expand All @@ -618,7 +625,9 @@ def is_video(packet: av.Packet) -> Any:
except StopIteration as ex:
raise StreamEndedError("Stream ended; no additional packets") from ex
except av.AVError as ex:
raise StreamWorkerError(f"Error demuxing stream: {ex!s}") from ex
raise StreamWorkerError(
f"Error demuxing stream ({redact_av_error_string(ex)})"
) from ex

muxer.mux_packet(packet)

Expand Down
7 changes: 5 additions & 2 deletions tests/components/stream/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,12 +772,15 @@ async def test_worker_log(

with patch("av.open") as av_open:
# pylint: disable-next=c-extension-no-member
av_open.side_effect = av.error.InvalidDataError(-2, "error")
av_open.side_effect = av.error.InvalidDataError(
code=-2, message="Invalid data", filename=stream_url
)
with pytest.raises(StreamWorkerError) as err:
run_worker(hass, stream, stream_url)
await hass.async_block_till_done()
assert (
str(err.value) == f"Error opening stream (ERRORTYPE_-2, error) {redacted_url}"
str(err.value)
== f"Error opening stream (ERRORTYPE_-2, Invalid data, {redacted_url})"
)
assert stream_url not in caplog.text

Expand Down

0 comments on commit 9304f93

Please sign in to comment.