From 87b7412dee52581f98ec04123116eb0fc320e0a7 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:33:23 -0600 Subject: [PATCH] match masked camera paths with the running config's environment_vars Matching a masked path under both the startup and saved environment_vars could pick the wrong input when a saved but unapplied change made two hosts collide, so a role swap saved one input's path for both. Saved paths now resolve only with the block the running config was built from, which is what /config showed. --- frigate/api/app.py | 50 ++++++++----------- frigate/test/http_api/test_http_config_set.py | 45 +++++++++++++++++ 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/frigate/api/app.py b/frigate/api/app.py index 7816b45807..8cb69762a4 100644 --- a/frigate/api/app.py +++ b/frigate/api/app.py @@ -791,16 +791,20 @@ def config_save(save_option: str, body: Any = Body(media_type="text/plain")): ) -def _restore_masked_camera_paths(config_data: dict, saved: dict) -> str | None: +def _restore_masked_camera_paths( + config_data: dict, saved: dict, shown_env_vars: dict[str, str] +) -> str | None: """Substitute incoming masked camera paths with the saved ones. The /config response masks ffmpeg input credentials, so the settings UI sends the masked path back when sibling fields (e.g. hwaccel_args) are edited. Each masked path is matched to the saved input that masks to the same value, with roles breaking a tie, so deleting or reordering inputs - keeps each path's own credentials. The saved path is restored as written, - keeping any {FRIGATE_*} placeholder. Mutates `config_data` in place and - returns an error message when a masked path has no single match. + keeps each path's own credentials. Saved paths resolve with + `shown_env_vars`, the block the running config and /config used. The saved + path is restored as written, keeping any {FRIGATE_*} placeholder. Mutates + `config_data` in place and returns an error message when a masked path has + no single match. """ cameras = config_data.get("cameras") if not isinstance(cameras, dict): @@ -810,8 +814,6 @@ def _restore_masked_camera_paths(config_data: dict, saved: dict) -> str | None: if not isinstance(saved_cameras, dict): saved_cameras = {} - saved_env_vars = saved.get("environment_vars") - for camera_name, camera_data in cameras.items(): if not isinstance(camera_data, dict): continue @@ -838,11 +840,13 @@ def _restore_masked_camera_paths(config_data: dict, saved: dict) -> str | None: path = input_obj.get("path") if not isinstance(path, str) or not is_masked_camera_path(path): continue - candidates = [ - inp - for inp in saved_inputs - if path in _masked_saved_paths(inp["path"], saved_env_vars) - ] + with candidate_env_vars(shown_env_vars): + candidates = [ + inp + for inp in saved_inputs + if clean_camera_user_pass(_resolve_frigate_vars(inp["path"])) + == path + ] if len({inp["path"] for inp in candidates}) > 1: candidates = [ inp @@ -868,21 +872,6 @@ def _resolve_frigate_vars(value: str) -> str: return value -def _masked_saved_paths(path: str, saved_env_vars: Any) -> set[str]: - """Mask a saved camera path the ways /config can show it. - - /config resolves placeholders with the installed environment_vars until a - no-restart update swaps in a config parsed with the saved block. - """ - with candidate_env_vars(saved_env_vars): - saved_resolved = _resolve_frigate_vars(path) - - return { - clean_camera_user_pass(_resolve_frigate_vars(path)), - clean_camera_user_pass(saved_resolved), - } - - def _saved_config_from_memory(config: FrigateConfig) -> dict[str, Any]: """Shape the in-memory camera inputs like the saved YAML.""" return { @@ -901,14 +890,14 @@ def _saved_config_from_memory(config: FrigateConfig) -> dict[str, Any]: def _config_data_to_updates( - config_data: dict, saved: dict[str, Any] + config_data: dict, saved: dict[str, Any], running: FrigateConfig ) -> tuple[dict[str, Any], str | None]: """Flatten a /config/set payload after restoring masked credentials. `saved` is the config the masked values were taken from. Returns the flat updates, or an error message when a masked value can't be restored. """ - error = _restore_masked_camera_paths(config_data, saved) + error = _restore_masked_camera_paths(config_data, saved, running.environment_vars) if error: return {}, error @@ -962,7 +951,9 @@ def _config_set_in_memory(request: Request, body: AppConfigSetBody) -> JSONRespo updates = {} if body.config_data: updates, error = _config_data_to_updates( - body.config_data, _saved_config_from_memory(request.app.frigate_config) + body.config_data, + _saved_config_from_memory(request.app.frigate_config), + request.app.frigate_config, ) if error: return JSONResponse( @@ -1143,6 +1134,7 @@ def config_set(request: Request, body: AppConfigSetBody): updates, error = _config_data_to_updates( body.config_data, saved_config if isinstance(saved_config, dict) else {}, + request.app.frigate_config, ) if error: diff --git a/frigate/test/http_api/test_http_config_set.py b/frigate/test/http_api/test_http_config_set.py index 211c4fa012..8b1db7f04e 100644 --- a/frigate/test/http_api/test_http_config_set.py +++ b/frigate/test/http_api/test_http_config_set.py @@ -669,6 +669,51 @@ class TestConfigSetWildcardPropagation(BaseTestHttp): finally: os.unlink(config_path) + @patch.dict( + "frigate.util.env._CONFIG_ENV_VARS", + {"FRIGATE_HOST_A": "10.0.0.1", "FRIGATE_HOST_B": "10.0.0.2"}, + ) + @patch.dict( + "frigate.util.env.FRIGATE_ENV_VARS", + {"FRIGATE_HOST_A": "10.0.0.1", "FRIGATE_HOST_B": "10.0.0.2"}, + ) + @patch("frigate.api.app.find_config_file") + def test_masked_paths_match_running_env_vars(self, mock_find_config): + """A saved but not yet applied environment_vars change must not make + one input's masked path match another's.""" + path_a = "rtsp://admin:pw@{FRIGATE_HOST_A}:554/live" + path_b = "rtsp://admin:pw@{FRIGATE_HOST_B}:554/live" + self._set_inputs([(path_a, "detect"), (path_b, "record")]) + self.minimal_config["environment_vars"] = { + "FRIGATE_HOST_A": "10.0.0.1", + "FRIGATE_HOST_B": "10.0.0.2", + } + app, _ = self._create_app_with_publisher() + + self.minimal_config["environment_vars"]["FRIGATE_HOST_A"] = "10.0.0.2" + config_path = self._write_config_file() + mock_find_config.return_value = config_path + + try: + with AuthTestClient(app) as client: + camera = client.get("/config").json()["cameras"]["front_door"] + inputs = camera["ffmpeg"]["inputs"] + inputs[0]["roles"], inputs[1]["roles"] = ["record"], ["detect"] + + resp = self._save( + client, {"cameras": {"front_door": {"ffmpeg": {"inputs": inputs}}}} + ) + self.assertEqual(resp.status_code, 200, resp.text) + + _, saved = self._read_config_file(config_path) + saved_inputs = saved["cameras"]["front_door"]["ffmpeg"]["inputs"] + self.assertEqual( + [(inp["path"], inp["roles"]) for inp in saved_inputs], + [(path_a, ["record"]), (path_b, ["detect"])], + ) + finally: + os.unlink(config_path) + @patch("frigate.api.app.find_config_file") def test_over_masked_path_round_trips(self, mock_find_config): path = "rtsp://10.0.0.1:554/live?x=a@b"