diff --git a/homeassistant/components/rest_command/services.py b/homeassistant/components/rest_command/services.py index 5ad9bd15c0c2..a70c77483e6c 100644 --- a/homeassistant/components/rest_command/services.py +++ b/homeassistant/components/rest_command/services.py @@ -108,11 +108,12 @@ def async_register_rest_command( if content_type: headers[hdrs.CONTENT_TYPE] = content_type + # Header values often hold tokens, so only their names are logged _LOGGER.debug( "Calling %s %s with headers: %s and payload: %s", method, request_url, - headers, + list(headers), payload, ) diff --git a/tests/components/rest_command/test_init.py b/tests/components/rest_command/test_init.py index 74f9f2785324..0ae7d96376a0 100644 --- a/tests/components/rest_command/test_init.py +++ b/tests/components/rest_command/test_init.py @@ -2,6 +2,7 @@ import base64 from http import HTTPStatus +import logging from unittest.mock import patch import aiohttp @@ -223,6 +224,31 @@ async def test_rest_command_configured_authorization_header_wins( assert CIMultiDict(headers).getall("Authorization") == ["Bearer configured"] +async def test_rest_command_header_values_not_logged( + hass: HomeAssistant, + setup_component: ComponentSetup, + aioclient_mock: AiohttpClientMocker, + caplog: pytest.LogCaptureFixture, +) -> None: + """Call a rest command and check its header values stay out of the log.""" + caplog.set_level(logging.DEBUG, logger="homeassistant.components.rest_command") + await setup_component( + { + "header_test": { + "url": TEST_URL, + "headers": {"Authorization": "Bearer secret"}, + } + } + ) + aioclient_mock.get(TEST_URL, content=b"success") + caplog.clear() + + await hass.services.async_call(DOMAIN, "header_test", {}, blocking=True) + + assert "Authorization" in caplog.text + assert "Bearer secret" not in caplog.text + + @pytest.mark.usefixtures("aioclient_mock") async def test_rest_command_digest_auth( hass: HomeAssistant,