From 0b8675e78f94a0f29ec05b40dfdb7bac0dc7a727 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 7 Oct 2026 04:24:54 +0200 Subject: [PATCH] Quote the credentials in the Freebox camera stream URL (#184572) --- homeassistant/components/freebox/camera.py | 20 +++++++++++- tests/components/freebox/test_camera.py | 37 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/freebox/camera.py b/homeassistant/components/freebox/camera.py index cd54c8c99376..1e9471994035 100644 --- a/homeassistant/components/freebox/camera.py +++ b/homeassistant/components/freebox/camera.py @@ -1,6 +1,7 @@ """Support for Freebox cameras.""" from typing import Any, override +from urllib.parse import quote from aiohttp import web from haffmpeg.camera import CameraMjpeg @@ -62,6 +63,23 @@ def add_entities( async_add_entities(new_tracked, True) +def _quote_credentials(url: str) -> str: + """Quote the credentials in a stream URL. + + The Freebox generates the camera password, which can contain characters like + a slash that break parsing the URL if they are not percent-encoded. + """ + scheme, separator, rest = url.partition("://") + credentials, at_sign, location = rest.rpartition("@") + if not separator or not at_sign: + return url + + user, colon, password = credentials.partition(":") + return ( + f"{scheme}://{quote(user, safe='')}{colon}{quote(password, safe='')}@{location}" + ) + + class FreeboxCamera(FreeboxHomeEntity, Camera): """Representation of a Freebox camera.""" @@ -87,7 +105,7 @@ class FreeboxCamera(FreeboxHomeEntity, Camera): @override async def stream_source(self) -> str: """Return the stream source.""" - return self._input.split(" ")[-1] + return _quote_credentials(self._input.split(" ")[-1]) @override async def async_camera_image( diff --git a/tests/components/freebox/test_camera.py b/tests/components/freebox/test_camera.py index fc72b8ce5db9..b621f3f7f7d4 100644 --- a/tests/components/freebox/test_camera.py +++ b/tests/components/freebox/test_camera.py @@ -4,11 +4,13 @@ from copy import deepcopy from unittest.mock import Mock from freezegun.api import FrozenDateTimeFactory +import pytest from homeassistant.components.camera import ( DOMAIN as CAMERA_DOMAIN, SERVICE_DISABLE_MOTION, SERVICE_ENABLE_MOTION, + async_get_stream_source, ) from homeassistant.components.freebox import SCAN_INTERVAL from homeassistant.components.freebox.const import DOMAIN @@ -115,3 +117,38 @@ async def test_motion_detection_toggle( blocking=True, ) router().home.set_home_endpoint_value.assert_called_with(15, 0, {"value": True}) + + +@pytest.mark.parametrize( + ("stream", "expected_source"), + [ + pytest.param( + "http://freeboxcam:AaaA/1A1@192.168.1.10/img/stream.m3u8", + "http://freeboxcam:AaaA%2F1A1@192.168.1.10/img/stream.m3u8", + id="slash_in_password", + ), + pytest.param( + "http://freeboxcam:Aa%1A@192.168.1.10/img/stream.m3u8", + "http://freeboxcam:Aa%251A@192.168.1.10/img/stream.m3u8", + id="percent_in_password", + ), + pytest.param( + "http://freeboxcam:AaaA1A1@192.168.1.10/img/stream.m3u8", + "http://freeboxcam:AaaA1A1@192.168.1.10/img/stream.m3u8", + id="plain_password", + ), + ], +) +async def test_stream_source_quotes_credentials( + hass: HomeAssistant, router: Mock, stream: str, expected_source: str +) -> None: + """Test the stream source has the credentials generated by the Freebox quoted.""" + nodes = deepcopy(DATA_HOME_GET_NODES) + for node in nodes: + if node["id"] == 15: # Caméra I from fixture + node["props"]["Stream"] = stream + break + router().home.get_home_nodes.return_value = nodes + await setup_platform(hass, CAMERA_DOMAIN) + + assert await async_get_stream_source(hass, "camera.camera_i") == expected_source