mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-20 04:23:01 +03:00
allow export thumbnails for camera-restricted roles
Export thumbnails under /clips/export/ weren't classified by media auth, so roles with a camera allow-list got a 403 on every thumbnail. The thumbnail now resolves to its export's camera through a thumb_path lookup, and the camera access check applies as usual.
This commit is contained in:
@@ -17,10 +17,10 @@ import re
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
|
|
||||||
from peewee import DoesNotExist
|
from peewee import DoesNotExist, Field
|
||||||
|
|
||||||
from frigate.config import FrigateConfig
|
from frigate.config import FrigateConfig
|
||||||
from frigate.const import EXPORT_DIR
|
from frigate.const import CLIPS_DIR, EXPORT_DIR
|
||||||
from frigate.models import Export, User
|
from frigate.models import Export, User
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -151,6 +151,7 @@ def _resolve_clip(
|
|||||||
# /clips/thumbs/{cam}/... → camera
|
# /clips/thumbs/{cam}/... → camera
|
||||||
# /clips/previews/{cam}/... → camera
|
# /clips/previews/{cam}/... → camera
|
||||||
# /clips/review/thumb-{cam}-{review_id}.webp → camera (parsed)
|
# /clips/review/thumb-{cam}-{review_id}.webp → camera (parsed)
|
||||||
|
# /clips/export/{export_id}.{ext} → camera (DB lookup)
|
||||||
# /clips/faces/... → admin-only
|
# /clips/faces/... → admin-only
|
||||||
# /clips/genai-requests/... → admin-only
|
# /clips/genai-requests/... → admin-only
|
||||||
# /clips/preview_restart_cache/... → admin-only
|
# /clips/preview_restart_cache/... → admin-only
|
||||||
@@ -175,6 +176,11 @@ def _resolve_clip(
|
|||||||
return MediaAuthResolution.CAMERA, camera
|
return MediaAuthResolution.CAMERA, camera
|
||||||
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
||||||
|
|
||||||
|
if second == "export" and len(parts) == 3:
|
||||||
|
return _export_camera(
|
||||||
|
Export.thumb_path, os.path.join(CLIPS_DIR, "export", parts[2])
|
||||||
|
)
|
||||||
|
|
||||||
if second in ("faces", "genai-requests", "preview_restart_cache"):
|
if second in ("faces", "genai-requests", "preview_restart_cache"):
|
||||||
return MediaAuthResolution.ADMIN_ONLY, None
|
return MediaAuthResolution.ADMIN_ONLY, None
|
||||||
|
|
||||||
@@ -234,15 +240,19 @@ def _resolve_export(
|
|||||||
if len(parts) != 2:
|
if len(parts) != 2:
|
||||||
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
||||||
|
|
||||||
filename = parts[1]
|
return _export_camera(Export.video_path, os.path.join(EXPORT_DIR, parts[1]))
|
||||||
full_path = os.path.join(EXPORT_DIR, filename)
|
|
||||||
|
|
||||||
|
def _export_camera(
|
||||||
|
field: Field, full_path: str
|
||||||
|
) -> tuple[MediaAuthResolution, str | None]:
|
||||||
try:
|
try:
|
||||||
export = Export.get(Export.video_path == full_path)
|
export = Export.get(field == full_path)
|
||||||
return MediaAuthResolution.CAMERA, export.camera
|
return MediaAuthResolution.CAMERA, export.camera
|
||||||
except DoesNotExist:
|
except DoesNotExist:
|
||||||
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("Export DB lookup failed for %s: %s", filename, e)
|
logger.warning("Export DB lookup failed for %s: %s", full_path, e)
|
||||||
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
return MediaAuthResolution.UNRESOLVED_MEDIA, None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ from frigate.api.media_auth import (
|
|||||||
resolve_media_uri,
|
resolve_media_uri,
|
||||||
)
|
)
|
||||||
from frigate.config import FrigateConfig
|
from frigate.config import FrigateConfig
|
||||||
from frigate.const import EXPORT_DIR
|
from frigate.const import CLIPS_DIR, EXPORT_DIR
|
||||||
from frigate.models import Event, Export, Recordings, ReviewSegment
|
from frigate.models import Event, Export, Recordings, ReviewSegment
|
||||||
from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS, TEST_DB_PRAGMAS
|
from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS, TEST_DB_PRAGMAS
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ class TestExportResolution(unittest.TestCase):
|
|||||||
name=f"export-{export_id}",
|
name=f"export-{export_id}",
|
||||||
date=int(datetime.datetime.now().timestamp()),
|
date=int(datetime.datetime.now().timestamp()),
|
||||||
video_path=os.path.join(EXPORT_DIR, filename),
|
video_path=os.path.join(EXPORT_DIR, filename),
|
||||||
thumb_path=os.path.join(EXPORT_DIR, f"{filename}.jpg"),
|
thumb_path=os.path.join(CLIPS_DIR, "export", f"{export_id}.webp"),
|
||||||
in_progress=False,
|
in_progress=False,
|
||||||
).execute()
|
).execute()
|
||||||
|
|
||||||
@@ -280,6 +280,34 @@ class TestExportResolution(unittest.TestCase):
|
|||||||
resolution, _ = resolve_media_uri("/exports/clip.mp4", self.config)
|
resolution, _ = resolve_media_uri("/exports/clip.mp4", self.config)
|
||||||
self.assertEqual(resolution, MediaAuthResolution.UNRESOLVED_MEDIA)
|
self.assertEqual(resolution, MediaAuthResolution.UNRESOLVED_MEDIA)
|
||||||
|
|
||||||
|
def test_export_thumbnail_resolves_camera(self):
|
||||||
|
self._insert_export("back_door_abc123", "back_door", "back_door_clip.mp4")
|
||||||
|
resolution, camera = resolve_media_uri(
|
||||||
|
"/clips/export/back_door_abc123.webp", self.config
|
||||||
|
)
|
||||||
|
self.assertEqual(resolution, MediaAuthResolution.CAMERA)
|
||||||
|
self.assertEqual(camera, "back_door")
|
||||||
|
|
||||||
|
def test_unknown_export_thumbnail_is_unresolved(self):
|
||||||
|
resolution, _ = resolve_media_uri("/clips/export/missing.webp", self.config)
|
||||||
|
self.assertEqual(resolution, MediaAuthResolution.UNRESOLVED_MEDIA)
|
||||||
|
|
||||||
|
def test_export_thumbnail_gated_by_camera_access(self):
|
||||||
|
self._insert_export("back_door_abc123", "back_door", "back_door_clip.mp4")
|
||||||
|
self._insert_export("front_door_def456", "front_door", "front_door_clip.mp4")
|
||||||
|
url = "/clips/export/{}.webp"
|
||||||
|
self.assertIsNone(
|
||||||
|
deny_response_for_media_uri(
|
||||||
|
url.format("front_door_def456"), "limited_user", self.config
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
deny_response_for_media_uri(
|
||||||
|
url.format("back_door_abc123"), "limited_user", self.config
|
||||||
|
),
|
||||||
|
403,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestDenyResponseForMediaUri(unittest.TestCase):
|
class TestDenyResponseForMediaUri(unittest.TestCase):
|
||||||
"""End-to-end decision check used by /auth."""
|
"""End-to-end decision check used by /auth."""
|
||||||
|
|||||||
Reference in New Issue
Block a user