diff --git a/homeassistant/components/generic/diagnostics.py b/homeassistant/components/generic/diagnostics.py index 00be287f053..39d6a81ad88 100644 --- a/homeassistant/components/generic/diagnostics.py +++ b/homeassistant/components/generic/diagnostics.py @@ -21,12 +21,12 @@ TO_REDACT = { # A very similar redact function is in components.sql. Possible to be made common. def redact_url(data: str) -> str: """Redact credentials from string url.""" - url_in = yarl.URL(data) + url = url_in = yarl.URL(data) if url_in.user: - url = url_in.with_user("****") + url = url.with_user("****") if url_in.password: url = url.with_password("****") - if url_in.path: + if url_in.path != "/": url = url.with_path("****") if url_in.query_string: url = url.with_query("****=****") diff --git a/tests/components/generic/test_diagnostics.py b/tests/components/generic/test_diagnostics.py index 2d4e4c536d8..d31503c11c8 100644 --- a/tests/components/generic/test_diagnostics.py +++ b/tests/components/generic/test_diagnostics.py @@ -1,5 +1,8 @@ """Test generic (IP camera) diagnostics.""" +import pytest + from homeassistant.components.diagnostics import REDACTED +from homeassistant.components.generic.diagnostics import redact_url from tests.components.diagnostics import get_diagnostics_for_config_entry @@ -22,3 +25,34 @@ async def test_entry_diagnostics(hass, hass_client, setup_entry): "content_type": "image/jpeg", }, } + + +@pytest.mark.parametrize( + ("url_in", "url_out_expected"), + [ + ( + "http://www.example.com", + "http://www.example.com", + ), + ( + "http://fred:letmein1@www.example.com/image.php?key=secret2", + "http://****:****@www.example.com/****?****=****", + ), + ( + "http://fred@www.example.com/image.php?key=secret2", + "http://****@www.example.com/****?****=****", + ), + ( + "http://fred@www.example.com/image.php", + "http://****@www.example.com/****", + ), + ( + "http://:letmein1@www.example.com", + "http://:****@www.example.com", + ), + ], +) +def test_redact_url(url_in, url_out_expected): + """Test url redaction.""" + url_out = redact_url(url_in) + assert url_out == url_out_expected