Improve redaction for stream error messages (#120867)

This commit is contained in:
Allen Porter 2024-07-05 00:42:29 -07:00 committed by GitHub
parent 1c1e1a7bfa
commit 700675042b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 6 deletions

View file

@ -47,6 +47,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."""
@ -516,8 +524,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]
@ -592,7 +599,7 @@ def stream_worker(
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(
@ -617,7 +624,9 @@ def stream_worker(
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)

View file

@ -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