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.
This commit is contained in:
Josh Hawkins
2026-11-24 09:33:23 -06:00
parent bcd64d96a6
commit 87b7412dee
2 changed files with 66 additions and 29 deletions
+21 -29
View File
@@ -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:
@@ -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"