mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-16 18:43:02 +03:00
limit go2rtc exec and echo to frigate's ffmpeg and disable ngrok (#24983)
CI / ARM Build (push) Waiting to run
CI / Jetson Jetpack 6 (push) Waiting to run
CI / AMD64 Extra Build (push) Blocked by required conditions
CI / ARM Extra Build (push) Blocked by required conditions
CI / AMD64 Build (push) Waiting to run
CI / AMD64 Smoke Test (push) Blocked by required conditions
CI / Synaptics Build (push) Blocked by required conditions
CI / Assemble and push default build (push) Blocked by required conditions
CI / ARM Build (push) Waiting to run
CI / Jetson Jetpack 6 (push) Waiting to run
CI / AMD64 Extra Build (push) Blocked by required conditions
CI / ARM Extra Build (push) Blocked by required conditions
CI / AMD64 Build (push) Waiting to run
CI / AMD64 Smoke Test (push) Blocked by required conditions
CI / Synaptics Build (push) Blocked by required conditions
CI / Assemble and push default build (push) Blocked by required conditions
go2rtc merges its config files, and the HomeKit file and anything saved through go2rtc's unauthenticated config API load before Frigate's generated config. A planted exec: or echo: source, or an ngrok.command, ran whatever program it named, and as root under FRIGATE_ROOT_SERVICES=go2rtc. Unless GO2RTC_ALLOW_ARBITRARY_EXEC is set, the generated config now sets exec.allow_paths and echo.allow_paths to Frigate's ffmpeg and sets an empty ngrok.command, which override earlier files, and the config-save API rejects new exec and echo keys. The non-root docs note that a root go2rtc still isn't separated from the frigate user, since ffmpeg with arbitrary arguments can overwrite files as root.
This commit is contained in:
@@ -36,6 +36,8 @@ There are two reasons to use it:
|
|||||||
|
|
||||||
Try the device grants and `EXTRA_GROUPS` first. The `frigate` service runs the API and every ffmpeg process that decodes your camera streams, so listing it puts those back on root as well, not just your detectors.
|
Try the device grants and `EXTRA_GROUPS` first. The `frigate` service runs the API and every ffmpeg process that decodes your camera streams, so listing it puts those back on root as well, not just your detectors.
|
||||||
|
|
||||||
|
Listing `go2rtc` without `frigate` doesn't separate the two. go2rtc's API accepts unauthenticated config changes from inside the container, so the unprivileged `frigate` service can make a root go2rtc run ffmpeg with any arguments, which can overwrite any file as root.
|
||||||
|
|
||||||
A listed service also stops honoring a [custom ffmpeg or go2rtc build](/configuration/advanced/system#custom-dependencies) kept in `/config`, since that directory stays owned by the unprivileged user and a binary there would run as root. `FRIGATE_RUN_AS_ROOT=true` has no such restriction.
|
A listed service also stops honoring a [custom ffmpeg or go2rtc build](/configuration/advanced/system#custom-dependencies) kept in `/config`, since that directory stays owned by the unprivileged user and a binary there would run as root. `FRIGATE_RUN_AS_ROOT=true` has no such restriction.
|
||||||
|
|
||||||
Recordings and exports are owned by `PUID`/`PGID` as soon as they're written, even by a root service. Snapshots, thumbnails, and other files under `clips/` are corrected on each restart, so they can show as root-owned from the host until then. A listed service also keeps root's home directory, so library caches go to the container layer instead of `/config`. The same applies to the [detector runtimes](/frigate/network_requirements#detector-runtimes) Frigate installs at first start (Hailo, MemryX, AXEngine): a root `frigate` service installs them into `/root/.local`, which is lost when the container is recreated, and never loads a copy left behind in `/config/.local`.
|
Recordings and exports are owned by `PUID`/`PGID` as soon as they're written, even by a root service. Snapshots, thumbnails, and other files under `clips/` are corrected on each restart, so they can show as root-owned from the host until then. A listed service also keeps root's home directory, so library caches go to the container layer instead of `/config`. The same applies to the [detector runtimes](/frigate/network_requirements#detector-runtimes) Frigate installs at first start (Hailo, MemryX, AXEngine): a root `frigate` service installs them into `/root/.local`, which is lost when the container is recreated, and never loads a copy left behind in `/config/.local`.
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ For security reasons, the `echo:`, `expr:`, and `exec:` stream sources are disab
|
|||||||
|
|
||||||
go2rtc's `${VAR:default}` variable syntax is also disabled by default in stream sources, since its default can name one of those sources. Use Frigate's [`{FRIGATE_*}` substitution](./config.md#environment-variable-substitution) instead.
|
go2rtc's `${VAR:default}` variable syntax is also disabled by default in stream sources, since its default can name one of those sources. Use Frigate's [`{FRIGATE_*}` substitution](./config.md#environment-variable-substitution) instead.
|
||||||
|
|
||||||
If you attempt to use these sources in your configuration, the streams will be removed and an error message will be printed in the logs.
|
If you attempt to use these sources in your configuration, the streams will be removed and an error message will be printed in the logs. go2rtc also refuses to run anything but Frigate's ffmpeg from `exec:` and `echo:` sources added through its API or `/config/go2rtc_homekit.yml`.
|
||||||
|
|
||||||
To enable these sources, you must set the environment variable `GO2RTC_ALLOW_ARBITRARY_EXEC=true`. This can be done in your Docker Compose file or container environment, or for Home Assistant App users with the `go2rtc_allow_arbitrary_exec` option in the App's configuration. The `environment_vars` section of the Frigate config can't enable it:
|
To enable these sources, you must set the environment variable `GO2RTC_ALLOW_ARBITRARY_EXEC=true`. This can be done in your Docker Compose file or container environment, or for Home Assistant App users with the `go2rtc_allow_arbitrary_exec` option in the App's configuration. The `environment_vars` section of the Frigate config can't enable it:
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Tests for the go2rtc ffmpeg and ngrok options applied at startup."""
|
"""Tests for the go2rtc exec-related options applied at startup."""
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
@@ -24,6 +24,18 @@ class TestApplyGo2rtcFfmpegConfig(unittest.TestCase):
|
|||||||
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == []
|
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == []
|
||||||
assert config["ffmpeg"] == {"bin": FFMPEG_PATH}
|
assert config["ffmpeg"] == {"bin": FFMPEG_PATH}
|
||||||
|
|
||||||
|
def test_limits_exec_and_echo_to_ffmpeg(self):
|
||||||
|
config = {}
|
||||||
|
apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH)
|
||||||
|
assert config["exec"] == {"allow_paths": [FFMPEG_PATH]}
|
||||||
|
assert config["echo"] == {"allow_paths": [FFMPEG_PATH]}
|
||||||
|
|
||||||
|
def test_forces_exec_allowlist_override(self):
|
||||||
|
config = {"exec": {"allow_paths": ["/bin/bash"]}, "echo": {}}
|
||||||
|
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == ["exec", "echo"]
|
||||||
|
assert config["exec"] == {"allow_paths": [FFMPEG_PATH]}
|
||||||
|
assert config["echo"] == {"allow_paths": [FFMPEG_PATH]}
|
||||||
|
|
||||||
def test_forces_bin_override(self):
|
def test_forces_bin_override(self):
|
||||||
config = {"ffmpeg": {"bin": "/bin/sh", "global": "-c 'id' x"}}
|
config = {"ffmpeg": {"bin": "/bin/sh", "global": "-c 'id' x"}}
|
||||||
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == ["ffmpeg.bin"]
|
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == ["ffmpeg.bin"]
|
||||||
@@ -34,10 +46,10 @@ class TestApplyGo2rtcFfmpegConfig(unittest.TestCase):
|
|||||||
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == []
|
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == []
|
||||||
assert config["ffmpeg"]["global"] == "-vaapi_device /dev/dri/renderD128"
|
assert config["ffmpeg"]["global"] == "-vaapi_device /dev/dri/renderD128"
|
||||||
|
|
||||||
def test_drops_ngrok(self):
|
def test_disables_ngrok(self):
|
||||||
config = {"ngrok": {"command": "/bin/sh -c id"}}
|
config = {"ngrok": {"command": "/bin/sh -c id"}}
|
||||||
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == ["ngrok"]
|
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == ["ngrok"]
|
||||||
assert "ngrok" not in config
|
assert config["ngrok"] == {"command": ""}
|
||||||
|
|
||||||
def test_allows_bin_and_ngrok_when_override_set(self):
|
def test_allows_bin_and_ngrok_when_override_set(self):
|
||||||
config = {
|
config = {
|
||||||
@@ -48,6 +60,8 @@ class TestApplyGo2rtcFfmpegConfig(unittest.TestCase):
|
|||||||
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == []
|
assert apply_go2rtc_ffmpeg_config(config, FFMPEG_PATH) == []
|
||||||
assert config["ffmpeg"]["bin"] == "/usr/bin/ffmpeg"
|
assert config["ffmpeg"]["bin"] == "/usr/bin/ffmpeg"
|
||||||
assert config["ngrok"] == {"command": "ngrok tcp 8555"}
|
assert config["ngrok"] == {"command": "ngrok tcp 8555"}
|
||||||
|
assert "exec" not in config
|
||||||
|
assert "echo" not in config
|
||||||
|
|
||||||
|
|
||||||
class TestRestrictedGo2rtcVarExpansion(unittest.TestCase):
|
class TestRestrictedGo2rtcVarExpansion(unittest.TestCase):
|
||||||
|
|||||||
@@ -1235,14 +1235,15 @@ def find_restricted_go2rtc_stream_changes(
|
|||||||
|
|
||||||
|
|
||||||
# go2rtc runs `ffmpeg.bin` and `ngrok.command`, and every other `ffmpeg` key is a
|
# go2rtc runs `ffmpeg.bin` and `ngrok.command`, and every other `ffmpeg` key is a
|
||||||
# template of ffmpeg arguments that `#video=<name>` and friends can reference
|
# template of ffmpeg arguments that `#video=<name>` and friends can reference.
|
||||||
_GO2RTC_EXEC_SECTIONS = ("ffmpeg", "ngrok")
|
# `exec.allow_paths` and `echo.allow_paths` choose the binaries go2rtc may run.
|
||||||
|
_GO2RTC_EXEC_SECTIONS = ("ffmpeg", "ngrok", "exec", "echo")
|
||||||
|
|
||||||
|
|
||||||
def find_restricted_go2rtc_exec_changes(
|
def find_restricted_go2rtc_exec_changes(
|
||||||
old_go2rtc: dict[str, Any] | None, new_go2rtc: dict[str, Any] | None
|
old_go2rtc: dict[str, Any] | None, new_go2rtc: dict[str, Any] | None
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
"""Return the go2rtc ffmpeg and ngrok keys added or changed through the API.
|
"""Return the go2rtc exec-related keys added or changed through the API.
|
||||||
|
|
||||||
These are only accepted with arbitrary exec enabled. Keys already in the
|
These are only accepted with arbitrary exec enabled. Keys already in the
|
||||||
saved config are left alone, the same as stream sources.
|
saved config are left alone, the same as stream sources.
|
||||||
@@ -1274,10 +1275,11 @@ def find_restricted_go2rtc_exec_changes(
|
|||||||
def apply_go2rtc_ffmpeg_config(
|
def apply_go2rtc_ffmpeg_config(
|
||||||
go2rtc_config: dict[str, Any], ffmpeg_path: str
|
go2rtc_config: dict[str, Any], ffmpeg_path: str
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
"""Set go2rtc's ffmpeg binary for startup.
|
"""Set go2rtc's ffmpeg binary and exec allowlist for startup.
|
||||||
|
|
||||||
Unless arbitrary exec is enabled, `ffmpeg.bin` is forced to Frigate's ffmpeg
|
Unless arbitrary exec is enabled, `ffmpeg.bin` is forced to Frigate's ffmpeg,
|
||||||
and `ngrok` is removed. Templates in the saved config are kept.
|
`ngrok` is disabled, and go2rtc may only run Frigate's ffmpeg from `exec:` and
|
||||||
|
`echo:` sources. Templates in the saved config are kept.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The keys that were overridden or removed
|
The keys that were overridden or removed
|
||||||
@@ -1295,9 +1297,18 @@ def apply_go2rtc_ffmpeg_config(
|
|||||||
restricted.append("ffmpeg.bin")
|
restricted.append("ffmpeg.bin")
|
||||||
ffmpeg["bin"] = ffmpeg_path
|
ffmpeg["bin"] = ffmpeg_path
|
||||||
|
|
||||||
if "ngrok" in go2rtc_config and not allowed:
|
# go2rtc merges its config files and this one loads last, so setting these
|
||||||
restricted.append("ngrok")
|
# keys also overrides the HomeKit file and anything saved through its API
|
||||||
del go2rtc_config["ngrok"]
|
if not allowed:
|
||||||
|
forced = {
|
||||||
|
"ngrok": {"command": ""},
|
||||||
|
"exec": {"allow_paths": [ffmpeg_path]},
|
||||||
|
"echo": {"allow_paths": [ffmpeg_path]},
|
||||||
|
}
|
||||||
|
for key, value in forced.items():
|
||||||
|
if key in go2rtc_config and go2rtc_config[key] != value:
|
||||||
|
restricted.append(key)
|
||||||
|
go2rtc_config[key] = value
|
||||||
|
|
||||||
return restricted
|
return restricted
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user